~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smtp_connection.py

  • Committer: John Arbash Meinel
  • Author(s): Mark Hammond
  • Date: 2008-09-09 17:02:21 UTC
  • mto: This revision was merged to the branch mainline in revision 3697.
  • Revision ID: john@arbash-meinel.com-20080909170221-svim3jw2mrz0amp3
An updated transparent icon for bzr.

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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""A convenience class around smtplib."""
18
18
 
19
 
from __future__ import absolute_import
20
 
 
21
19
from email import Utils
22
20
import errno
23
21
import smtplib
25
23
 
26
24
from bzrlib import (
27
25
    config,
28
 
    osutils,
 
26
    ui,
29
27
    )
30
28
from bzrlib.errors import (
31
29
    NoDestinationAddress,
35
33
    )
36
34
 
37
35
 
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
 
 
52
36
class SMTPConnection(object):
53
37
    """Connect to an SMTP server and send an email.
54
38
 
64
48
        if self._smtp_factory is None:
65
49
            self._smtp_factory = smtplib.SMTP
66
50
        self._config = config
67
 
        self._config_smtp_server = config.get('smtp_server')
 
51
        self._config_smtp_server = config.get_user_option('smtp_server')
68
52
        self._smtp_server = self._config_smtp_server
69
53
        if self._smtp_server is None:
70
54
            self._smtp_server = self._default_smtp_server
71
55
 
72
 
        self._smtp_username = config.get('smtp_username')
73
 
        self._smtp_password = config.get('smtp_password')
 
56
        self._smtp_username = config.get_user_option('smtp_username')
 
57
        self._smtp_password = config.get_user_option('smtp_password')
74
58
 
75
59
        self._connection = None
76
60
 
80
64
            return
81
65
 
82
66
        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
86
67
        self._authenticate()
87
68
 
88
69
    def _create_connection(self):
122
103
        """If necessary authenticate yourself to the server."""
123
104
        auth = config.AuthenticationConfig()
124
105
        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.
128
106
            self._smtp_username = auth.get_user('smtp', self._smtp_server)
129
107
            if self._smtp_username is None:
130
108
                return
133
111
            self._smtp_password = auth.get_password(
134
112
                'smtp', self._smtp_server, self._smtp_username)
135
113
 
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)
 
114
        self._connection.login(self._smtp_username, self._smtp_password)
144
115
 
145
116
    @staticmethod
146
117
    def get_message_addresses(message):