Merge branch 'mediagoblin-upstream' into bug444_fix_utils_py_redux
[mediagoblin.git] / mediagoblin / tools / mail.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 MediaGoblin contributors. See AUTHORS.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 import smtplib
18 from email.MIMEText import MIMEText
19 from mediagoblin import mg_globals
20 from mediagoblin.tools import common
21
22 ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 ### Special email test stuff begins HERE
24 ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25
26 # We have two "test inboxes" here:
27 #
28 # EMAIL_TEST_INBOX:
29 # ----------------
30 # If you're writing test views, you'll probably want to check this.
31 # It contains a list of MIMEText messages.
32 #
33 # EMAIL_TEST_MBOX_INBOX:
34 # ----------------------
35 # This collects the messages from the FakeMhost inbox. It's reslly
36 # just here for testing the send_email method itself.
37 #
38 # Anyway this contains:
39 # - from
40 # - to: a list of email recipient addresses
41 # - message: not just the body, but the whole message, including
42 # headers, etc.
43 #
44 # ***IMPORTANT!***
45 # ----------------
46 # Before running tests that call functions which send email, you should
47 # always call _clear_test_inboxes() to "wipe" the inboxes clean.
48
49 EMAIL_TEST_INBOX = []
50 EMAIL_TEST_MBOX_INBOX = []
51
52 class FakeMhost(object):
53 """
54 Just a fake mail host so we can capture and test messages
55 from send_email
56 """
57 def login(self, *args, **kwargs):
58 pass
59
60 def sendmail(self, from_addr, to_addrs, message):
61 EMAIL_TEST_MBOX_INBOX.append(
62 {'from': from_addr,
63 'to': to_addrs,
64 'message': message})
65
66 def _clear_test_inboxes():
67 global EMAIL_TEST_INBOX
68 global EMAIL_TEST_MBOX_INBOX
69 EMAIL_TEST_INBOX = []
70 EMAIL_TEST_MBOX_INBOX = []
71
72 ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
73 ### </Special email test stuff>
74 ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
75
76 def send_email(from_addr, to_addrs, subject, message_body):
77 """
78 Simple email sending wrapper, use this so we can capture messages
79 for unit testing purposes.
80
81 Args:
82 - from_addr: address you're sending the email from
83 - to_addrs: list of recipient email addresses
84 - subject: subject of the email
85 - message_body: email body text
86 """
87 if common.TESTS_ENABLED or mg_globals.app_config['email_debug_mode']:
88 mhost = FakeMhost()
89 elif not mg_globals.app_config['email_debug_mode']:
90 mhost = smtplib.SMTP(
91 mg_globals.app_config['email_smtp_host'],
92 mg_globals.app_config['email_smtp_port'])
93
94 # SMTP.__init__ Issues SMTP.connect implicitly if host
95 if not mg_globals.app_config['email_smtp_host']: # e.g. host = ''
96 mhost.connect() # We SMTP.connect explicitly
97
98 if mg_globals.app_config['email_smtp_user'] \
99 or mg_globals.app_config['email_smtp_pass']:
100 mhost.login(
101 mg_globals.app_config['email_smtp_user'],
102 mg_globals.app_config['email_smtp_pass'])
103
104 message = MIMEText(message_body.encode('utf-8'), 'plain', 'utf-8')
105 message['Subject'] = subject
106 message['From'] = from_addr
107 message['To'] = ', '.join(to_addrs)
108
109 if common.TESTS_ENABLED:
110 EMAIL_TEST_INBOX.append(message)
111
112 if mg_globals.app_config['email_debug_mode']:
113 print u"===== Email ====="
114 print u"From address: %s" % message['From']
115 print u"To addresses: %s" % message['To']
116 print u"Subject: %s" % message['Subject']
117 print u"-- Body: --"
118 print message.get_payload(decode=True)
119
120 return mhost.sendmail(from_addr, to_addrs, message.as_string())