Code Monkey home page Code Monkey logo

ses-mailer's Introduction

SES Mailer

A simple module to send email via AWS SES

You can use it as standalone or with Flask

Requirements:

  • AWS Credentials
  • boto
  • Jinja

Install

pip install ses-mailer

Usage

Setup

from ses_mailer import Mail

mail = Mail(aws_access_key_id="####",
             aws_secret_access_key="####",
             region="us-east-1",
             sender="[email protected]",
             reply_to="[email protected]",
             template="./email-templates")

Send Basic Email

mail.send(to="[email protected]",
          subject="My Email subject",
          body="My message body")

Send Template Email

mail.send_template(template="welcome.txt", 
                   to="[email protected]", 
                   name="Ricky Roze", 
                   user_id=12739)

##API

Mail._init_

- aws_access_key_id
- aws_secret_access_key
- sender
- reply_to
- template

Mail.send

- to
- subject
- body
- reply_to

Mail.send_template

- template
- to
- reply_to
- **context	

Initiate with Flask

from flask import Flask
from ses_mailer import Mail

app = Flask(__name__)

mail = Mail()
mail.init_app(app)

Templates

You can use pre-made templates to send email

The template must be a Jinja template, containing at least the following blocks:

- subject
- body

welcome.txt

{% block subject %}
    Welcome {{name}} to our site 
{% endblock %}

{% block body %}
    Dear {{name}} this is the content of the message 
    
    Thank you very much for your visiting us
{% endblock %}

File Templates:

Place you template files inside of a directory, let's say: email-templates

Inside of email-templates we have the following files:

/email-templates
    |
    |_ welcome.txt
    |
    |_ lost-password.txt
    
    
mail = Mail(aws_access_key_id="####",
             aws_secret_access_key="####",
             sender="[email protected]",
             reply_to="[email protected]",
             template="./email-templates")

Dictionary based templates

If you don't want to create files, you can dictionary based templates

templates = {
    "welcome.txt": """
        {% block subject %}I'm subject{% endblock %}
        {% block body %}How are you {{name}}?{% endblock %}
    """,
    "lost-password.txt": """
        {% block subject %}Lost Password{% endblock %}
        {% block body %}Hello {{ name }}. 
        Here's your new password: {{ new_password }} 
        {% endblock %}
    """,    
}

mail = Mail(aws_access_key_id="####",
             aws_secret_access_key="####",
             sender="[email protected]",
             reply_to="[email protected]",
             template=templates)

To send the email for either files or dictionary based templates:

new_password = "mynewpassword"
mail.send_template("lost-password.txt", to="[email protected]", name="Lolo", new_password=new_password)

Config For Flask

SES-Mailer is configured through the standard Flask config API. These are the available options:

SES_AWS_ACCESS_KEY : Your AWS access key id

SES_AWS_SECRET_KEY: Your AWS secred key

SES_REGION: AWS region of the SES

SES_SENDER: The sender email address as string.

SES_REPLY_TO: The reply to address

SES_TEMPLATE: (mixed) directory or dict of template to use as template

SES_TEMPLATE_CONTEXT: A dict of default data to be passed by default

SES_AWS_ACCESS_KEY = ""
SES_AWS_SECRET_KEY = ""
SES_REGION = ""
SES_SENDER = ""
SES_REPLY_TO = ""
SES_TEMPLATE = None
SES_TEMPLATE_CONTEXT = {}

(c) 2015 Mardix

ses-mailer's People

Contributors

mardix avatar sid137 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

ses-mailer's Issues

Email address is not verified

Getting this error when I try to send a mail. The email address is verified and they key is correct.

When I check

from ses_mailer import Mail
mail = Mail(aws_access_key_id="<KEY>", aws_secret_access_key="<KEY>", sender="<sender_email>", reply_to="<reply_to_email>")
mail.ses.host

I get

'email.us-east-1.amazonaws.com'

but my area isn't us-east-1, I need us-west-2.
Is there any way to change the host?

AttributeError: 'NoneType' object has no attribute 'render_blocks'

File .....view.py", line 331, in forgot_password
user_id=12739)
File .../ses_mailer.py", line 224, in send_template
mail_data = self.parse_template(template, **context)
File .../ses_mailer.py", line 245, in parse_template
blocks = self.template.render_blocks(template, **context)
AttributeError: 'NoneType' object has no attribute 'render_blocks'

What's user_id here ?

Fails to use IAM Instance Profile credentials properly

Hi,

I was trying to use this to send status emails from an EMR cluster, however the library wouldn't properly load the credentials set by the IAM role. It turns out because you force the providing of the aws_access_key_id and aws_secret_key here: https://github.com/mardix/ses-mailer/blob/master/ses_mailer.py#L127

Removing this condition allows boto to fall back to the default credentials provider chain and work properly. More details provided here: https://forums.aws.amazon.com/thread.jspa?messageID=539025

Here was the change that works now: https://github.com/ividata/ses-mailer/commit/1eacf2c6c884e5464e323c34a64c0ed909e81e2f

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.