1 #!/usr/bin/env python 2 3 """ 4 Mail preparation support. 5 6 Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 from imiptools.config import MESSAGE_SENDER, OUTGOING_PREFIX 23 from email.mime.message import MIMEMessage 24 from email.mime.multipart import MIMEMultipart 25 from email.mime.text import MIMEText 26 from smtplib import LMTP, SMTP 27 28 MESSAGE_SUBJECT = "Calendar system message" 29 30 MESSAGE_TEXT = """\ 31 This is a message from the calendar system. 32 """ 33 34 PREAMBLE_TEXT = """\ 35 This message contains several different parts, one of which will contain 36 calendar information that will only be understood by a suitable program. 37 """ 38 39 class Messenger: 40 41 "Sending of outgoing messages." 42 43 def __init__(self, lmtp_socket=None, local_smtp=False, sender=None, subject=None, body_text=None, preamble_text=None): 44 45 """ 46 Deliver to a local mail system using LMTP if 'lmtp_socket' is provided 47 or if 'local_smtp' is given as a true value. 48 """ 49 50 self.lmtp_socket = lmtp_socket 51 self.local_smtp = local_smtp 52 self.sender = sender or MESSAGE_SENDER 53 self.subject = subject or MESSAGE_SUBJECT 54 self.body_text = body_text or MESSAGE_TEXT 55 self.preamble_text = preamble_text or PREAMBLE_TEXT 56 57 def local_delivery(self): 58 59 "Return whether local delivery is performed using this messenger." 60 61 return self.lmtp_socket is not None or self.local_smtp 62 63 def sendmail(self, recipients, data, sender=None, outgoing_bcc=None): 64 65 """ 66 Send a mail to the given 'recipients' consisting of the given 'data', 67 using the given 'sender' identity if indicated, indicating an 68 'outgoing_bcc' identity if indicated. 69 70 The 'outgoing_bcc' argument is required when sending on behalf of a user 71 from the calendar@domain address, since this will not be detected as a 72 valid participant and handled using the outgoing transport. 73 """ 74 75 if self.lmtp_socket: 76 smtp = LMTP(self.lmtp_socket) 77 else: 78 smtp = SMTP("localhost") 79 80 if outgoing_bcc: 81 recipients = list(recipients) + ["%s+%s" % (OUTGOING_PREFIX, outgoing_bcc)] 82 elif self.local_smtp: 83 recipients = [self.make_local(recipient) for recipient in recipients] 84 85 smtp.sendmail(sender or self.sender, recipients, data) 86 smtp.quit() 87 88 def make_local(self, recipient): 89 90 """ 91 Make the 'recipient' an address for local delivery. For this to function 92 correctly, a virtual alias or equivalent must be defined for addresses 93 of the following form: 94 95 local+NAME@DOMAIN 96 97 Such aliases should direct delivery to the local recipient. 98 """ 99 100 parts = recipient.split("+", 1) 101 return "%s+%s" % ("local", parts[-1]) 102 103 def make_outgoing_message(self, parts, recipients, sender=None, outgoing_bcc=None): 104 105 """ 106 Make a message from the given 'parts' for the given 'recipients', using 107 the given 'sender' identity if indicated, indicating an 'outgoing_bcc' 108 identity if indicated. 109 """ 110 111 message = self._make_summary_for_parts(parts) 112 113 message["From"] = sender or self.sender 114 for recipient in recipients: 115 message["To"] = recipient 116 if outgoing_bcc: 117 message["Bcc"] = "%s+%s" % (OUTGOING_PREFIX, outgoing_bcc) 118 message["Subject"] = self.subject 119 120 return message 121 122 def make_summary_message(self, msg, parts): 123 124 """ 125 Return a simple summary using details from 'msg' and the given 'parts'. 126 """ 127 128 message = self._make_summary_for_parts(parts) 129 self._copy_headers(message, msg) 130 return message 131 132 def wrap_message(self, msg, parts): 133 134 "Wrap 'msg' and provide the given 'parts' as the primary content." 135 136 message = self._make_container_for_parts(parts) 137 payload = message.get_payload() 138 payload.append(MIMEMessage(msg)) 139 self._copy_headers(message, msg) 140 return message 141 142 def _make_summary_for_parts(self, parts): 143 144 "Return a simple summary for the given 'parts'." 145 146 if len(parts) == 1: 147 return parts[0] 148 else: 149 return self._make_container_for_parts(parts) 150 151 def _make_container_for_parts(self, parts): 152 153 "Return a container for the given 'parts'." 154 155 message = MIMEMultipart("mixed", _subparts=parts) 156 message.preamble = self.preamble_text 157 return message 158 159 def _copy_headers(self, message, msg): 160 message["From"] = msg["From"] 161 message["To"] = msg["To"] 162 message["Subject"] = msg["Subject"] 163 164 # vim: tabstop=4 expandtab shiftwidth=4