Added an email debug mode which, by default, is enabled

This commit is contained in:
Christopher Allan Webber 2011-05-07 22:45:06 -05:00
parent 3eae207c54
commit 29f3fb7052
4 changed files with 23 additions and 6 deletions

View File

@ -15,6 +15,8 @@ publicstore_base_dir = %(here)s/user_dev/media/public
publicstore_base_url = /mgoblin_media/ publicstore_base_url = /mgoblin_media/
direct_remote_path = /mgoblin_static/ direct_remote_path = /mgoblin_static/
email_sender_address = "notice@mediagoblin.example.org" email_sender_address = "notice@mediagoblin.example.org"
# set to false to enable sending notices
email_debug_mode = true
## Uncomment this to put some user-overriding templates here ## Uncomment this to put some user-overriding templates here
#local_templates = %(here)s/user_dev/templates/ #local_templates = %(here)s/user_dev/templates/

View File

@ -18,6 +18,7 @@ import urllib
import routes import routes
import mongokit import mongokit
from paste.deploy.converters import asbool
from webob import Request, exc from webob import Request, exc
from mediagoblin import routing, util, models, storage, staticdirect from mediagoblin import routing, util, models, storage, staticdirect
@ -36,7 +37,7 @@ class MediaGoblinApp(object):
def __init__(self, connection, database_path, def __init__(self, connection, database_path,
public_store, queue_store, public_store, queue_store,
staticdirector, staticdirector,
email_sender_address, email_sender_address, email_debug_mode,
user_template_path=None): user_template_path=None):
# Get the template environment # Get the template environment
self.template_env = util.get_jinja_env(user_template_path) self.template_env = util.get_jinja_env(user_template_path)
@ -61,6 +62,7 @@ class MediaGoblinApp(object):
# object. # object.
setup_globals( setup_globals(
email_sender_address=email_sender_address, email_sender_address=email_sender_address,
email_debug_mode=email_debug_mode,
db_connection=connection, db_connection=connection,
database=self.db, database=self.db,
public_store=self.public_store, public_store=self.public_store,
@ -141,8 +143,9 @@ def paste_app_factory(global_config, **app_config):
connection, app_config.get('db_name', 'mediagoblin'), connection, app_config.get('db_name', 'mediagoblin'),
public_store=public_store, queue_store=queue_store, public_store=public_store, queue_store=queue_store,
staticdirector=staticdirector, staticdirector=staticdirector,
email_sender_address=app_config.get('email_sender_address', email_sender_address=app_config.get(
'notice@mediagoblin.example.org'), 'email_sender_address', 'notice@mediagoblin.example.org'),
email_debug_mode=app_config.get('email_debug_mode'),
user_template_path=app_config.get('local_templates')) user_template_path=app_config.get('local_templates'))
return mgoblin_app return mgoblin_app

View File

@ -82,6 +82,7 @@ def setup_self(setup_globals_func=setup_globals):
db_connection=connection, db_connection=connection,
database=db, database=db,
public_store=public_store, public_store=public_store,
email_debug_mode=app_config.get('email_debug_mode'),
email_sender_address=mgoblin_section.get( email_sender_address=mgoblin_section.get(
'email_sender_address', 'email_sender_address',
'notice@mediagoblin.example.org'), 'notice@mediagoblin.example.org'),

View File

@ -21,6 +21,8 @@ import sys
import jinja2 import jinja2
import mongokit import mongokit
from mediagoblin import globals as mgoblin_globals
TESTS_ENABLED = False TESTS_ENABLED = False
def _activate_testing(): def _activate_testing():
@ -153,9 +155,9 @@ def send_email(from_addr, to_addrs, subject, message_body):
- message_body: email body text - message_body: email body text
""" """
# TODO: make a mock mhost if testing is enabled # TODO: make a mock mhost if testing is enabled
if TESTS_ENABLED: if TESTS_ENABLED or mgoblin_globals.email_debug_mode:
mhost = FakeMhost() mhost = FakeMhost()
else: elif not mgoblin_globals.email_debug_mode:
mhost = smtplib.SMTP() mhost = smtplib.SMTP()
mhost.connect() mhost.connect()
@ -168,4 +170,13 @@ def send_email(from_addr, to_addrs, subject, message_body):
if TESTS_ENABLED: if TESTS_ENABLED:
EMAIL_TEST_INBOX.append(message) EMAIL_TEST_INBOX.append(message)
return mhost.sendmail(from_addr, to_addrs, message.as_string()) elif mgoblin_globals.email_debug_mode:
print u"===== Email ====="
print u"From address: %s" % message['From']
print u"To addresses: %s" % message['To']
print u"Subject: %s" % message['Subject']
print u"-- Body: --"
print message.get_payload(decode=True)
else:
return mhost.sendmail(from_addr, to_addrs, message.as_string())