[3356] | 1 | ############################################################################## |
---|
| 2 | # |
---|
| 3 | # Copyright (c) 2002 Zope Foundation and Contributors. |
---|
| 4 | # |
---|
| 5 | # This software is subject to the provisions of the Zope Public License, |
---|
| 6 | # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. |
---|
| 7 | # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED |
---|
| 8 | # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
---|
| 9 | # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
---|
| 10 | # FOR A PARTICULAR PURPOSE |
---|
| 11 | # |
---|
| 12 | ############################################################################## |
---|
| 13 | |
---|
| 14 | from DocumentTemplate.DT_Util import parse_params |
---|
| 15 | from DocumentTemplate.DT_Util import render_blocks |
---|
| 16 | from DocumentTemplate.DT_String import String |
---|
| 17 | |
---|
| 18 | from Products.MailHost.MailHost import MailBase, MailHostError |
---|
| 19 | |
---|
| 20 | |
---|
| 21 | class SendMailTag: |
---|
| 22 | '''the send mail tag, used like thus: |
---|
| 23 | |
---|
| 24 | <dtml-sendmail mailhost="someMailHostID"> |
---|
| 25 | to: person@their.machine.com |
---|
| 26 | from: me@mymachine.net |
---|
| 27 | subject: just called to say... |
---|
| 28 | |
---|
| 29 | boy howdy! |
---|
| 30 | </dtml-sendmail> |
---|
| 31 | |
---|
| 32 | Text between the sendmail and /sendmail tags is processed |
---|
| 33 | by the MailHost machinery and delivered. There must be at least |
---|
| 34 | one blank line seperating the headers (to/from/etc..) from the body |
---|
| 35 | of the message. |
---|
| 36 | |
---|
| 37 | Instead of specifying a MailHost, an smtphost may be specified |
---|
| 38 | ala 'smtphost="mail.mycompany.com" port=25' (port defaults to 25 |
---|
| 39 | automatically). Other parameters are |
---|
| 40 | |
---|
| 41 | * mailto -- person (or comma-seperated list of persons) to send the |
---|
| 42 | mail to. If not specified, there **must** be a to: header in the |
---|
| 43 | message. |
---|
| 44 | |
---|
| 45 | * mailfrom -- person sending the mail (basically who the recipient can |
---|
| 46 | reply to). If not specified, there **must** be a from: header in the |
---|
| 47 | message. |
---|
| 48 | |
---|
| 49 | * subject -- optional subject. If not specified, there **must** be a |
---|
| 50 | subject: header in the message. |
---|
| 51 | |
---|
| 52 | * encode -- optional encoding. Possible values are: 'base64', |
---|
| 53 | 'quoted-printable' and 'uuencode'. |
---|
| 54 | |
---|
| 55 | ''' |
---|
| 56 | |
---|
| 57 | name='sendmail' |
---|
| 58 | blockContinuations=() |
---|
| 59 | encode=None |
---|
| 60 | |
---|
| 61 | def __init__(self, blocks): |
---|
| 62 | tname, args, section=blocks[0] |
---|
| 63 | args=parse_params(args, mailhost=None, mailto=None, mailfrom=None, |
---|
| 64 | subject=None, smtphost=None, port='25', |
---|
| 65 | encode=None) |
---|
| 66 | |
---|
| 67 | smtphost=None |
---|
| 68 | |
---|
| 69 | has_key=args.has_key |
---|
| 70 | if has_key('mailhost'): |
---|
| 71 | mailhost = args['mailhost'] |
---|
| 72 | elif has_key('smtphost'): |
---|
| 73 | mailhost = smtphost = args['smtphost'] |
---|
| 74 | elif has_key(''): |
---|
| 75 | mailhost = args['mailhost'] = args[''] |
---|
| 76 | else: |
---|
| 77 | raise MailHostError('No mailhost was specified in tag') |
---|
| 78 | |
---|
| 79 | for key in ('mailto', 'mailfrom', 'subject', 'port'): |
---|
| 80 | if not key in args: |
---|
| 81 | args[key] = '' |
---|
| 82 | |
---|
| 83 | if has_key('encode') and args['encode'] not in \ |
---|
| 84 | ('base64', 'quoted-printable', 'uuencode', 'x-uuencode', |
---|
| 85 | 'uue', 'x-uue'): |
---|
| 86 | raise MailHostError( |
---|
| 87 | 'An unsupported encoding was specified in tag') |
---|
| 88 | |
---|
| 89 | if not smtphost: |
---|
| 90 | self.__name__=self.mailhost=mailhost |
---|
| 91 | self.smtphost=None |
---|
| 92 | else: |
---|
| 93 | self.__name__=self.smtphost=smtphost |
---|
| 94 | self.mailhost=None |
---|
| 95 | self.section=section |
---|
| 96 | self.args=args |
---|
| 97 | self.mailto=args['mailto'] |
---|
| 98 | self.mailfrom=args['mailfrom'] |
---|
| 99 | self.subject=None or args['subject'] |
---|
| 100 | if args['port'] and type(args['port']) is type('s'): |
---|
| 101 | self.port=args['port']=int(args['port']) |
---|
| 102 | elif args['port']=='': |
---|
| 103 | self.port=args['port']=25 |
---|
| 104 | else: |
---|
| 105 | self.port=args['port'] |
---|
| 106 | if has_key('encode'): |
---|
| 107 | self.encode=args['encode'] |
---|
| 108 | else: |
---|
| 109 | self.encode=None |
---|
| 110 | |
---|
| 111 | def render(self, md): |
---|
| 112 | if self.mailhost: |
---|
| 113 | mhost = md[self.mailhost] |
---|
| 114 | elif self.smtphost: |
---|
| 115 | mhost = MailBase(smtp_host=self.smtphost, smtp_port=self.port) |
---|
| 116 | |
---|
| 117 | mhost.send(render_blocks(self.section.blocks, md), |
---|
| 118 | self.mailto, self.mailfrom, |
---|
| 119 | self.subject, self.encode) |
---|
| 120 | |
---|
| 121 | return ' ' |
---|
| 122 | |
---|
| 123 | __call__ = render |
---|
| 124 | |
---|
| 125 | String.commands['sendmail'] = SendMailTag |
---|