Commit 49514924 authored by Jeffrey Shell's avatar Jeffrey Shell

Seperated SMTP calls into seperate class, added (hopefully) working

good python-callable interface.
parent ad86a253
......@@ -22,8 +22,7 @@ def add(self, id='mailForm', title='Some mail thing', smtp_host=None,
class MailForm(Persistent, Acquisition.Implicit):
'a mailform...?'
manage=HTMLFile('MailForm/manageMailForm')
index_html=HTMLFile('MailForm/manageMailForm')
index_html=HTMLFile('MailForm/mailForm')
def __init__(self):
'nothing yet'
......@@ -48,49 +47,81 @@ class MailForm(Persistent, Acquisition.Implicit):
def send(trueself, self):
'uhh, sponges off the request and mails it..?'
messageText=getattr(self,self.mailTemplate)(self, trueself.REQUEST)
headers, message=decapitate(messageText)
if trueself.REQUEST.has_key('d_template'):
mtemplate = getattr(self, trueself.REQUEST['d_template'])
else:
mtemplate = getattr(self, trueself.mailTemplate)
messageText = mtemplate(self, trueself.REQUEST)
headers, message = decapitate(messageText)
for requiredHeader in ('to', 'from', 'subject'):
if not headers.has_key(requiredHeader):
raise MailFormError, "Message missing SMTP Header '%s'" \
% requiredHeader
trueself._mailConnect()
trueself._send(mfrom=headers['from'], mto=headers['to'],
SendMail(self.smtpHost, self.smtpPort, self.localHost).send(
mfrom=headers['from'], mto=headers['to'],
subj=headers['subject'], body=messageText)
trueself._close()
return getattr(self,self.sentMailTemplate)(self, self.REQUEST,
messageText=message)
def _mailConnect(self):
self._v_conn = socket(AF_INET, SOCK_STREAM)
self._v_conn.connect(self.smtpHost, self.smtpPort)
self._v_conn.send("helo "+self.localHost+"\r\n")
def trueSend(trueself, self=None, REQUEST=None, **kw):
if REQUEST: kw=REQUEST
if not self: self=trueself
if kw.has_key('d_template'):
mtemplate = getattr(self, kw['d_template'])
else:
mtemplate = getattr(self, trueself.mailTemplate)
messageText = mtemplate(self, kw)
headers, message = decapitate(messageText)
for requiredHeader in ('to', 'from', 'subject'):
if not headers.has_key(requiredHeader):
raise MailFormError, "Message missing SMTP Header '%s'" \
% requiredHeader
SendMail(trueself.smtpHost, trueself.smtpPort,
trueself.localHost).send(
mfrom=headers['from'], mto=headers['to'],
subj=headers['subject'], body=messageText
)
return getattr(trueself,trueself.sentMailTemplate)(self, kw,
messageText=message)
class SendMail:
def __init__(self, smtpHost, smtpPort, localHost="localhost"):
self.conn = socket(AF_INET, SOCK_STREAM)
self.conn.connect(smtpHost, smtpPort)
self.conn.send("helo "+localHost+"\r\n")
self._check('220')
def __del__(self):
self.close()
def _check(self, lev='250'):
data = self._v_conn.recv(1024)
data = self.conn.recv(1024)
if data[:3] != lev:
raise smtpError, "Expected %s, got %s from SMTP"%(lev, data[:3])
def _send(self, mfrom, mto, subj, body):
self._v_conn.send("mail from:<%s>\n"%mfrom)
def send(self, mfrom, mto, subj, body):
self.conn.send("mail from:<%s>\n"%mfrom)
self._check()
if type(mto) == type([1,2]):
for person in mto:
self._v_conn.send("rcpt to:<%s>\n" % person)
self.conn.send("rcpt to:<%s>\n" % person)
self._check()
else:
self._v_conn.send("rcpt to:<%s>\n"%mto)
self.conn.send("rcpt to:<%s>\n"%mto)
self._check()
self._v_conn.send("data\n")
self.conn.send("data\n")
self._check()
self._v_conn.send(body)
self._v_conn.send("\n.\n")
self.conn.send(body)
self.conn.send("\n.\n")
self._check('354')
def _close(self):
self._v_conn.send("quit\n")
self._v_conn.close()
self.conn.send("quit\n")
self.conn.close()
def decapitate(message,
header_re=regex.compile(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment