/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 bzrlib/email_message.py

  • Committer: John Ferlito
  • Date: 2009-09-02 04:31:45 UTC
  • mto: (4665.7.1 serve-init)
  • mto: This revision was merged to the branch mainline in revision 4913.
  • Revision ID: johnf@inodes.org-20090902043145-gxdsfw03ilcwbyn5
Add a debian init script for bzr --serve

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
24
 
from . import __version__ as _breezy_version
25
 
from .errors import BzrBadParameterNotUnicode
26
 
from .osutils import safe_unicode
27
 
from .smtp_connection import SMTPConnection
 
19
from email import (
 
20
    Header,
 
21
    Message,
 
22
    MIMEMultipart,
 
23
    MIMEText,
 
24
    Utils,
 
25
    )
 
26
 
 
27
from bzrlib import __version__ as _bzrlib_version
 
28
from bzrlib.osutils import safe_unicode
 
29
from bzrlib.smtp_connection import SMTPConnection
28
30
 
29
31
 
30
32
class EmailMessage(object):
56
58
        self._body = body
57
59
        self._parts = []
58
60
 
59
 
        if isinstance(to_address, (bytes, str)):
60
 
            to_address = [to_address]
 
61
        if isinstance(to_address, basestring):
 
62
            to_address = [ to_address ]
61
63
 
62
64
        to_addresses = []
63
65
 
66
68
 
67
69
        self._headers['To'] = ', '.join(to_addresses)
68
70
        self._headers['From'] = self.address_to_encoded_header(from_address)
69
 
        self._headers['Subject'] = Header(safe_unicode(subject))
70
 
        self._headers['User-Agent'] = 'Bazaar (%s)' % _breezy_version
 
71
        self._headers['Subject'] = Header.Header(safe_unicode(subject))
 
72
        self._headers['User-Agent'] = 'Bazaar (%s)' % _bzrlib_version
71
73
 
72
74
    def add_inline_attachment(self, body, filename=None, mime_subtype='plain'):
73
75
        """Add an inline attachment to the message.
98
100
            Used for tests.
99
101
        """
100
102
        if not self._parts:
101
 
            msgobj = Message()
 
103
            msgobj = Message.Message()
102
104
            if self._body is not None:
103
105
                body, encoding = self.string_with_encoding(self._body)
104
106
                msgobj.set_payload(body, encoding)
105
107
        else:
106
 
            msgobj = MIMEMultipart()
 
108
            msgobj = MIMEMultipart.MIMEMultipart()
107
109
 
108
110
            if boundary is not None:
109
111
                msgobj.set_boundary(boundary)
110
112
 
111
113
            for body, filename, mime_subtype in self._parts:
112
114
                body, encoding = self.string_with_encoding(body)
113
 
                payload = MIMEText(body, mime_subtype, encoding)
 
115
                payload = MIMEText.MIMEText(body, mime_subtype, encoding)
114
116
 
115
117
                if filename is not None:
116
118
                    content_type = payload['Content-Type']
145
147
 
146
148
    @staticmethod
147
149
    def send(config, from_address, to_address, subject, body, attachment=None,
148
 
             attachment_filename=None, attachment_mime_subtype='plain'):
 
150
            attachment_filename=None, attachment_mime_subtype='plain'):
149
151
        """Create an email message and send it with SMTPConnection.
150
152
 
151
153
        :param config: config object to pass to SMTPConnection constructor.
156
158
        msg = EmailMessage(from_address, to_address, subject, body)
157
159
        if attachment is not None:
158
160
            msg.add_inline_attachment(attachment, attachment_filename,
159
 
                                      attachment_mime_subtype)
 
161
                    attachment_mime_subtype)
160
162
        SMTPConnection(config).send_email(msg)
161
163
 
162
164
    @staticmethod
166
168
        :param address: An unicode string, or UTF-8 byte string.
167
169
        :return: A possibly RFC2047-encoded string.
168
170
        """
169
 
        if not isinstance(address, str):
170
 
            raise BzrBadParameterNotUnicode(address)
171
171
        # Can't call Header over all the address, because that encodes both the
172
172
        # name and the email address, which is not permitted by RFCs.
173
 
        user, email = parseaddr(address)
 
173
        user, email = Utils.parseaddr(address)
174
174
        if not user:
175
175
            return email
176
176
        else:
177
 
            return formataddr((str(Header(safe_unicode(user))),
178
 
                               email))
 
177
            return Utils.formataddr((str(Header.Header(safe_unicode(user))),
 
178
                email))
179
179
 
180
180
    @staticmethod
181
181
    def string_with_encoding(string_):
182
182
        """Return a str object together with an encoding.
183
183
 
184
 
        :param string\\_: A str or unicode object.
 
184
        :param string_: A str or unicode object.
185
185
        :return: A tuple (str, encoding), where encoding is one of 'ascii',
186
186
            'utf-8', or '8-bit', in that preferred order.
187
187
        """
190
190
        # avoid base64 when it's not necessary in order to be most compatible
191
191
        # with the capabilities of the receiving side, we check with encode()
192
192
        # and decode() whether the body is actually ascii-only.
193
 
        if isinstance(string_, str):
 
193
        if isinstance(string_, unicode):
194
194
            try:
195
195
                return (string_.encode('ascii'), 'ascii')
196
196
            except UnicodeEncodeError: