imip-agent

imiptools/mail.py

487:8ff3ab2b456b
2015-04-05 Paul Boddie Hide any replaced start and end information for recurring events.
     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, sender=None, subject=None, body_text=None, preamble_text=None):    44         self.sender = sender or MESSAGE_SENDER    45         self.subject = subject or MESSAGE_SUBJECT    46         self.body_text = body_text or MESSAGE_TEXT    47         self.preamble_text = preamble_text or PREAMBLE_TEXT    48     49     def sendmail(self, recipients, data, sender=None, outgoing_bcc=None, lmtp_socket=None):    50     51         """    52         Send a mail to the given 'recipients' consisting of the given 'data',    53         using the given 'sender' identity if indicated, indicating an    54         'outgoing_bcc' identity if indicated, delivering to a local mail system    55         using LMTP if 'lmtp_socket' is provided.    56     57         The 'outgoing_bcc' argument is required when sending on behalf of a user    58         from the calendar@domain address, since this will not be detected as a    59         valid participant and handled using the outgoing transport.    60         """    61     62         if lmtp_socket:    63             smtp = LMTP(lmtp_socket)    64         else:    65             smtp = SMTP("localhost")    66     67         if outgoing_bcc:    68             recipients = list(recipients) + ["%s+%s" % (OUTGOING_PREFIX, outgoing_bcc)]    69     70         smtp.sendmail(sender or self.sender, recipients, data)    71         smtp.quit()    72     73     def make_outgoing_message(self, parts, recipients, sender=None, outgoing_bcc=None):    74     75         """    76         Make a message from the given 'parts' for the given 'recipients', using    77         the given 'sender' identity if indicated, indicating an 'outgoing_bcc'    78         identity if indicated.    79         """    80     81         message = self._make_summary_for_parts(parts)    82     83         message["From"] = sender or self.sender    84         for recipient in recipients:    85             message["To"] = recipient    86         if outgoing_bcc:    87             message["Bcc"] = "%s+%s" % (OUTGOING_PREFIX, outgoing_bcc)    88         message["Subject"] = self.subject    89     90         return message    91     92     def make_summary_message(self, msg, parts):    93     94         """    95         Return a simple summary using details from 'msg' and the given 'parts'.    96         """    97     98         message = self._make_summary_for_parts(parts)    99         self._copy_headers(message, msg)   100         return message   101    102     def wrap_message(self, msg, parts):   103    104         "Wrap 'msg' and provide the given 'parts' as the primary content."   105    106         message = self._make_container_for_parts(parts)   107         payload = message.get_payload()   108         payload.append(MIMEMessage(msg))   109         self._copy_headers(message, msg)   110         return message   111    112     def _make_summary_for_parts(self, parts):   113    114         "Return a simple summary for the given 'parts'."   115    116         if len(parts) == 1:   117             return parts[0]   118         else:   119             return self._make_container_for_parts(parts)   120    121     def _make_container_for_parts(self, parts):   122    123         "Return a container for the given 'parts'."   124    125         message = MIMEMultipart("mixed", _subparts=parts)   126         message.preamble = self.preamble_text   127         return message   128    129     def _copy_headers(self, message, msg):   130         message["From"] = msg["From"]   131         message["To"] = msg["To"]   132         message["Subject"] = msg["Subject"]   133    134 # vim: tabstop=4 expandtab shiftwidth=4