~jteh/bzr-email/pushPullEnhancements

« back to all changes in this revision

Viewing changes to smtp_connection.py

  • Committer: James Teh
  • Date: 2011-06-08 07:08:58 UTC
  • mfrom: (40.2.9 trunk)
  • Revision ID: jamie@jantrid.net-20110608070858-mmmg2in7iobmzp28
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
77
77
                msg="Unable to connect to smtp server to send email to",
78
78
                orig_error=e)
79
79
 
 
80
        # If the server is sending a weird TLS error, but the SMTP would work
 
81
        # fine without requesting TLS, offer the user a chance to ignore the TLS call.
 
82
        if self._config.get_user_option('smtp_ignore_tls'):
 
83
            return
 
84
 
80
85
        # If this fails, it just returns an error, but it shouldn't raise an
81
86
        # exception unless something goes really wrong (in which case we want
82
87
        # to fail anyway).
123
128
        """
124
129
        return parseaddr(address)
125
130
 
126
 
    def _basic_message(self, from_address, to_addresses, subject):
 
131
    def _basic_message(self, from_address, to_addresses, subject,
 
132
        extra_mail_headers=None):
127
133
        """Create the basic Message using the right Header info.
128
134
 
129
135
        This creates an email Message with no payload.
146
152
        msg['From'] = '%s <%s>' % (Header(unicode(from_user)), from_email)
147
153
        msg['User-Agent'] = 'bzr/%s' % _bzrlib_version
148
154
 
 
155
        # MIMEMultipart doesn't support update()
 
156
        if extra_mail_headers:
 
157
            for key in extra_mail_headers:
 
158
                msg[key] = extra_mail_headers[key]
 
159
 
149
160
        to_emails = []
150
161
        to_header = []
151
162
        for addr in to_addresses:
157
168
        msg['Subject'] = Header(subject)
158
169
        return msg, from_email, to_emails
159
170
 
160
 
    def create_email(self, from_address, to_addresses, subject, text):
 
171
    def create_email(self, from_address, to_addresses, subject, text,
 
172
        extra_mail_headers=None):
161
173
        """Create an email.Message object.
162
174
 
163
175
        This function allows you to create a basic email, and then add extra
178
190
            to_emails: the list of email addresses extracted from to_addresses
179
191
        """
180
192
        msg, from_email, to_emails = self._basic_message(from_address,
181
 
                                                         to_addresses, subject)
 
193
            to_addresses, subject, extra_mail_headers)
182
194
        payload = MIMEText(text.encode('utf-8'), 'plain', 'utf-8')
183
195
        msg.attach(payload)
184
196
        return msg, from_email, to_emails
202
214
        self._connection.sendmail(from_email, to_emails,
203
215
                                  email_message.as_string())
204
216
 
205
 
    def send_text_email(self, from_address, to_addresses, subject, message):
 
217
    def send_text_email(self, from_address, to_addresses, subject, message,
 
218
                        extra_mail_headers=None):
206
219
        """Send a single text-only email.
207
220
 
208
221
        This is a helper when you know you are just sending a simple text
209
222
        message. See create_email for an explanation of parameters.
210
223
        """
211
224
        msg, from_email, to_emails = self.create_email(from_address,
212
 
                                            to_addresses, subject, message)
 
225
                                            to_addresses, subject, message,
 
226
                                            extra_mail_headers)
213
227
        self.send_email(msg, from_email, to_emails)
214
228
 
215
229
    def send_text_and_attachment_email(self, from_address, to_addresses,
216
230
                                       subject, message, attachment_text,
217
 
                                       attachment_filename='patch.diff'):
 
231
                                       attachment_filename='patch.diff',
 
232
                                       extra_mail_headers=None):
218
233
        """Send a Unicode message and an 8-bit attachment.
219
234
 
220
235
        See create_email for common parameter definitions.
225
240
            give a default name for email programs to save the attachment.
226
241
        """
227
242
        msg, from_email, to_emails = self.create_email(from_address,
228
 
                                            to_addresses, subject, message)
 
243
                                            to_addresses, subject, message,
 
244
                                            extra_mail_headers)
229
245
        # Must be an 8-bit string
230
246
        assert isinstance(attachment_text, str)
231
247