1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
18 from email
.MIMEText
import MIMEText
19 from mediagoblin
import mg_globals
, messages
20 from mediagoblin
.tools
import common
22 ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 ### Special email test stuff begins HERE
24 ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 # We have two "test inboxes" here:
30 # If you're writing test views, you'll probably want to check this.
31 # It contains a list of MIMEText messages.
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.
38 # Anyway this contains:
40 # - to: a list of email recipient addresses
41 # - message: not just the body, but the whole message, including
46 # Before running tests that call functions which send email, you should
47 # always call _clear_test_inboxes() to "wipe" the inboxes clean.
50 EMAIL_TEST_MBOX_INBOX
= []
53 class FakeMhost(object):
55 Just a fake mail host so we can capture and test messages
58 def login(self
, *args
, **kwargs
):
61 def sendmail(self
, from_addr
, to_addrs
, message
):
62 EMAIL_TEST_MBOX_INBOX
.append(
68 def _clear_test_inboxes():
69 global EMAIL_TEST_INBOX
70 global EMAIL_TEST_MBOX_INBOX
72 EMAIL_TEST_MBOX_INBOX
= []
75 ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
76 ### </Special email test stuff>
77 ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
79 def send_email(from_addr
, to_addrs
, subject
, message_body
):
81 Simple email sending wrapper, use this so we can capture messages
82 for unit testing purposes.
85 - from_addr: address you're sending the email from
86 - to_addrs: list of recipient email addresses
87 - subject: subject of the email
88 - message_body: email body text
90 if common
.TESTS_ENABLED
or mg_globals
.app_config
['email_debug_mode']:
92 elif not mg_globals
.app_config
['email_debug_mode']:
93 if mg_globals
.app_config
['email_smtp_use_ssl']:
94 smtp_init
= smtplib
.SMTP_SSL
96 smtp_init
= smtplib
.SMTP
99 mg_globals
.app_config
['email_smtp_host'],
100 mg_globals
.app_config
['email_smtp_port'])
102 # SMTP.__init__ Issues SMTP.connect implicitly if host
103 if not mg_globals
.app_config
['email_smtp_host']: # e.g. host = ''
104 mhost
.connect() # We SMTP.connect explicitly
106 if ((not common
.TESTS_ENABLED
)
107 and (mg_globals
.app_config
['email_smtp_user']
108 or mg_globals
.app_config
['email_smtp_pass'])):
110 mg_globals
.app_config
['email_smtp_user'],
111 mg_globals
.app_config
['email_smtp_pass'])
113 message
= MIMEText(message_body
.encode('utf-8'), 'plain', 'utf-8')
114 message
['Subject'] = subject
115 message
['From'] = from_addr
116 message
['To'] = ', '.join(to_addrs
)
118 if common
.TESTS_ENABLED
:
119 EMAIL_TEST_INBOX
.append(message
)
121 elif mg_globals
.app_config
['email_debug_mode']:
122 print u
"===== Email ====="
123 print u
"From address: %s" % message
['From']
124 print u
"To addresses: %s" % message
['To']
125 print u
"Subject: %s" % message
['Subject']
127 print message
.get_payload(decode
=True)
129 return mhost
.sendmail(from_addr
, to_addrs
, message
.as_string())
132 def normalize_email(email
):
133 """return case sensitive part, lower case domain name
135 :returns: None in case of broken email addresses"""
137 em_user
, em_dom
= email
.split('@', 1)
139 # email contained no '@'
141 email
= "@".join((em_user
, em_dom
.lower()))
145 def email_debug_message(request
):
147 If the server is running in email debug mode (which is
148 the current default), give a debug message to the user
149 so that they have an idea where to find their email.
151 if mg_globals
.app_config
['email_debug_mode']:
152 # DEBUG message, no need to translate
153 messages
.add_message(request
, messages
.DEBUG
,
154 u
"This instance is running in email debug mode. "
155 u
"The email will be on the console of the server process.")