Mercurial > kallithea
comparison rhodecode/lib/rcmail/smtp_mailer.py @ 2031:82a88013a3fd
merge 1.3 into stable
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Sun, 26 Feb 2012 17:25:09 +0200 |
parents | rhodecode/lib/smtp_mailer.py@95c3e33ef32e rhodecode/lib/smtp_mailer.py@349a0ca30a75 |
children | 63e58ef80ef1 |
comparison
equal
deleted
inserted
replaced
2005:ab0e122b38a7 | 2031:82a88013a3fd |
---|---|
1 # -*- coding: utf-8 -*- | |
2 """ | |
3 rhodecode.lib.rcmail.smtp_mailer | |
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
5 | |
6 Simple smtp mailer used in RhodeCode | |
7 | |
8 :created_on: Sep 13, 2010 | |
9 :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com> | |
10 :license: GPLv3, see COPYING for more details. | |
11 """ | |
12 # This program is free software: you can redistribute it and/or modify | |
13 # it under the terms of the GNU General Public License as published by | |
14 # the Free Software Foundation, either version 3 of the License, or | |
15 # (at your option) any later version. | |
16 # | |
17 # This program is distributed in the hope that it will be useful, | |
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 # GNU General Public License for more details. | |
21 # | |
22 # You should have received a copy of the GNU General Public License | |
23 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
24 | |
25 import logging | |
26 import smtplib | |
27 from socket import sslerror | |
28 from rhodecode.lib.rcmail.message import Message | |
29 | |
30 | |
31 class SmtpMailer(object): | |
32 """SMTP mailer class | |
33 | |
34 mailer = SmtpMailer(mail_from, user, passwd, mail_server, smtp_auth | |
35 mail_port, ssl, tls) | |
36 mailer.send(recipients, subject, body, attachment_files) | |
37 | |
38 :param recipients might be a list of string or single string | |
39 :param attachment_files is a dict of {filename:location} | |
40 it tries to guess the mimetype and attach the file | |
41 | |
42 """ | |
43 | |
44 def __init__(self, mail_from, user, passwd, mail_server, smtp_auth=None, | |
45 mail_port=None, ssl=False, tls=False, debug=False): | |
46 | |
47 self.mail_from = mail_from | |
48 self.mail_server = mail_server | |
49 self.mail_port = mail_port | |
50 self.user = user | |
51 self.passwd = passwd | |
52 self.ssl = ssl | |
53 self.tls = tls | |
54 self.debug = debug | |
55 self.auth = smtp_auth | |
56 | |
57 def send(self, recipients=[], subject='', body='', html='', | |
58 attachment_files=None): | |
59 | |
60 if isinstance(recipients, basestring): | |
61 recipients = [recipients] | |
62 msg = Message(subject, recipients, body, html, self.mail_from, | |
63 recipients_separator=", ") | |
64 raw_msg = msg.to_message() | |
65 | |
66 if self.ssl: | |
67 smtp_serv = smtplib.SMTP_SSL(self.mail_server, self.mail_port) | |
68 else: | |
69 smtp_serv = smtplib.SMTP(self.mail_server, self.mail_port) | |
70 | |
71 if self.tls: | |
72 smtp_serv.ehlo() | |
73 smtp_serv.starttls() | |
74 | |
75 if self.debug: | |
76 smtp_serv.set_debuglevel(1) | |
77 | |
78 smtp_serv.ehlo() | |
79 if self.auth: | |
80 smtp_serv.esmtp_features["auth"] = self.auth | |
81 | |
82 # if server requires authorization you must provide login and password | |
83 # but only if we have them | |
84 if self.user and self.passwd: | |
85 smtp_serv.login(self.user, self.passwd) | |
86 | |
87 smtp_serv.sendmail(msg.sender, msg.send_to, raw_msg.as_string()) | |
88 logging.info('MAIL SEND TO: %s' % recipients) | |
89 | |
90 try: | |
91 smtp_serv.quit() | |
92 except sslerror: | |
93 # sslerror is raised in tls connections on closing sometimes | |
94 pass |