|
| 1 | +# Send Emails Using Twilio SendGrid |
| 2 | + |
| 3 | +Setting up an email service for your application is always difficult. You can choose to set everything up yourself or use a third party email service. The most ideal approach, all factors considered, is always to favour the simplicity of a dedicated email service. |
| 4 | + |
| 5 | +In the tutorial [Email Support In Flask](/email_support_in_flask.md), we went over how you can use the Flask-Mail extension to set an email infrastructure for your application. In this article, you will learn how to integrate Twilio SendGrid in a similar application. |
| 6 | + |
| 7 | +## Requiremets |
| 8 | + |
| 9 | +- Python 3.6 and above |
| 10 | +- A free [Twilio SendGrid account](/twilio_sendgrid/01_create_acccount.md) |
| 11 | +- Familiarity with Flask |
| 12 | + |
| 13 | + |
| 14 | +## SendGrid Configuration |
| 15 | + |
| 16 | +Once you have created an account, you need to generate an API key. To do so, from your dashboard, click "Settings > API Key". |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +Then, click on the blue "Create API" button. |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +Fill in the details and select "Full Access" for permissions. This will allow you to perform all email sending functions. |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +You will be redirected to another page containing your API Key. Copy the key in your clipboard. |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +This key should be a secret. I am showing it to you now, but I will discard it in no time. Once copied, click "Done". |
| 33 | + |
| 34 | +## Updating Email Keys |
| 35 | + |
| 36 | +In the project built in [Email Support In Flask](/email_support_in_flask.md#add-email-server-details), we set up our email configurations as follows: |
| 37 | + |
| 38 | +```python |
| 39 | +# config.py: Email configuration |
| 40 | + |
| 41 | +# ... |
| 42 | + |
| 43 | + |
| 44 | +class Config(object): |
| 45 | + # ... |
| 46 | + |
| 47 | + # Email configurations |
| 48 | + MAIL_SERVER = os.environ.get('MAIL_SERVER') |
| 49 | + MAIL_PORT = int(os.environ.get('MAIL_PORT') or 25) |
| 50 | + MAIL_USE_TLS = os.environ.get('MAIL_USE_TLS') is not None |
| 51 | + MAIL_USERNAME = os.environ.get('MAIL_USERNAME') |
| 52 | + MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD') |
| 53 | + |
| 54 | +``` |
| 55 | + |
| 56 | +The only change we are going to make is the `MAIL_USERNAME` and `MAIL_PASSWORD`. We are also going to add `MAIL_DEFAULT_SENDER`. |
| 57 | + |
| 58 | +```python |
| 59 | +# config.py: SendGrid configuration |
| 60 | + |
| 61 | +# ... |
| 62 | + |
| 63 | + |
| 64 | +class Config(object): |
| 65 | + # ... |
| 66 | + |
| 67 | + # Email configurations |
| 68 | + MAIL_SERVER = os.environ.get('MAIL_SERVER') |
| 69 | + MAIL_PORT = int(os.environ.get('MAIL_PORT') or 25) |
| 70 | + MAIL_USE_TLS = os.environ.get('MAIL_USE_TLS') is not None |
| 71 | + MAIL_USERNAME = os.environ.get('MAIL_USERNAME') |
| 72 | + MAIL_PASSWORD = os.environ.get('SENDGRID_API_KEY') |
| 73 | + MAIL_DEFAULT_SENDER = os.environ.get('MAIL_DEFAULT_SENDER') |
| 74 | +``` |
| 75 | + |
| 76 | +The values of these variables are found in `.env` file as follows: |
| 77 | + |
| 78 | +```python |
| 79 | +# .env: Values of environment variables |
| 80 | + |
| 81 | +MAIL_SERVER=smtp.sendgrid.net |
| 82 | +MAIL_PORT=587 |
| 83 | +MAIL_USE_TLS=True |
| 84 | +MAIL_USERNAME=apikey |
| 85 | +SENDGRID_API_KEY=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 |
| 86 | +MAIL_DEFAULT_SENDER=name@email.com |
| 87 | +``` |
| 88 | + |
| 89 | +While working with SendGrid, the mail server will always be `smtp.sendgrid.net`, the port `587` (or `25` if you prefer) and `TLS` must be enabled. For the username, you have to use `apikey`. Your password will be your API Key we just copied earlier. SendGrid will require you to authenticate an email that will be used in the "From" field. This email is what is referred to as `MAIL_DEFAULT_SENDER`. |
| 90 | + |
| 91 | +To create a `MAIL_DEFAULT_SENDER`, click on "Settings > Sender Authentication". |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | +Click on "Verify A Single Sender". |
| 96 | + |
| 97 | + |
| 98 | + |
0 commit comments