/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/smtp_connection.py

  • Committer: Martin Pool
  • Date: 2007-09-14 06:31:28 UTC
  • mfrom: (2822 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2823.
  • Revision ID: mbp@sourcefrog.net-20070914063128-0p7mh6zfb4pzdg9p
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
"""A convenience class around smtplib."""
18
18
 
19
19
from email import Utils
 
20
import errno
20
21
import smtplib
 
22
import socket
21
23
 
22
24
from bzrlib import ui
23
 
from bzrlib.errors import NoDestinationAddress, SMTPError
 
25
from bzrlib.errors import (
 
26
    NoDestinationAddress,
 
27
    SMTPError,
 
28
    DefaultSMTPConnectionRefused,
 
29
    SMTPConnectionRefused,
 
30
    )
24
31
 
25
32
 
26
33
class SMTPConnection(object):
33
40
 
34
41
    _default_smtp_server = 'localhost'
35
42
 
36
 
    def __init__(self, config):
 
43
    def __init__(self, config, _smtp_factory=None):
 
44
        self._smtp_factory = _smtp_factory
 
45
        if self._smtp_factory is None:
 
46
            self._smtp_factory = smtplib.SMTP
37
47
        self._config = config
38
 
        self._smtp_server = config.get_user_option('smtp_server')
 
48
        self._config_smtp_server = config.get_user_option('smtp_server')
 
49
        self._smtp_server = self._config_smtp_server
39
50
        if self._smtp_server is None:
40
51
            self._smtp_server = self._default_smtp_server
41
52
 
54
65
 
55
66
    def _create_connection(self):
56
67
        """Create an SMTP connection."""
57
 
        self._connection = smtplib.SMTP()
58
 
        self._connection.connect(self._smtp_server)
 
68
        self._connection = self._smtp_factory()
 
69
        try:
 
70
            self._connection.connect(self._smtp_server)
 
71
        except socket.error, e:
 
72
            if e.args[0] == errno.ECONNREFUSED:
 
73
                if self._config_smtp_server is None:
 
74
                    raise DefaultSMTPConnectionRefused(socket.error,
 
75
                                                       self._smtp_server)
 
76
                else:
 
77
                    raise SMTPConnectionRefused(socket.error,
 
78
                                                self._smtp_server)
 
79
            else:
 
80
                raise
59
81
 
60
82
        # If this fails, it just returns an error, but it shouldn't raise an
61
83
        # exception unless something goes really wrong (in which case we want
79
101
    def get_message_addresses(message):
80
102
        """Get the origin and destination addresses of a message.
81
103
 
82
 
        :param message: An email.Message or email.MIMEMultipart object.
 
104
        :param message: A message object supporting get() to access its
 
105
            headers, like email.Message or bzrlib.email_message.EmailMessage.
83
106
        :return: A pair (from_email, to_emails), where from_email is the email
84
107
            address in the From header, and to_emails a list of all the
85
108
            addresses in the To, Cc, and Bcc headers.
86
109
        """
87
 
        from_email = Utils.parseaddr(message['From'])[1]
 
110
        from_email = Utils.parseaddr(message.get('From', None))[1]
88
111
        to_full_addresses = []
89
112
        for header in ['To', 'Cc', 'Bcc']:
90
 
            to_full_addresses += message.get_all(header, [])
 
113
            value = message.get(header, None)
 
114
            if value:
 
115
                to_full_addresses.append(value)
91
116
        to_emails = [ pair[1] for pair in
92
117
                Utils.getaddresses(to_full_addresses) ]
93
118