Skip to content

Commit 642b5f8

Browse files
committed
Doc: Update the send email tutorial sendgrid
1 parent beed070 commit 642b5f8

File tree

2 files changed

+100
-2
lines changed

2 files changed

+100
-2
lines changed

twilio_sendgrid/01_create_acccount.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Create Twilio SendGrid Account
22

33

4-
Is it your first time implementing email support in your Flask app? Start [here](/email_support_in_flask.md). If not, check out the other parts of the Twilio SendGrid series below:
4+
Is it your first time implementing email support in your Flask app? If so, start [here](/email_support_in_flask.md). If not, check out the other parts of the Twilio SendGrid series below:
55

66
- [SendGrid Overview](/twilio_sendgrid/00_overview.md)
77
- [Create A Twilio SendGrid Account](/twilio_sendgrid/01_create_acccount.md) (this article)
@@ -51,4 +51,4 @@ You will be required to include your contact information.
5151

5252
![Create Sender](/images/sendgrid/create_account/create_sender.png)
5353

54-
Click create to create your sender. An verification email will be sent to your inbox. Once verified, you are all set.
54+
Click **Create** to create your sender. An verification email will be sent to your inbox. Once verified, you are all set.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
![Settings API key](/images/sendgrid/send_emails/settings_api.png)
19+
20+
Then, click on the blue "Create API" button.
21+
22+
![Create API Key](/images/sendgrid/send_emails/create_api_key.png)
23+
24+
Fill in the details and select "Full Access" for permissions. This will allow you to perform all email sending functions.
25+
26+
![Name API Key](/images/sendgrid/send_emails/name_api_key.png)
27+
28+
You will be redirected to another page containing your API Key. Copy the key in your clipboard.
29+
30+
![API Key](/images/sendgrid/send_emails/api_key.png)
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+
![Sender authentication](/images/sendgrid/send_emails/sender_authentication.png)
94+
95+
Click on "Verify A Single Sender".
96+
97+
![Verify A Single Sender](/images/sendgrid/send_emails/verify_a_single_sender.png)
98+

0 commit comments

Comments
 (0)