<s> import os <EOL> import logging <EOL> import re <EOL> import urlparse <EOL> import smtplib <EOL> from email . MIMEImage import MIMEImage <EOL> from django . conf import settings <EOL> from django import template <EOL> from django . template import loader <EOL> from django . core import mail <EOL> from common import exception <EOL> from common import util <EOL> from google . appengine . api import urlfetch <EOL> from google . appengine . api import mail as gae_mail <EOL> def is_allowed_to_send_email_to ( email ) : <EOL> if settings . EMAIL_LIMIT_DOMAIN : <EOL> limit_email = re . compile ( r'<STR_LIT>' % ( settings . EMAIL_LIMIT_DOMAIN ) ) <EOL> return limit_email . match ( email ) <EOL> if settings . EMAIL_TEST_ONLY and email not in settings . EMAIL_TEST_ADDRESSES : <EOL> return False <EOL> return True <EOL> def _greeting_name ( actor_ref ) : <EOL> try : <EOL> return actor_ref . extra [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> return actor_ref . display_nick ( ) <EOL> def _full_name ( actor_ref ) : <EOL> try : <EOL> return "<STR_LIT>" % ( actor_ref . extra [ '<STR_LIT>' ] , <EOL> actor_ref . extra [ '<STR_LIT>' ] ) <EOL> except KeyError : <EOL> return actor_ref . display_nick ( ) <EOL> def log_blocked_send ( on_behalf , email , subject , message = None ) : <EOL> logging . info ( "<STR_LIT>" , <EOL> on_behalf , email , subject ) <EOL> if message : <EOL> logging . info ( "<STR_LIT>" , message ) <EOL> def send ( to_email , subject , message , on_behalf = None , html_message = None ) : <EOL> on_behalf = on_behalf and on_behalf or settings . DEFAULT_FROM_EMAIL <EOL> if is_allowed_to_send_email_to ( to_email ) : <EOL> _message = gae_mail . EmailMessage ( sender = on_behalf , <EOL> to = to_email , <EOL> subject = subject , <EOL> body = message , <EOL> html = html_message ) <EOL> try : <EOL> _message . send ( ) <EOL> return True <EOL> except : <EOL> logging . error ( '<STR_LIT>' ) <EOL> return False <EOL> else : <EOL> log_blocked_send ( on_behalf , to_email , subject , message ) <EOL> raise exception . ValidationError ( "<STR_LIT>" ) <EOL> def get_default_attacheds ( ) : <EOL> logging . info ( "<STR_LIT>" ) <EOL> url_logo = "<STR_LIT>" % ( settings . DOMAIN , settings . DEFAULT_THEME ) <EOL> result = urlfetch . fetch ( url = url_logo ) <EOL> image = MIMEImage ( result . content ) <EOL> image . add_header ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> logging . info ( image . as_string ( ) ) <EOL> return ( '<STR_LIT>' , image . as_string ( ) ) <EOL> def filter_out_blocked_addresses ( message_tuples ) : <EOL> send_count = <NUM_LIT:0> <EOL> allowed = [ ] <EOL> for subject , message , from_email , recipients in message_tuples : <EOL> blocked = [ r for r in recipients if not is_allowed_to_send_email_to ( r ) ] <EOL> for r in blocked : <EOL> log_blocked_send ( from_email , r , subject ) <EOL> send_count += <NUM_LIT:1> <EOL> allowed_recipients = [ r for r in recipients if not r in blocked ] <EOL> allowed . append ( ( subject , message , from_email , allowed_recipients ) ) <EOL> return ( allowed , send_count ) <EOL> def mass_send ( message_tuples ) : <EOL> send_count = <NUM_LIT:0> <EOL> allowed , fake_send_count = filter_out_blocked_addresses ( message_tuples ) <EOL> send_count += fake_send_count <EOL> send_count += mail . send_mass_mail ( tuple ( allowed ) ) <EOL> return send_count <EOL> def email_comment_notification ( actor_to_ref , actor_from_ref , <EOL> comment_ref , entry_ref ) : <EOL> """<STR_LIT>""" <EOL> SITE_NAME = util . get_metadata ( '<STR_LIT>' ) <EOL> DOMAIN = settings . DOMAIN <EOL> POST_NAME = util . get_metadata ( '<STR_LIT>' ) <EOL> entry_url = entry_ref . url ( ) <EOL> entry_mobile_url = entry_ref . url ( mobile = True ) <EOL> from_name = actor_from_ref . display_nick ( ) <EOL> my_entry = ( actor_to_ref . nick == entry_ref . actor ) <EOL> entry_actor_name = util . display_nick ( entry_ref . actor ) <EOL> entry_title = entry_ref . title ( ) <EOL> comment_pretty = comment_ref . extra . get ( '<STR_LIT:content>' , '<STR_LIT>' ) <EOL> t = loader . get_template ( '<STR_LIT>' ) <EOL> c = template . Context ( locals ( ) , autoescape = False ) <EOL> message = t . render ( c ) <EOL> html_template = loader . get_template ( '<STR_LIT>' ) <EOL> html_message = html_template . render ( c ) <EOL> subject = '<STR_LIT>' % ( entry_ref . title ( ) ) <EOL> return ( subject , message , html_message ) <EOL> def email_confirmation_message ( actor , activation_code ) : <EOL> SITE_NAME = util . get_metadata ( '<STR_LIT>' ) <EOL> DOMAIN = settings . DOMAIN <EOL> name = _greeting_name ( actor ) <EOL> activation_url = '<STR_LIT>' % ( settings . DOMAIN , activation_code ) <EOL> activation_mobile_url = '<STR_LIT>' % ( settings . DOMAIN , <EOL> activation_code ) <EOL> email_first = name <EOL> email_link = activation_url <EOL> email_mobile_link = activation_mobile_url <EOL> t = loader . get_template ( '<STR_LIT>' ) <EOL> c = template . Context ( locals ( ) , autoescape = False ) <EOL> message = t . render ( c ) <EOL> c . autoescape = True <EOL> html_template = loader . get_template ( <EOL> '<STR_LIT>' ) <EOL> html_message = html_template . render ( c ) <EOL> subject = "<STR_LIT>" % util . get_metadata ( '<STR_LIT>' ) <EOL> return ( subject , message , html_message ) <EOL> def email_invite ( from_actor_ref , invite_code ) : <EOL> SITE_NAME = util . get_metadata ( '<STR_LIT>' ) <EOL> POST_NAME = util . get_metadata ( '<STR_LIT>' ) <EOL> SITE_DESCRIPTION = util . get_metadata ( '<STR_LIT>' ) <EOL> DOMAIN = settings . DOMAIN <EOL> full_name = _full_name ( from_actor_ref ) <EOL> nick_name = from_actor_ref . display_nick ( ) <EOL> accept_url = '<STR_LIT>' % ( settings . DOMAIN , invite_code ) <EOL> accept_mobile_url = '<STR_LIT>' % ( settings . DOMAIN , <EOL> invite_code ) <EOL> t = loader . get_template ( '<STR_LIT>' ) <EOL> c = template . Context ( locals ( ) , autoescape = False ) <EOL> message = t . render ( c ) <EOL> c . autoescape = True <EOL> html_template = loader . get_template ( <EOL> '<STR_LIT>' ) <EOL> html_message = html_template . render ( c ) <EOL> subject = '<STR_LIT>' % full_name <EOL> return ( subject , message , html_message ) <EOL> def email_new_follower ( owner_ref , target_ref ) : <EOL> SITE_NAME = util . get_metadata ( '<STR_LIT>' ) <EOL> DOMAIN = settings . DOMAIN <EOL> owner_full_name = _full_name ( owner_ref ) <EOL> target_full_name = _full_name ( target_ref ) <EOL> email_url = owner_ref . url ( ) <EOL> email_mobile_url = owner_ref . url ( mobile = True ) <EOL> t = loader . get_template ( '<STR_LIT>' ) <EOL> c = template . Context ( locals ( ) , autoescape = False ) <EOL> message = t . render ( c ) <EOL> c . autoescape = True <EOL> html_template = loader . get_template ( <EOL> '<STR_LIT>' ) <EOL> html_message = html_template . render ( c ) <EOL> subject = '<STR_LIT>' % ( owner_ref . display_nick ( ) , util . get_metadata ( '<STR_LIT>' ) ) <EOL> return ( subject , message , html_message ) <EOL> def email_new_follower_mutual ( owner_ref , target_ref ) : <EOL> SITE_NAME = util . get_metadata ( '<STR_LIT>' ) <EOL> DOMAIN = settings . DOMAIN <EOL> profile_url = owner_ref . url ( ) <EOL> profile_mobile_url = owner_ref . url ( mobile = True ) <EOL> full_name = _full_name ( owner_ref ) <EOL> t = loader . get_template ( <EOL> '<STR_LIT>' ) <EOL> c = template . Context ( locals ( ) , autoescape = False ) <EOL> message = t . render ( c ) <EOL> c . autoescape = True <EOL> html_template = loader . get_template ( <EOL> '<STR_LIT>' ) <EOL> html_message = html_template . render ( c ) <EOL> subject = '<STR_LIT>' % full_name <EOL> return ( subject , message , html_message ) <EOL> def email_lost_password ( actor , email , code ) : <EOL> SITE_NAME = util . get_metadata ( '<STR_LIT>' ) <EOL> DOMAIN = settings . DOMAIN <EOL> email_link = ( "<STR_LIT>" % <EOL> ( settings . DOMAIN , email , code ) ) <EOL> email_mobile_link = ( "<STR_LIT>" % <EOL> ( settings . DOMAIN , email , code ) ) <EOL> t = loader . get_template ( '<STR_LIT>' ) <EOL> c = template . Context ( locals ( ) , autoescape = False ) <EOL> message = t . render ( c ) <EOL> c . autoescape = True <EOL> html_template = loader . get_template ( <EOL> '<STR_LIT>' ) <EOL> html_message = html_template . render ( c ) <EOL> subject = ( '<STR_LIT>' ) <EOL> return ( subject , message , html_message ) <EOL> def email_dm ( actor_ref , owner_ref , direct_message ) : <EOL> SITE_NAME = util . get_metadata ( '<STR_LIT>' ) <EOL> DOMAIN = settings . DOMAIN <EOL> actor_fullname = _full_name ( actor_ref ) <EOL> fullname = _full_name ( owner_ref ) <EOL> email_link = ( "<STR_LIT>" % <EOL> ( settings . DOMAIN , actor_ref . display_nick ( ) ) ) <EOL> t = loader . get_template ( '<STR_LIT>' ) <EOL> c = template . Context ( locals ( ) , autoescape = False ) <EOL> message = t . render ( c ) <EOL> c . autoescape = True <EOL> html_template = loader . get_template ( <EOL> '<STR_LIT>' ) <EOL> html_message = html_template . render ( c ) <EOL> subject = ( '<STR_LIT>' % actor_fullname ) <EOL> return ( subject , message , html_message ) </s>
