Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using mandrill (mailchimp) service to send emails
from application.mail import mail_send
...
SECURITY_SEND_MAIL_FUNCTION = mail_send
...
FROM_EMAIL = 'dispatch@site.ru'
FROM_NAME = u'Site Admin'
EMAIL_ARCHIVE = 'mail-archive@site.ru'
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import current_app
from mailsnake import MailSnake
mandrill_api = MailSnake('api_key', api='mandrill')
def mail_send(subject, recipient, template, **kwargs):
if template == 'welcome':
send_welcome(**kwargs)
elif template == 'confirmation_instructions':
send_confirmation_instructions(**kwargs)
elif template == 'login_instructions':
send_login_instructions(**kwargs)
elif template == 'reset_instructions':
send_reset_instructions(**kwargs)
elif template == 'reset_notice':
send_reset_notice(**kwargs)
else:
return NotImplemented
def send_welcome(**kwargs):
context = {}
context['template_name'] = 'welcome'
context['template_content'] = []
context['message'] = {"subject": u"Site.ru - Thanks for registration :)",
"from_email": current_app.config.get('FROM_EMAIL'),
"from_name": current_app.config.get('FROM_NAME'),
"to":[{"email": kwargs['user'].email}],
"track_opens":True,
"track_clicks":True,
"auto_text":True,
"url_strip_qs":False,
"preserve_recipients":False,
"bcc_address": current_app.config.get('EMAIL_ARCHIVE'),
"merge_vars":[
{
"rcpt": kwargs['user'].email,
"vars":[{"name": "CONFIRMATION_LINK", "content": kwargs['confirmation_link']}]}],
"tags":["welcome"],
"recipient_metadata": [{
"rcpt": kwargs['user'].email,
"values": [{ "user_id": kwargs['user'].id}]}]
}
mandrill_api.messages.send_template(**context)
def send_confirmation_instructions(**context):
pass
def send_login_instructions(**context):
pass
def send_reset_instructions(**context):
pass
def send_reset_notice(**context):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.