paul@83 | 1 | #!/usr/bin/env python |
paul@83 | 2 | |
paul@146 | 3 | """ |
paul@146 | 4 | Mail preparation support. |
paul@146 | 5 | |
paul@146 | 6 | Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> |
paul@146 | 7 | |
paul@146 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@146 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@146 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@146 | 11 | version. |
paul@146 | 12 | |
paul@146 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@146 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@146 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@146 | 16 | details. |
paul@146 | 17 | |
paul@146 | 18 | You should have received a copy of the GNU General Public License along with |
paul@146 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@146 | 20 | """ |
paul@146 | 21 | |
paul@128 | 22 | from imiptools.config import MESSAGE_SENDER, OUTGOING_PREFIX |
paul@83 | 23 | from email.mime.message import MIMEMessage |
paul@83 | 24 | from email.mime.multipart import MIMEMultipart |
paul@83 | 25 | from email.mime.text import MIMEText |
paul@83 | 26 | from smtplib import LMTP, SMTP |
paul@83 | 27 | |
paul@83 | 28 | MESSAGE_SUBJECT = "Calendar system message" |
paul@83 | 29 | |
paul@83 | 30 | MESSAGE_TEXT = """\ |
paul@139 | 31 | This is a message from the calendar system. |
paul@83 | 32 | """ |
paul@83 | 33 | |
paul@87 | 34 | PREAMBLE_TEXT = """\ |
paul@87 | 35 | This message contains several different parts, one of which will contain |
paul@87 | 36 | calendar information that will only be understood by a suitable program. |
paul@87 | 37 | """ |
paul@87 | 38 | |
paul@83 | 39 | class Messenger: |
paul@83 | 40 | |
paul@83 | 41 | "Sending of outgoing messages." |
paul@83 | 42 | |
paul@87 | 43 | def __init__(self, sender=None, subject=None, body_text=None, preamble_text=None): |
paul@83 | 44 | self.sender = sender or MESSAGE_SENDER |
paul@83 | 45 | self.subject = subject or MESSAGE_SUBJECT |
paul@83 | 46 | self.body_text = body_text or MESSAGE_TEXT |
paul@87 | 47 | self.preamble_text = preamble_text or PREAMBLE_TEXT |
paul@83 | 48 | |
paul@128 | 49 | def sendmail(self, recipients, data, sender=None, outgoing_bcc=None, lmtp_socket=None): |
paul@83 | 50 | |
paul@83 | 51 | """ |
paul@83 | 52 | Send a mail to the given 'recipients' consisting of the given 'data', |
paul@128 | 53 | using the given 'sender' identity if indicated, indicating an |
paul@128 | 54 | 'outgoing_bcc' identity if indicated, delivering to a local mail system |
paul@128 | 55 | using LMTP if 'lmtp_socket' is provided. |
paul@83 | 56 | """ |
paul@83 | 57 | |
paul@83 | 58 | if lmtp_socket: |
paul@83 | 59 | smtp = LMTP(lmtp_socket) |
paul@83 | 60 | else: |
paul@83 | 61 | smtp = SMTP("localhost") |
paul@83 | 62 | |
paul@128 | 63 | if outgoing_bcc: |
paul@128 | 64 | recipients = list(recipients) + ["%s+%s" % (OUTGOING_PREFIX, outgoing_bcc)] |
paul@128 | 65 | |
paul@86 | 66 | smtp.sendmail(sender or self.sender, recipients, data) |
paul@83 | 67 | smtp.quit() |
paul@83 | 68 | |
paul@128 | 69 | def make_message(self, parts, recipients, sender=None, outgoing_bcc=None): |
paul@83 | 70 | |
paul@86 | 71 | """ |
paul@86 | 72 | Make a message from the given 'parts' for the given 'recipients', using |
paul@128 | 73 | the given 'sender' identity if indicated, indicating an 'outgoing_bcc' |
paul@128 | 74 | identity if indicated. |
paul@86 | 75 | """ |
paul@83 | 76 | |
paul@83 | 77 | message = MIMEMultipart("mixed", _subparts=parts) |
paul@87 | 78 | message.preamble = self.preamble_text |
paul@83 | 79 | payload = message.get_payload() |
paul@83 | 80 | payload.insert(0, MIMEText(self.body_text)) |
paul@83 | 81 | |
paul@86 | 82 | message["From"] = sender or self.sender |
paul@83 | 83 | for recipient in recipients: |
paul@83 | 84 | message["To"] = recipient |
paul@128 | 85 | if outgoing_bcc: |
paul@128 | 86 | message["Bcc"] = "%s+%s" % (OUTGOING_PREFIX, outgoing_bcc) |
paul@83 | 87 | message["Subject"] = self.subject |
paul@83 | 88 | |
paul@83 | 89 | return message |
paul@83 | 90 | |
paul@83 | 91 | def wrap_message(self, msg, parts): |
paul@83 | 92 | |
paul@83 | 93 | "Wrap 'msg' and provide the given 'parts' as the primary content." |
paul@83 | 94 | |
paul@83 | 95 | message = MIMEMultipart("mixed", _subparts=parts) |
paul@87 | 96 | message.preamble = self.preamble_text |
paul@87 | 97 | payload = message.get_payload() |
paul@87 | 98 | payload.insert(0, MIMEText(self.body_text)) |
paul@87 | 99 | payload.append(MIMEMessage(msg)) |
paul@83 | 100 | |
paul@83 | 101 | message["From"] = msg["From"] |
paul@83 | 102 | message["To"] = msg["To"] |
paul@83 | 103 | message["Subject"] = msg["Subject"] |
paul@83 | 104 | |
paul@83 | 105 | return message |
paul@83 | 106 | |
paul@83 | 107 | # vim: tabstop=4 expandtab shiftwidth=4 |