/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/email_message.py

  • Committer: Jelmer Vernooij
  • Date: 2017-06-08 23:30:31 UTC
  • mto: This revision was merged to the branch mainline in revision 6690.
  • Revision ID: jelmer@jelmer.uk-20170608233031-3qavls2o7a1pqllj
Update imports.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""A convenience class around email.Message and email.MIMEMultipart."""
18
18
 
19
 
from email.message import Message
20
 
from email.header import Header
21
 
from email.mime.multipart import MIMEMultipart
22
 
from email.mime.text import MIMEText
23
 
from email.utils import formataddr, parseaddr
 
19
from __future__ import absolute_import
 
20
 
 
21
from email import (
 
22
    Header,
 
23
    Message,
 
24
    MIMEMultipart,
 
25
    MIMEText,
 
26
    Utils,
 
27
    )
 
28
 
24
29
from . import __version__ as _breezy_version
25
 
from .errors import BzrBadParameterNotUnicode
26
30
from .osutils import safe_unicode
27
31
from .smtp_connection import SMTPConnection
28
32
 
56
60
        self._body = body
57
61
        self._parts = []
58
62
 
59
 
        if isinstance(to_address, (bytes, str)):
60
 
            to_address = [to_address]
 
63
        if isinstance(to_address, basestring):
 
64
            to_address = [ to_address ]
61
65
 
62
66
        to_addresses = []
63
67
 
66
70
 
67
71
        self._headers['To'] = ', '.join(to_addresses)
68
72
        self._headers['From'] = self.address_to_encoded_header(from_address)
69
 
        self._headers['Subject'] = Header(safe_unicode(subject))
 
73
        self._headers['Subject'] = Header.Header(safe_unicode(subject))
70
74
        self._headers['User-Agent'] = 'Bazaar (%s)' % _breezy_version
71
75
 
72
76
    def add_inline_attachment(self, body, filename=None, mime_subtype='plain'):
98
102
            Used for tests.
99
103
        """
100
104
        if not self._parts:
101
 
            msgobj = Message()
 
105
            msgobj = Message.Message()
102
106
            if self._body is not None:
103
107
                body, encoding = self.string_with_encoding(self._body)
104
108
                msgobj.set_payload(body, encoding)
105
109
        else:
106
 
            msgobj = MIMEMultipart()
 
110
            msgobj = MIMEMultipart.MIMEMultipart()
107
111
 
108
112
            if boundary is not None:
109
113
                msgobj.set_boundary(boundary)
110
114
 
111
115
            for body, filename, mime_subtype in self._parts:
112
116
                body, encoding = self.string_with_encoding(body)
113
 
                payload = MIMEText(body, mime_subtype, encoding)
 
117
                payload = MIMEText.MIMEText(body, mime_subtype, encoding)
114
118
 
115
119
                if filename is not None:
116
120
                    content_type = payload['Content-Type']
156
160
        msg = EmailMessage(from_address, to_address, subject, body)
157
161
        if attachment is not None:
158
162
            msg.add_inline_attachment(attachment, attachment_filename,
159
 
                                      attachment_mime_subtype)
 
163
                    attachment_mime_subtype)
160
164
        SMTPConnection(config).send_email(msg)
161
165
 
162
166
    @staticmethod
166
170
        :param address: An unicode string, or UTF-8 byte string.
167
171
        :return: A possibly RFC2047-encoded string.
168
172
        """
169
 
        if not isinstance(address, str):
170
 
            raise BzrBadParameterNotUnicode(address)
171
173
        # Can't call Header over all the address, because that encodes both the
172
174
        # name and the email address, which is not permitted by RFCs.
173
 
        user, email = parseaddr(address)
 
175
        user, email = Utils.parseaddr(address)
174
176
        if not user:
175
177
            return email
176
178
        else:
177
 
            return formataddr((str(Header(safe_unicode(user))),
178
 
                               email))
 
179
            return Utils.formataddr((str(Header.Header(safe_unicode(user))),
 
180
                email))
179
181
 
180
182
    @staticmethod
181
183
    def string_with_encoding(string_):
190
192
        # avoid base64 when it's not necessary in order to be most compatible
191
193
        # with the capabilities of the receiving side, we check with encode()
192
194
        # and decode() whether the body is actually ascii-only.
193
 
        if isinstance(string_, str):
 
195
        if isinstance(string_, unicode):
194
196
            try:
195
197
                return (string_.encode('ascii'), 'ascii')
196
198
            except UnicodeEncodeError: