Commit e1add391 authored by Jeffrey Shell's avatar Jeffrey Shell

minor cleanups, added SendMailTag class for DTML.

parent 1237bcb1
"""MailHost, a Principia SMTP object"""
from Globals import Persistent, HTMLFile, HTML, MessageDialog from Globals import Persistent, HTMLFile, HTML, MessageDialog
from socket import *; from select import select from socket import *; from select import select
from AccessControl.Role import RoleManager from AccessControl.Role import RoleManager
...@@ -7,8 +9,8 @@ import Globals ...@@ -7,8 +9,8 @@ import Globals
from Scheduler.OneTimeEvent import OneTimeEvent from Scheduler.OneTimeEvent import OneTimeEvent
from ImageFile import ImageFile from ImageFile import ImageFile
#$Id: MailHost.py,v 1.22 1997/12/31 21:15:19 brian Exp $ #$Id: MailHost.py,v 1.23 1998/01/02 22:55:44 jeffrey Exp $
__version__ = "$Revision: 1.22 $"[11:-2] __version__ = "$Revision: 1.23 $"[11:-2]
smtpError = "SMTP Error" smtpError = "SMTP Error"
MailHostError = "MailHost Error" MailHostError = "MailHost Error"
...@@ -24,8 +26,7 @@ def add(self, id, title='', smtp_host=None, ...@@ -24,8 +26,7 @@ def add(self, id, title='', smtp_host=None,
if REQUEST: return self.manage_main(self,REQUEST) if REQUEST: return self.manage_main(self,REQUEST)
class MailHost(Persistent, Acquisition.Implicit, OFS.SimpleItem.Item, class MailBase(Acquisition.Implicit, OFS.SimpleItem.Item, RoleManager):
RoleManager):
'a mailhost...?' 'a mailhost...?'
meta_type='Mail Host' meta_type='Mail Host'
manage=manage_main=HTMLFile('manageMailHost', globals()) manage=manage_main=HTMLFile('manageMailHost', globals())
...@@ -61,7 +62,8 @@ class MailHost(Persistent, Acquisition.Implicit, OFS.SimpleItem.Item, ...@@ -61,7 +62,8 @@ class MailHost(Persistent, Acquisition.Implicit, OFS.SimpleItem.Item,
self.smtpPort=smtpPort self.smtpPort=smtpPort
self.sentMessages=0 self.sentMessages=0
def manage_makeChanges(self,title,localHost,smtpHost,smtpPort,REQUEST=None): def manage_makeChanges(self,title,localHost,smtpHost,smtpPort,
REQUEST=None):
'make the changes' 'make the changes'
self.title=title self.title=title
self.localHost=localHost self.localHost=localHost
...@@ -97,9 +99,16 @@ class MailHost(Persistent, Acquisition.Implicit, OFS.SimpleItem.Item, ...@@ -97,9 +99,16 @@ class MailHost(Persistent, Acquisition.Implicit, OFS.SimpleItem.Item,
threadsafe=1 threadsafe=1
)) ))
return "SEND OK" if not statusTemplate: return "SEND OK"
try:
stemplate=getattr(self, statusTemplate)
return stemplate(self, trueself.REQUEST)
except:
return "SEND OK"
def send(self, messageText, mto=None, mfrom=None): def send(self, messageText, mto=None, mfrom=None, subject=None):
if subject: messageText="subject: %s\n%s" % (subject, messageText)
headers, message = newDecapitate(messageText) headers, message = newDecapitate(messageText)
if mto: headers['to'] = mto if mto: headers['to'] = mto
if mfrom: headers['from'] = mfrom if mfrom: headers['from'] = mfrom
...@@ -112,7 +121,8 @@ class MailHost(Persistent, Acquisition.Implicit, OFS.SimpleItem.Item, ...@@ -112,7 +121,8 @@ class MailHost(Persistent, Acquisition.Implicit, OFS.SimpleItem.Item,
mfrom=headers['from'], mto=headers['to'], mfrom=headers['from'], mto=headers['to'],
subj=headers['subject'], body=messageText) subj=headers['subject'], body=messageText)
def scheduledSend(self, messageText, mto=None, mfrom=None): def scheduledSend(self, messageText, mto=None, mfrom=None, subject=None):
if subject: messageText="subject: %s\n%s" % (subject, messageText)
headers, message = newDecapitate(messageText) headers, message = newDecapitate(messageText)
if mto: headers['to'] = mto if mto: headers['to'] = mto
if mfrom: headers['from'] = mfrom if mfrom: headers['from'] = mfrom
...@@ -135,6 +145,9 @@ class MailHost(Persistent, Acquisition.Implicit, OFS.SimpleItem.Item, ...@@ -135,6 +145,9 @@ class MailHost(Persistent, Acquisition.Implicit, OFS.SimpleItem.Item,
SendMail(self.smtpHost, self.smtpPort, self.localHost).send( SendMail(self.smtpHost, self.smtpPort, self.localHost).send(
mfrom=mfrom, mto=mto, subj=subject, body=body) mfrom=mfrom, mto=mto, subj=subject, body=body)
class MailHost(Persistent, MailBase):
"persistent version"
def Send(host, port, localhost, from_, to, subject, body): def Send(host, port, localhost, from_, to, subject, body):
SendMail(host, port, localhost).send(from_, to, subject, body) SendMail(host, port, localhost).send(from_, to, subject, body)
...@@ -170,20 +183,19 @@ class SendMail: ...@@ -170,20 +183,19 @@ class SendMail:
raise smtpError, "Cannot convert line from SMTP: %s" % line raise smtpError, "Cannot convert line from SMTP: %s" % line
if code > 500: if code > 500:
#raise smtpError, "Expected %s, got %s from SMTP" % (lev, data[:3])
raise smtpError, "Recieved error code %s from SMTP: %s"\ raise smtpError, "Recieved error code %s from SMTP: %s"\
% (code, line) % (code, line)
return 1 return 1
def send(self, mfrom, mto, subj, body): def send(self, mfrom, mto, subj, body):
self.conn.send("mail from:<%s>\n"%mfrom) self.conn.send("mail from:<%s>\n" % mfrom)
self._check() self._check()
if type(mto) in [types.ListType, types.TupleType]: if type(mto) in [types.ListType, types.TupleType]:
for person in mto: for person in mto:
self.conn.send("rcpt to:<%s>\n" % person) self.conn.send("rcpt to:<%s>\n" % person)
self._check() self._check()
else: else:
self.conn.send("rcpt to:<%s>\n"%mto) self.conn.send("rcpt to:<%s>\n" % mto)
self._check() self._check()
self.conn.send("data\n") self.conn.send("data\n")
self._check() self._check()
...@@ -228,7 +240,104 @@ def decapitate(message, **kw): ...@@ -228,7 +240,104 @@ def decapitate(message, **kw):
return newDecapitate(message) return newDecapitate(message)
from DocumentTemplate.DT_Util import *
from DocumentTemplate.DT_String import String
class SendMailTag:
'''the send mail tag, used like thus:
<!--#sendmail someMailHostID-->
to: person@their.machine.com
from: me@mymachine.net
subject: just called to say...
boy howdy!
<!--#/sendmail-->
Text between the sendmail and /sendmail tags is processed
by the MailHost machinery and delivered. There must be at least
one blank line seperating the headers (to/from/etc..) from the body
of the message.
Instead of specifying a MailHost, an smtphost may be specified
ala 'smtphost="mail.mycompany.com" port=25' (port defaults to 25
automatically). Other parameters are
* mailto -- person (or comma-seperated list of persons) to send the
mail to. If not specified, there **must** be a to: header in the
message.
* mailfrom -- person sending the mail (basically who the recipient can
reply to). If not specified, there **must** be a from: header in the
message.
* subject -- optional subject. If not specified, there **must** be a
subject: header in the message.
'''
name='sendmail'
blockContinuations=()
def __init__(self, blocks):
tname, args, section=blocks[0]
args=parse_params(args, mailhost=None, mailto=None, mailfrom=None,
subject=None, smtphost=None, port='25')
for key in ('mailto', 'mailfrom', 'subject', 'smtphost', 'port'):
if not args.has_key(key):args[key]=''
smtphost=None
has_key=args.has_key
if has_key('mailhost'): mailhost=args['mailhost']
elif has_key('smtphost'): mailhost=smtphost=args['smtphost']
elif has_key(''): mailhost=args['mailhost']=args['']
else: raise MailHostError, 'No mailhost was specified in tag'
if not smtphost:
self.__name__=self.mailhost=mailhost
self.smtphost=None
else:
self.__name__=self.smtphost=smtphost
self.mailhost=None
self.section=section
self.args=args
self.mailto=args['mailto']
self.mailfrom=args['mailfrom']
self.subject=None or args['subject']
if args['port'] and type(args['port']) is type('s'):
self.port=args['port']=string.atoi(args['port'])
elif args['port']=='':
self.port=args['port']=25
else:
self.port=args['port']
def render(self, md):
args=self.args
has_key=args.has_key
if self.mailhost:
mhost.send(self.section(md.this, md), self.mailto, self.mailfrom,
self.subject)
elif self.smtphost:
mhost=MailBase()
mhost._init(localHost=gethostname(), smtpHost=self.smtphost,
smtpPort=self.port)
mhost.send(self.section(md.this, md), self.mailto, self.mailfrom,
self.subject)
return ' '
__call__=render
String.commands['sendmail']=SendMailTag
####################################################################
#
#$Log: MailHost.py,v $ #$Log: MailHost.py,v $
#Revision 1.23 1998/01/02 22:55:44 jeffrey
#minor cleanups, added SendMailTag class for DTML.
#
#Revision 1.22 1997/12/31 21:15:19 brian #Revision 1.22 1997/12/31 21:15:19 brian
#Security update #Security update
# #
......
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