/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: 2020-05-06 02:13:25 UTC
  • mfrom: (7490.7.21 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200506021325-awbmmqu1zyorz7sj
Merge 3.1 branch.

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