Add & Configure Mail in Django (or Wagtail).

A concise tutorial to add a production mail service to your Django app.

Published: May 28, 2021, 10:41 p.m.
Last Updated: July 15, 2021, 3:57 p.m.
email-pexels-miguel-á-padriñán.jpg

Category: Coding

This short guide will provide the steps necessary to add the (free) SendGrid mail service to a Django (or Wagtail) app.

Preface

Mail Service Provider

During development of a Django or Wagtail (now referred to just as Django) app, the email is handled by a development account or sent to the console via EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'. When deploying a Django app to production, a reliable and secure mail service is required.

A mail (or email) service provider simply hosts email servers to send, receive, accept, and store email for other organizations or end users (on their behalf). The following protocols are generally used: Simple Mail Transfer Protocol (SMTP) and possibly providing access to messages through Internet Message Access Protocol (IMAP), the Post Office Protocol, Webmail, or a proprietary protocol.

There are many such providers, for example: mialgun, mailchimp, omnisend, sendinblue, etc. In this article, we will use SendGrid

SendGrid

In addition to being easy to use, SendGrid is free (no credit card/account required) with 100 email a day, up to 2,000 contacts, and 6,000 marketing emails a month.

Mail can be sent via SMTP, UI, API; and the API is available in 7 languages (C#, Go, Java, Node JS, PHP, Python, and Ruby)

Tutorial

How to Add a Django (or Wagtail) Mail Service

Without further ado:

Step 1: Create an account at SendGrid.

SendGridTutorialWelcomeScreen.png

Step 2: If the Authenticate a domain instead option is available click it, otherwise; (on the sidebar) navigate to Settings > Sender Authentication. Then click the Get Started button in the Domain Authentication row.

Step 3: Select or enter (if the option is not yet available) your DNS host and select Yes under the Would you also like to brand the links for this domain? radial button, then click the Next button. NOTE: "Email link branding allows all of the click-tracked links and opens tracked images in your emails to be from your domain instead of from sendgrid.net." See the How to set up link branding page for more info.

SendGridTutorialAuthenticateYourDomain.png

Step 4: Enter your domain in the Domain You Send From text box and press the Next button.

SendGridTutorialAuthenticateYourDomain2.png

Step 5: Log In to your DNS host and install, or add, each DNS record shown in the Add all of these records to <your_dns_host>'s DNS section.

SendGridTutorialDNSInstructions.png

Then check the I've added these records check box, and press the Verify Button. NOTE: For additional documentation see the How to set up domain authentication doc page and the Troubleshooting Sender Authentication doc page.

SendGridTutorialAddedRecords.png

Step 6: After your domain is authenticated, (on the sidebar) navigate to Email API > Integration Guide. You can now use one of the (7) Web APIs or SMTP Relay via their respective Choose buttons.

SendGridTutorialTwoOptions.png

NOTE: We will choose the SMTP Relay option in this tutorial (a few supplementary screenshots for the Web API option will be in the Web API section below).

Click on the Choose button below SMTP Relay.

7th Step: Enter a Key Name in the My First API Key Name text box and click on the Create Key button. Enter the generated key in your Python settings file.

SendGridTutorialSMTPRelaySetup.png

Example Settings:

...
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = 'apikey'
EMAIL_HOST_PASSWORD = "my_12345_key.67890^derp"
DEFAULT_FROM_EMAIL = 'webmaster@mpettersson.com'
 ...

Once done, check the I've updated my settings check box and click on the Next: Verify Integration button.

8th Step: You will now have an opportunity to test your configuration.

SendGridTutorialTestIntegration.png

From within your Django apps directory (with your python environment set), enter the following commands to test the email configuration:

python manage.py shell
from django.core.mail import send_mail
send_mail('Test Subject Text', 'Test Message Text', 'from@<your_domain_here>', ['<to_you>@<your_email_here>'], fail_silently=False,)

If successful configured, <to_you>@<your_email_here> will receive an email from from@<your_domain_here>. Click the Verify Integration Button so SendGrid can confirm and provide you with additional links.

SendGridTutorialGreatSuccess.png


Congratulations, You're Done!


(Alternatively) Web API Option:

The web api option is essentially the same; pick a language, generate a key, add the key to your configuration/environment, and then test.

Pick a language, any language:

SendGridTutorialWebAPILangs.png

Then follow the remaining instructions for your chosen language:

SendGridTutorialPythonWebAPI.png
Back to the Blog Page  ⋅  0 Comments have been posted.

No comments yet. Be the first to comment!

To comment, please sign in or provide a name and valid email address.