~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smtp_connection.py

  • Committer: Patch Queue Manager
  • Date: 2016-04-21 04:10:52 UTC
  • mfrom: (6616.1.1 fix-en-user-guide)
  • Revision ID: pqm@pqm.ubuntu.com-20160421041052-clcye7ns1qcl2n7w
(richard-wilbur) Ensure build of English use guide always uses English text
 even when user's locale specifies a different language. (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""A convenience class around smtplib."""
18
18
 
 
19
from __future__ import absolute_import
 
20
 
19
21
from email import Utils
20
22
import errno
21
23
import smtplib
23
25
 
24
26
from bzrlib import (
25
27
    config,
26
 
    ui,
 
28
    osutils,
27
29
    )
28
30
from bzrlib.errors import (
29
31
    NoDestinationAddress,
33
35
    )
34
36
 
35
37
 
 
38
smtp_password = config.Option('smtp_password', default=None,
 
39
        help='''\
 
40
Password to use for authentication to SMTP server.
 
41
''')
 
42
smtp_server = config.Option('smtp_server', default=None,
 
43
        help='''\
 
44
Hostname of the SMTP server to use for sending email.
 
45
''')
 
46
smtp_username = config.Option('smtp_username', default=None,
 
47
        help='''\
 
48
Username to use for authentication to SMTP server.
 
49
''')
 
50
 
 
51
 
36
52
class SMTPConnection(object):
37
53
    """Connect to an SMTP server and send an email.
38
54
 
48
64
        if self._smtp_factory is None:
49
65
            self._smtp_factory = smtplib.SMTP
50
66
        self._config = config
51
 
        self._config_smtp_server = config.get_user_option('smtp_server')
 
67
        self._config_smtp_server = config.get('smtp_server')
52
68
        self._smtp_server = self._config_smtp_server
53
69
        if self._smtp_server is None:
54
70
            self._smtp_server = self._default_smtp_server
55
71
 
56
 
        self._smtp_username = config.get_user_option('smtp_username')
57
 
        self._smtp_password = config.get_user_option('smtp_password')
 
72
        self._smtp_username = config.get('smtp_username')
 
73
        self._smtp_password = config.get('smtp_password')
58
74
 
59
75
        self._connection = None
60
76
 
64
80
            return
65
81
 
66
82
        self._create_connection()
 
83
        # FIXME: _authenticate() should only be called when the server has
 
84
        # refused unauthenticated access, so it can safely try to authenticate 
 
85
        # with the default username. JRV20090407
67
86
        self._authenticate()
68
87
 
69
88
    def _create_connection(self):
103
122
        """If necessary authenticate yourself to the server."""
104
123
        auth = config.AuthenticationConfig()
105
124
        if self._smtp_username is None:
 
125
            # FIXME: Since _authenticate gets called even when no authentication
 
126
            # is necessary, it's not possible to use the default username 
 
127
            # here yet.
106
128
            self._smtp_username = auth.get_user('smtp', self._smtp_server)
107
129
            if self._smtp_username is None:
108
130
                return
111
133
            self._smtp_password = auth.get_password(
112
134
                'smtp', self._smtp_server, self._smtp_username)
113
135
 
114
 
        self._connection.login(self._smtp_username, self._smtp_password)
 
136
        # smtplib requires that the username and password be byte
 
137
        # strings.  The CRAM-MD5 spec doesn't give any guidance on
 
138
        # encodings, but the SASL PLAIN spec says UTF-8, so that's
 
139
        # what we'll use.
 
140
        username = osutils.safe_utf8(self._smtp_username)
 
141
        password = osutils.safe_utf8(self._smtp_password)
 
142
 
 
143
        self._connection.login(username, password)
115
144
 
116
145
    @staticmethod
117
146
    def get_message_addresses(message):