How To Send And Receive Email With Django And Amazon SES

MicroPyramid
2 min readNov 9, 2017

--

Using Amazon SES, You can send and receive email from your verified emails and domains. SES offers email receiving for entire domains or for individual addresses within a domain.We can use other AWS services S3(to store encrypted messages in bucket), SNS topic(to process the message, lamda function).

To send, receive, process the incoming messages, we have a package called django-ses-gateway

Install:

pip install django-ses-gateway

Things To Do Before Integration:

1. You must have an AWS account and you need to verify domain in SES to confirm that we are using those domains or to prevent from others using it. Once your domain is verified, then all your emails in the domain will also be verified.

AWS_ACCESS_KEY_ID = "Your AWS Access Key"    AWS_SECRET_ACCESS_KEY = "Your AWS Secret Key"

2.To use Amazon SES as your email receiver, you must create at least one active receipt rule set in which you have specified the sns topic(to route messages to an endpoint), s3 bucket(to store messages), lamda function(to process the receiving email) details.

3. After creating the rule set, for an sns topic you need to specify the list of recipients to which you want to process the message.

4. You need to subscribe to the specified SNS topic by providing your domain url(which handles email recieving) as an endpoint. You can give multiple enpoints which may either email, AWS SQS, HTTP or HTTPS.

5. In SES panel, you can also give a list of ip address to which you want reject the email receiving.

How amazon ses performs email receiving:

1. When user replies to an email, then it checks in the active receipt rule set(checks the specified email address, all addresses in the domain.) in the particular region.

2. If any rule set matches, then it checks against your IP address filters, and rejected if there any matches.

3. then after ses will store those messages in an crypted format using a key stored in AWS Key Management Service (KMS), publish the message to a specified SNS topic(endpoint).

Usage:

1. Sending an email

from django_ses_gateway.receiving_mail import sending_mail    sending_mail(subject, email_template_name, context, from_email, to_email)

Here we are sending a from_mail as <unique-identifier>@yourdomain.com. Here unique identifier will be used to process the reply to thi mail.

2. Receiving an email

from django_ses_gateway.receiving_mail import sns_notification    subject, from_mail, to_mail, hash_code, mail_content = sns_notification(request.body)

Using the unique identifier(hash_code), we can easily process or store the incoming messages.

It will process your message content, will return the email subject, from mail, to email(abc@yourdomain.com), hashcode(abc) or unique-identifier, mail content.

You can view the complete documentation here. http://django-ses-gateway.readthedocs.io/en/latest/

Github Repository: https://github.com/MicroPyramid/django-ses-gateway

The article was originally published at MicroPyramid blog

--

--

MicroPyramid
MicroPyramid

Written by MicroPyramid

Python, Django, Android and IOS, reactjs, react-native, AWS, Salesforce consulting & development company

No responses yet