~maria-captains/bzr-email/serg

« back to all changes in this revision

Viewing changes to smtp_connection.py

  • Committer: Robert Collins
  • Date: 2010-03-11 07:24:31 UTC
  • mfrom: (41.1.7 email)
  • Revision ID: robertc@robertcollins.net-20100311072431-sff0rvfybn4pifm9
Merge patch from Steve Langasek adding support for arbitrary headers on revision notification emails.

Show diffs side-by-side

added added

removed removed

Lines of Context:
123
123
        """
124
124
        return parseaddr(address)
125
125
 
126
 
    def _basic_message(self, from_address, to_addresses, subject):
 
126
    def _basic_message(self, from_address, to_addresses, subject,
 
127
        extra_mail_headers=None):
127
128
        """Create the basic Message using the right Header info.
128
129
 
129
130
        This creates an email Message with no payload.
146
147
        msg['From'] = '%s <%s>' % (Header(unicode(from_user)), from_email)
147
148
        msg['User-Agent'] = 'bzr/%s' % _bzrlib_version
148
149
 
 
150
        # MIMEMultipart doesn't support update()
 
151
        if extra_mail_headers:
 
152
            for key in extra_mail_headers:
 
153
                msg[key] = extra_mail_headers[key]
 
154
 
149
155
        to_emails = []
150
156
        to_header = []
151
157
        for addr in to_addresses:
157
163
        msg['Subject'] = Header(subject)
158
164
        return msg, from_email, to_emails
159
165
 
160
 
    def create_email(self, from_address, to_addresses, subject, text):
 
166
    def create_email(self, from_address, to_addresses, subject, text,
 
167
        extra_mail_headers=None):
161
168
        """Create an email.Message object.
162
169
 
163
170
        This function allows you to create a basic email, and then add extra
178
185
            to_emails: the list of email addresses extracted from to_addresses
179
186
        """
180
187
        msg, from_email, to_emails = self._basic_message(from_address,
181
 
                                                         to_addresses, subject)
 
188
            to_addresses, subject, extra_mail_headers)
182
189
        payload = MIMEText(text.encode('utf-8'), 'plain', 'utf-8')
183
190
        msg.attach(payload)
184
191
        return msg, from_email, to_emails
214
221
 
215
222
    def send_text_and_attachment_email(self, from_address, to_addresses,
216
223
                                       subject, message, attachment_text,
217
 
                                       attachment_filename='patch.diff'):
 
224
                                       attachment_filename='patch.diff',
 
225
                                       extra_mail_headers=None):
218
226
        """Send a Unicode message and an 8-bit attachment.
219
227
 
220
228
        See create_email for common parameter definitions.
225
233
            give a default name for email programs to save the attachment.
226
234
        """
227
235
        msg, from_email, to_emails = self.create_email(from_address,
228
 
                                            to_addresses, subject, message)
 
236
                                            to_addresses, subject, message,
 
237
                                            extra_mail_headers)
229
238
        # Must be an 8-bit string
230
239
        assert isinstance(attachment_text, str)
231
240