8

I'm trying to write a separate mail service, which is decoupled with our Flask application. I'm looking for a way to send welcome emails when users first log into our Flask application. I'm using Celery and RabbitMQ to do it asynchronously.

Here is my email function:

sen = '[email protected]'
pwd = 'my_password'

@celery.task
def send_email(nickname, email):
    msg = MIMEMultipart('alternative')
    msg['Subject'] = 'my_sub'
    msg['From'] = sen
    msg['To'] = email

    html = <b>test_body</b>

    part1 = MIMEText(html, 'html')
    msg.attach(part1)

    server = smtplib.SMTP("smtp.gmail.com", 587) 
    server.ehlo()
    server.starttls()
    server.login(sen, pwd)
    server.sendmail(sen, [email], msg.as_string())
    server.close()

Initially I was using Flask's render_template to get the HTML body and subject. But I don't want to use the Flask extension (I have my reasons). So my questions are:

  1. How can I use email templates so that the subject and body fields can be configured easily?

  2. How can I put the default email sender and password in a config file/email template (might be related to q1)?

  3. It seems to be that I have a lot of code to send a simple email. Can you suggest some optimization techniques (omitting steps)?

4
  • Another reason for using email template - pass in params. In order to send personalized emails. Commented Oct 25, 2013 at 17:18
  • 1
    If you don't want to use template engine like jinja2, you would consider about the python standard library string.Template which provides simple string substitutions. Commented Oct 25, 2013 at 17:32
  • @user2216194, i also have similar requirements, did you come up with a solution? and what is your reason for not using jinja? or why not render_template? (edit: it'll help me in evaluating solutions) Commented Feb 20, 2014 at 4:45
  • Someone asked a similar question here. <stackoverflow.com/questions/2809547/…> Commented Oct 7, 2014 at 23:28

2 Answers 2

2

I've wrritten a simple module(mail.py) to send emails using templates(HTML/TEXT). Hope that helps!

https://github.com/ludmal/pylib

Sign up to request clarification or add additional context in comments.

Comments

0

It may be simpler to use an external service.

A service (e.g. Mailchimp) is simple to integrate. You can design the template in their layer, and trigger emails by sending merge data from your app to the service API. Functionally it's a lot like rendering a template locally and mailing it out via SMTP, but they have sophisticated tools for adapting message format to devices, tracking bounces, improving deliverability, reporting etc.

Services like this often have a free tier for up to 1000's of emails per month.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.