Skip to main content

SMTP and SES mail integration with AWS Chalice

Project description

One of the most basic functions in a web application is the ability to send emails to your users fully serverlessly.

# Maintainers wanted

Chalice-Mail

The Chalice-Mail extension provides a simple interface to set up SMTP and AWS SES(Simple Email Service) with your Chalice application and to send messages from your views and scripts. source code available at: Github Repo

Usage

Using SMTP => TLS Encryption
from chalice import Chalice
from chalice_mail import Mail, Message

app = Chalice(app_name='chalice-mail-test1')
mail = Mail(app)
mail.is_smtp = True
mail.smtp_using_tls = True
mail.username = "someone@example.com"
mail.password = "world's_top_secret_password"
mail.smtp_server = 'smtp.example.com'
mail.smtp_port = 587
mail.login()

@app.route('/send-smtp-mail')
def send_smtp_mail():
    msg = Message(mail)
    msg.subject = "this is the subject"
    msg.add_recipient("aniketsarkar@yahoo.com")
    msg.plain = "This is the email body."
    mail.send_email(msg)
    return {'message':'email sended successfully'}
Using SMTP => SSL Encryption
from chalice import Chalice
from chalice_mail import Mail, Message

app = Chalice(app_name='chalice-mail-test1')
mail = Mail(app)
mail.is_smtp = True
mail.smtp_using_ssl = True
mail.username = "someone@example.com"
mail.password = "world's_top_secret_password"
mail.smtp_server = 'smtp.example.com'
mail.smtp_port = 465
mail.login()

@app.route('/send-smtp-mail')
def send_smtp_mail():
    msg = Message(mail)
    msg.subject = "this is the subject"
    msg.add_recipient("aniketsarkar@yahoo.com")
    msg.plain = "This is the email body."
    mail.send_email(msg)
    return {'message':'email sended successfully'}
Using SES(Simple Email Service)
from chalice import Chalice
from chalice_mail import Mail, Message

app = Chalice(app_name='chalice-mail-test1')
mail = Mail(app)
mail.username = "someone@example.com"
mail.aws_region = "ap-south-1"
"""
please provide the value of 'mail.aws_secret_id' 
and 'mail.aws_secret_key' if you want to set the AWS 
Iam user manually.
"""
mail.login()

@app.route('/send-smtp-mail')
def send_smtp_mail():
    msg = Message(mail)
    msg.subject = "this is the subject"
    msg.add_recipient("aniketsarkar@yahoo.com")
    msg.plain = "This is the email body."
    mail.send_email(msg)
    return {'message':'email sended successfully'}
Using SMTP => Email with HTML
from chalice import Chalice
from chalice_mail import Mail, Message

app = Chalice(app_name='chalice-mail-test1')
mail = Mail(app)
mail.is_smtp = True
mail.smtp_using_tls = True
mail.username = "someone@example.com"
mail.password = "world's_top_secret_password"
mail.smtp_server = 'smtp.example.com'
mail.smtp_port = 587
mail.login()

@app.route('/send-smtp-mail')
def send_smtp_mail():
    msg = Message(mail)
    msg.subject = "this is the subject"
    msg.add_recipient("aniketsarkar@yahoo.com")
    """ 
    Simply pass the html value to the html variable of Message instance.
    """
    msg.html = "<h1>This is the email body.</h1>"
    mail.send_email(msg)
    return {'message':'email sended successfully'}
Using SMTP => Email with HTML rendering
from chalice import Chalice
from chalice_mail import Mail, Message
from pathlib import Path
from os import path

app = Chalice(app_name='chalice-mail-test1')
_baseDir = Path(path.realpath(__file__)).parent
mail = Mail(app)
mail.is_smtp = True
mail.smtp_using_tls = True
mail.username = "someone@example.com"
mail.password = "world's_top_secret_password"
mail.smtp_server = 'smtp.example.com'
mail.smtp_port = 587
mail.template_dir = _baseDir/'templates' # required if using template rendering.
mail.login()

