/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-12-04 23:01:39 UTC
  • mto: This revision was merged to the branch mainline in revision 6839.
  • Revision ID: jelmer@jelmer.uk-20171204230139-1sc3c18ikwewdejm
Remove bytes_to_gzip; work with chunks instead.

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 import (
20
 
    Header,
21
 
    Message,
22
 
    MIMEMultipart,
23
 
    MIMEText,
24
 
    Utils,
 
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
 
35
from . import __version__ as _breezy_version
 
36
from .osutils import safe_unicode
 
37
from .sixish import (
 
38
    text_type,
25
39
    )
26
 
 
27
 
from bzrlib import __version__ as _bzrlib_version
28
 
from bzrlib.osutils import safe_unicode
29
 
from bzrlib.smtp_connection import SMTPConnection
 
40
from .smtp_connection import SMTPConnection
30
41
 
31
42
 
32
43
class EmailMessage(object):
58
69
        self._body = body
59
70
        self._parts = []
60
71
 
61
 
        if isinstance(to_address, basestring):
 
72
        if isinstance(to_address, (str, text_type)):
62
73
            to_address = [ to_address ]
63
74
 
64
75
        to_addresses = []
68
79
 
69
80
        self._headers['To'] = ', '.join(to_addresses)
70
81
        self._headers['From'] = self.address_to_encoded_header(from_address)
71
 
        self._headers['Subject'] = Header.Header(safe_unicode(subject))
72
 
        self._headers['User-Agent'] = 'Bazaar (%s)' % _bzrlib_version
 
82
        self._headers['Subject'] = Header(safe_unicode(subject))
 
83
        self._headers['User-Agent'] = 'Bazaar (%s)' % _breezy_version
73
84
 
74
85
    def add_inline_attachment(self, body, filename=None, mime_subtype='plain'):
75
86
        """Add an inline attachment to the message.
100
111
            Used for tests.
101
112
        """
102
113
        if not self._parts:
103
 
            msgobj = Message.Message()
 
114
            msgobj = Message()
104
115
            if self._body is not None:
105
116
                body, encoding = self.string_with_encoding(self._body)
106
117
                msgobj.set_payload(body, encoding)
107
118
        else:
108
 
            msgobj = MIMEMultipart.MIMEMultipart()
 
119
            msgobj = MIMEMultipart()
109
120
 
110
121
            if boundary is not None:
111
122
                msgobj.set_boundary(boundary)
112
123
 
113
124
            for body, filename, mime_subtype in self._parts:
114
125
                body, encoding = self.string_with_encoding(body)
115
 
                payload = MIMEText.MIMEText(body, mime_subtype, encoding)
 
126
                payload = MIMEText(body, mime_subtype, encoding)
116
127
 
117
128
                if filename is not None:
118
129
                    content_type = payload['Content-Type']
147
158
 
148
159
    @staticmethod
149
160
    def send(config, from_address, to_address, subject, body, attachment=None,
150
 
            attachment_filename=None, attachment_mime_subtype='plain'):
 
161
             attachment_filename=None, attachment_mime_subtype='plain'):
151
162
        """Create an email message and send it with SMTPConnection.
152
163
 
153
164
        :param config: config object to pass to SMTPConnection constructor.
170
181
        """
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
 
        user, email = Utils.parseaddr(address)
 
184
        user, email = parseaddr(address)
174
185
        if not user:
175
186
            return email
176
187
        else:
177
 
            return Utils.formataddr((str(Header.Header(safe_unicode(user))),
 
188
            return formataddr((str(Header(safe_unicode(user))),
178
189
                email))
179
190
 
180
191
    @staticmethod
181
192
    def string_with_encoding(string_):
182
193
        """Return a str object together with an encoding.
183
194
 
184
 
        :param string_: A str or unicode object.
 
195
        :param string\\_: A str or unicode object.
185
196
        :return: A tuple (str, encoding), where encoding is one of 'ascii',
186
197
            'utf-8', or '8-bit', in that preferred order.
187
198
        """