annotate kallithea/lib/rcmail/smtp_mailer.py @ 5555:32cdc6f70f13

cleanup: stop using mutable default params
author Jiří Suchan <yed@vanyli.net>
date Wed, 14 Oct 2015 20:08:18 +0100
parents 0529498575ce
children 3009a239199d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
981
c45c910714e4 fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents: 952
diff changeset
1 # -*- coding: utf-8 -*-
1532
2afe9320d5e6 updated docstrings
Marcin Kuzminski <marcin@python-works.com>
parents: 1417
diff changeset
2 # This program is free software: you can redistribute it and/or modify
2afe9320d5e6 updated docstrings
Marcin Kuzminski <marcin@python-works.com>
parents: 1417
diff changeset
3 # it under the terms of the GNU General Public License as published by
2afe9320d5e6 updated docstrings
Marcin Kuzminski <marcin@python-works.com>
parents: 1417
diff changeset
4 # the Free Software Foundation, either version 3 of the License, or
2afe9320d5e6 updated docstrings
Marcin Kuzminski <marcin@python-works.com>
parents: 1417
diff changeset
5 # (at your option) any later version.
2afe9320d5e6 updated docstrings
Marcin Kuzminski <marcin@python-works.com>
parents: 1417
diff changeset
6 #
2afe9320d5e6 updated docstrings
Marcin Kuzminski <marcin@python-works.com>
parents: 1417
diff changeset
7 # This program is distributed in the hope that it will be useful,
2afe9320d5e6 updated docstrings
Marcin Kuzminski <marcin@python-works.com>
parents: 1417
diff changeset
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
2afe9320d5e6 updated docstrings
Marcin Kuzminski <marcin@python-works.com>
parents: 1417
diff changeset
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2afe9320d5e6 updated docstrings
Marcin Kuzminski <marcin@python-works.com>
parents: 1417
diff changeset
10 # GNU General Public License for more details.
2afe9320d5e6 updated docstrings
Marcin Kuzminski <marcin@python-works.com>
parents: 1417
diff changeset
11 #
2afe9320d5e6 updated docstrings
Marcin Kuzminski <marcin@python-works.com>
parents: 1417
diff changeset
12 # You should have received a copy of the GNU General Public License
2afe9320d5e6 updated docstrings
Marcin Kuzminski <marcin@python-works.com>
parents: 1417
diff changeset
13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 2776
diff changeset
14 """
4186
7e5f8c12a3fc First step in two-part process to rename directories to kallithea.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4116
diff changeset
15 kallithea.lib.rcmail.smtp_mailer
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 2776
diff changeset
16 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 2776
diff changeset
17
4212
24c0d584ba86 General renaming to Kallithea
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4211
diff changeset
18 Simple smtp mailer used in Kallithea
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 2776
diff changeset
19
4211
1948ede028ef RhodeCode GmbH is not the sole author of this work
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4208
diff changeset
20 This file was forked by the Kallithea project in July 2014.
1948ede028ef RhodeCode GmbH is not the sole author of this work
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4208
diff changeset
21 Original author and date, and relevant copyright and licensing information is below:
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 2776
diff changeset
22 :created_on: Sep 13, 2010
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 2776
diff changeset
23 :author: marcink
4211
1948ede028ef RhodeCode GmbH is not the sole author of this work
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4208
diff changeset
24 :copyright: (c) 2013 RhodeCode GmbH, and others.
4208
ad38f9f93b3b Correct licensing information in individual files.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4187
diff changeset
25 :license: GPLv3, see LICENSE.md for more details.
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 2776
diff changeset
26 """
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 2776
diff changeset
27
2452
d95ef6587bca fixes #481 rhodecode emails are sent without Date header
Marcin Kuzminski <marcin@python-works.com>
parents: 1846
diff changeset
28 import time
474
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
29 import logging
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
30 import smtplib
981
c45c910714e4 fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents: 952
diff changeset
31 from socket import sslerror
2452
d95ef6587bca fixes #481 rhodecode emails are sent without Date header
Marcin Kuzminski <marcin@python-works.com>
parents: 1846
diff changeset
32 from email.utils import formatdate
4186
7e5f8c12a3fc First step in two-part process to rename directories to kallithea.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4116
diff changeset
33 from kallithea.lib.rcmail.message import Message
7e5f8c12a3fc First step in two-part process to rename directories to kallithea.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4116
diff changeset
34 from kallithea.lib.rcmail.utils import DNS_NAME
1307
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
35
1824
89efedac4e6c 2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents: 1717
diff changeset
36
474
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
37 class SmtpMailer(object):
981
c45c910714e4 fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents: 952
diff changeset
38 """SMTP mailer class
1203
6832ef664673 source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents: 1169
diff changeset
39
1583
8e77c75bd65a made auth type optional in constructor
Marcin Kuzminski <marcin@python-works.com>
parents: 1581
diff changeset
40 mailer = SmtpMailer(mail_from, user, passwd, mail_server, smtp_auth
1307
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
41 mail_port, ssl, tls)
1203
6832ef664673 source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents: 1169
diff changeset
42 mailer.send(recipients, subject, body, attachment_files)
6832ef664673 source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents: 1169
diff changeset
43
474
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
44 :param recipients might be a list of string or single string
1203
6832ef664673 source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents: 1169
diff changeset
45 :param attachment_files is a dict of {filename:location}
6832ef664673 source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents: 1169
diff changeset
46 it tries to guess the mimetype and attach the file
6832ef664673 source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents: 1169
diff changeset
47
474
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
48 """
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
49
1583
8e77c75bd65a made auth type optional in constructor
Marcin Kuzminski <marcin@python-works.com>
parents: 1581
diff changeset
50 def __init__(self, mail_from, user, passwd, mail_server, smtp_auth=None,
8e77c75bd65a made auth type optional in constructor
Marcin Kuzminski <marcin@python-works.com>
parents: 1581
diff changeset
51 mail_port=None, ssl=False, tls=False, debug=False):
689
ecc566f8b69f fixes #59, notifications for user registrations + some changes to mailer
Marcin Kuzminski <marcin@python-works.com>
parents: 604
diff changeset
52
474
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
53 self.mail_from = mail_from
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
54 self.mail_server = mail_server
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
55 self.mail_port = mail_port
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
56 self.user = user
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
57 self.passwd = passwd
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
58 self.ssl = ssl
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
59 self.tls = tls
1169
f6dca275c5a8 control mailer debug with the .ini file
Marcin Kuzminski <marcin@python-works.com>
parents: 1029
diff changeset
60 self.debug = debug
1581
67377fd685be applied smth_auth options update patch
Les Peabody <lpeabody@ccom.unh.edu>
parents: 1532
diff changeset
61 self.auth = smtp_auth
689
ecc566f8b69f fixes #59, notifications for user registrations + some changes to mailer
Marcin Kuzminski <marcin@python-works.com>
parents: 604
diff changeset
62
5555
32cdc6f70f13 cleanup: stop using mutable default params
Jiří Suchan <yed@vanyli.net>
parents: 4384
diff changeset
63 def send(self, recipients=None, subject='', body='', html='',
4384
0529498575ce notifications: insert 'References' mail headers to help MUA threading
Mads Kiilerich <madski@unity3d.com>
parents: 4331
diff changeset
64 attachment_files=None, headers=None):
5555
32cdc6f70f13 cleanup: stop using mutable default params
Jiří Suchan <yed@vanyli.net>
parents: 4384
diff changeset
65 recipients = recipients or []
474
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
66 if isinstance(recipients, basestring):
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
67 recipients = [recipients]
4384
0529498575ce notifications: insert 'References' mail headers to help MUA threading
Mads Kiilerich <madski@unity3d.com>
parents: 4331
diff changeset
68 if headers is None:
0529498575ce notifications: insert 'References' mail headers to help MUA threading
Mads Kiilerich <madski@unity3d.com>
parents: 4331
diff changeset
69 headers = {}
0529498575ce notifications: insert 'References' mail headers to help MUA threading
Mads Kiilerich <madski@unity3d.com>
parents: 4331
diff changeset
70 headers.setdefault('Date', formatdate(time.time()))
1846
349a0ca30a75 Changed default recipients separator for mails to ', '
Marcin Kuzminski <marcin@python-works.com>
parents: 1824
diff changeset
71 msg = Message(subject, recipients, body, html, self.mail_from,
2452
d95ef6587bca fixes #481 rhodecode emails are sent without Date header
Marcin Kuzminski <marcin@python-works.com>
parents: 1846
diff changeset
72 recipients_separator=", ", extra_headers=headers)
1717
7ff304d3028f Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1716
diff changeset
73 raw_msg = msg.to_message()
7ff304d3028f Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1716
diff changeset
74
474
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
75 if self.ssl:
2495
9766f0baf5c5 Use local_hostname in mailer
Marcin Kuzminski <marcin@python-works.com>
parents: 2452
diff changeset
76 smtp_serv = smtplib.SMTP_SSL(self.mail_server, self.mail_port,
9766f0baf5c5 Use local_hostname in mailer
Marcin Kuzminski <marcin@python-works.com>
parents: 2452
diff changeset
77 local_hostname=DNS_NAME.get_fqdn())
474
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
78 else:
2495
9766f0baf5c5 Use local_hostname in mailer
Marcin Kuzminski <marcin@python-works.com>
parents: 2452
diff changeset
79 smtp_serv = smtplib.SMTP(self.mail_server, self.mail_port,
9766f0baf5c5 Use local_hostname in mailer
Marcin Kuzminski <marcin@python-works.com>
parents: 2452
diff changeset
80 local_hostname=DNS_NAME.get_fqdn())
474
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
81
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
82 if self.tls:
981
c45c910714e4 fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents: 952
diff changeset
83 smtp_serv.ehlo()
474
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
84 smtp_serv.starttls()
689
ecc566f8b69f fixes #59, notifications for user registrations + some changes to mailer
Marcin Kuzminski <marcin@python-works.com>
parents: 604
diff changeset
85
ecc566f8b69f fixes #59, notifications for user registrations + some changes to mailer
Marcin Kuzminski <marcin@python-works.com>
parents: 604
diff changeset
86 if self.debug:
474
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
87 smtp_serv.set_debuglevel(1)
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
88
952
beabd452f08b fixed ehlo command for mailing.
Marcin Kuzminski <marcin@python-works.com>
parents: 689
diff changeset
89 smtp_serv.ehlo()
1581
67377fd685be applied smth_auth options update patch
Les Peabody <lpeabody@ccom.unh.edu>
parents: 1532
diff changeset
90 if self.auth:
67377fd685be applied smth_auth options update patch
Les Peabody <lpeabody@ccom.unh.edu>
parents: 1532
diff changeset
91 smtp_serv.esmtp_features["auth"] = self.auth
474
a3d9d24acbec Implemented password reset(forms/models/ tasks) and mailing tasks.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
92
1583
8e77c75bd65a made auth type optional in constructor
Marcin Kuzminski <marcin@python-works.com>
parents: 1581
diff changeset
93 # if server requires authorization you must provide login and password
8e77c75bd65a made auth type optional in constructor
Marcin Kuzminski <marcin@python-works.com>
parents: 1581
diff changeset
94 # but only if we have them
981
c45c910714e4 fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents: 952
diff changeset
95 if self.user and self.passwd:
c45c910714e4 fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents: 952
diff changeset
96 smtp_serv.login(self.user, self.passwd)
c45c910714e4 fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents: 952
diff changeset
97
1717
7ff304d3028f Notification fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1716
diff changeset
98 smtp_serv.sendmail(msg.sender, msg.send_to, raw_msg.as_string())
4331
2655b2d46055 mail: improve logging - including always passing recipients as list as documented
Mads Kiilerich <madski@unity3d.com>
parents: 4212
diff changeset
99 logging.info('MAIL SENT TO: %s' % recipients)
981
c45c910714e4 fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents: 952
diff changeset
100
c45c910714e4 fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents: 952
diff changeset
101 try:
c45c910714e4 fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents: 952
diff changeset
102 smtp_serv.quit()
c45c910714e4 fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents: 952
diff changeset
103 except sslerror:
c45c910714e4 fixed smtp_mailer with Johan Walles suggestion, and made some patches according to
Marcin Kuzminski <marcin@python-works.com>
parents: 952
diff changeset
104 # sslerror is raised in tls connections on closing sometimes
2495
9766f0baf5c5 Use local_hostname in mailer
Marcin Kuzminski <marcin@python-works.com>
parents: 2452
diff changeset
105 smtp_serv.close()