/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: 2018-06-14 17:59:16 UTC
  • mto: This revision was merged to the branch mainline in revision 7065.
  • Revision ID: jelmer@jelmer.uk-20180614175916-a2e2xh5k533guq1x
Move breezy.plugins.git to breezy.git.

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
try:
 
22
    from email.message import Message
 
23
    from email.header import Header
 
24
    from email.mime.multipart import MIMEMultipart
 
25
    from email.mime.text import MIMEText
 
26
    from email.utils import formataddr, parseaddr
 
27
except ImportError:   # python < 3
 
28
    from email import (
 
29
        Header,
 
30
        Message,
 
31
        MIMEMultipart,
 
32
        MIMEText,
 
33
        )
 
34
    from email.Utils import formataddr, parseaddr
24
35
from . import __version__ as _breezy_version
25
 
from .errors import BzrBadParameterNotUnicode
26
36
from .osutils import safe_unicode
 
37
from .sixish import (
 
38
    text_type,
 
39
    )
27
40
from .smtp_connection import SMTPConnection
28
41
 
29
42
 
56
69
        self._body = body
57
70
        self._parts = []
58
71
 
59
 
        if isinstance(to_address, (bytes, str)):
60
 
            to_address = [to_address]
 
72
        if isinstance(to_address, (str, text_type)):
 
73
            to_address = [ to_address ]
61
74
 
62
75
        to_addresses = []
63
76
 
156
169
        msg = EmailMessage(from_address, to_address, subject, body)
157
170
        if attachment is not None:
158
171
            msg.add_inline_attachment(attachment, attachment_filename,
159
 
                                      attachment_mime_subtype)
 
172
                    attachment_mime_subtype)
160
173
        SMTPConnection(config).send_email(msg)
161
174
 
162
175
    @staticmethod
166
179
        :param address: An unicode string, or UTF-8 byte string.
167
180
        :return: A possibly RFC2047-encoded string.
168
181
        """
169
 
        if not isinstance(address, str):
170
 
            raise BzrBadParameterNotUnicode(address)
171
182
        # Can't call Header over all the address, because that encodes both the
172
183
        # name and the email address, which is not permitted by RFCs.
173
184
        user, email = parseaddr(address)
175
186
            return email
176
187
        else:
177
188
            return formataddr((str(Header(safe_unicode(user))),
178
 
                               email))
 
189
                email))
179
190
 
180
191
    @staticmethod
181
192
    def string_with_encoding(string_):
190
201
        # avoid base64 when it's not necessary in order to be most compatible
191
202
        # with the capabilities of the receiving side, we check with encode()
192
203
        # and decode() whether the body is actually ascii-only.
193
 
        if isinstance(string_, str):
 
204
        if isinstance(string_, unicode):
194
205
            try:
195
206
                return (string_.encode('ascii'), 'ascii')
196
207
            except UnicodeEncodeError: