def send_mail(to, subject, text, html=None): ''' Utility to handle the Amazon SES mailer and configuration logic. If mail isn't enabled, emails are logged instead. ''' if type(to) != list: to = [to] mail_access_key = CONFIG.get('email', 'access_key_id') mail_access_secret = CONFIG.get('email', 'secret_access_key') sender = CONFIG.get('email', 'sender') if all([mail_access_key, mail_access_secret, sender]): mailer = AmazonSESMailer(mail_access_key, mail_access_secret) mailer.send(subject=subject, text=text, html=html, from_email=sender, to=to)
def send_email_task(self, email_id): logger = get_task_logger(__name__) try: email_message = EmailMessage.objects.get(id=email_id) except EmailMessage.DoesNotExist: return False email_message.sending_status = EmailMessage.SENDING email_message.save() from_email = email_message.sender_email_address if email_message.sender_name.strip() != '': from_email = '%s <%s>' % (email_message.sender_name, email_message.sender_email_address) to_email = email_message.recipient_email_address if email_message.recipient_name.strip() != '': to_email = '%s <%s>' % (email_message.recipient_name, email_message.recipient_email_address) reply_email = [] if email_message.reply_email_address.strip() != '': reply_email = [email_message.reply_email_address] if email_message.reply_name.strip() != '': reply_email = '%s <%s>' % (email_message.reply_name, email_message.reply_email_address) reply_email = [reply_email] message_package = EmailMessageContainer( subject=email_message.subject, text_content=email_message.plain_content, html_content=email_message.html_content, from_email=from_email, to=to_email, reply_to=reply_email) # Send to the console mailer = ToConsoleMailer() mailer.send_messages(message_package) mailer = AmazonSESMailer( aws_access_key_id=settings.AWS_ACCESS_KEY_ID, aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY, region_name='eu-west-1') responses = mailer.send_messages(message_package) if len(responses) < 1: email_message.sending_status = email_message.FAILED_TO_SEND email_message.save() logger.error("Unable to send email #%s" % email_message.pk) return False email_message.sending_status = EmailMessage.SENT email_message.message_id = responses[0].get('MessageId') email_message.save() logger.info("Sent email %s" % email_message) return True
def mail(app_conf, env, project_root='.'): if not app_conf: return smtp_conf = { k[5:]: v for k, v in app_conf.__dict__.items() if k.startswith('smtp_') } if smtp_conf: smtp_conf.setdefault('timeout', app_conf.socket_timeout) if getattr(app_conf, 'ses_region', None): mailer = AmazonSESMailer(env.aws_access_key_id, env.aws_secret_access_key, region_name=app_conf.ses_region) elif smtp_conf: mailer = SMTPMailer(**smtp_conf) else: mailer = ToConsoleMailer() emails = {} emails_dir = project_root + '/emails/' i = len(emails_dir) for spt in find_files(emails_dir, '*.spt'): base_name = spt[i:-4] emails[base_name] = compile_email_spt(spt) def log_email(message): message = dict(message) html, text = message.pop('html'), message.pop('text') print('\n', ' ', '=' * 26, 'BEGIN EMAIL', '=' * 26) print(json.dumps(message)) print('[---] text/html') print(html) print('[---] text/plain') print(text) print(' ', '=' * 27, 'END EMAIL', '=' * 27) if app_conf.log_emails and not isinstance(mailer, ToConsoleMailer): log_email = log_email else: log_email = lambda *a, **kw: None return {'emails': emails, 'log_email': log_email, 'mailer': mailer}