@app.route('/send-smtp-mail')
def send_smtp_mail():
    msg = Message(mail)
    msg.subject = "this is the subject"
    msg.add_recipient("aniketsarkar@yahoo.com")
    context={}
    """
    This package comes with the jinja2 based template rendering system.
    Simpley use the 'render_template()' function to rend the html file.
        'render_template()' functions takes the html file name as arguments 
    and the context as well as.
    """
    msg.html = mail.render_template('index.html', context)
    mail.send_email(msg)
    return {'message':'email sended successfully'}
Using SMTP => Email with attachments
from chalice import Chalice
from chalice_mail import Mail, Message
from pathlib import Path
from os import path

app = Chalice(app_name='chalice-mail-test1')
_baseDir = Path(path.realpath(__file__)).parent
mail = Mail(app)
mail.is_smtp = True
mail.smtp_using_tls = True
mail.username = "someone@example.com"
mail.password = "world's_top_secret_password"
mail.smtp_server = 'smtp.example.com'
mail.smtp_port = 587
mail.attachment_dir = _baseDir/'attachments' # required if using attachments.
mail.login()

@app.route('/send-smtp-mail')
def send_smtp_mail():
    msg = Message(mail)
    msg.subject = "this is the subject"
    msg.add_recipient("aniketsarkar@yahoo.com")
    msg.plain = "This is the email body."
    """
    Use the 'add_attachments()' function to add the attachments 
    with the message instance. Don't forget to put the attachments 
    on the attachments folder.
        'add_attachments()' function basically takes list or str data 
    type as argument. If you want to add only one attachment just pass 
    the attachment name. If you want to add more than one attachments use a list.
    """
    msg.add_attachments(['python.png', 'README.rst'])
    mail.send_email(msg)
    return {'message':'email sended successfully'}

Installation

chalice-mail is available from pypi.

install using pip

pip install chalice-mail

install from source code

git clone https://github.com/marktennyson/chalice-mail && cd chalice-mail
python setup.py install --user

Compatibility

chalice-mail is compatiable with python3.6 and higher versions. Not compatiable for Python version 2.

Contributing

We welcome contributions of all types!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chalice-mail-0.0.1.tar.gz (8.6 kB view details)

Uploaded Source

Built Distributions

chalice_mail-0.0.1-py3.9.egg (13.7 kB view details)

Uploaded Source

chalice_mail-0.0.1-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file chalice-mail-0.0.1.tar.gz.

File metadata

  • Download URL: chalice-mail-0.0.1.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.23.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for chalice-mail-0.0.1.tar.gz
Algorithm Hash digest
SHA256 c66d335ecabd34327bcde62101cf598c036b95c4f3e2cc168d40e668b75fe4c2
MD5 43c840b872c70fd4b447bf5be97ad92c
BLAKE2b-256 68f83803b9fd4633041678f914d4eb66343bf8f60fc5a68bdf0339b067cc4180

See more details on using hashes here.

File details

Details for the file chalice_mail-0.0.1-py3.9.egg.

File metadata

  • Download URL: chalice_mail-0.0.1-py3.9.egg
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.23.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for chalice_mail-0.0.1-py3.9.egg
Algorithm Hash digest
SHA256 7294b14ff05e57508a5e4b49932f94bc34af638cc823ed392fcfe94b64465901
MD5 428b0927d428e759d8e83dbd6aaef2f7
BLAKE2b-256 514e4bda47395467ef5cbf5fd0a7d1a35851aeafaa29e385682b2d046f2274b9

See more details on using hashes here.

File details

Details for the file chalice_mail-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: chalice_mail-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.23.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for chalice_mail-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 97c263a13c4670dcda5b1ef6b898021d3c906a332a9b65aa957f6756e40d484d
MD5 b354d833ae928829898aee9256d956b3
BLAKE2b-256 aa486cea1236c844c68f8bcc52545e1cbd54e60d589cea0a61b72129c58a32a0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page