Skip to content

Commit 9bba537

Browse files
Mail Send Hello World
1 parent 7245bcb commit 9bba537

1 file changed

Lines changed: 13 additions & 14 deletions

File tree

proposals/mail-helper-refactor.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,21 @@ The following code assumes you are storing the API key in an [environment variab
55
```python
66
import os
77
import sendgrid
8-
from sendgrid.helpers.mail import *
9-
10-
client = sendgrid.SendGridClientFactory(os.environ.get('SENDGRID_API_KEY'))
11-
from_email = Email("test@example.com", "Example User")
12-
to_email = Email("test@example.com", "Example User")
13-
subject = "Sending with SendGrid is Fun"
14-
plain_text_content = "and easy to do anywhere, even with Python"
15-
html_content = "<strong>and easy to do anywhere, even with Ruby</strong>"
16-
msg = Mail.create_single_email(from_email, subject, to_email, content)
8+
from sendgrid.helpers.mail import From, To, Subject, PlainTextContent, HtmlContent, Mail
9+
10+
msg = Mail(From("test@example.com", "Example User"),
11+
To("test@example.com", "Example User"),
12+
Subject("Sending with SendGrid is Fun"),
13+
PlainTextContent("and easy to do anywhere, even with Python"),
14+
HtmlContent("<strong>and easy to do anywhere, even with Ruby</strong>"))
15+
1716
try:
18-
response = client.send_email(msg)
19-
except urllib.error.HTTPError as e:
17+
response = sendgrid.send(msg, api_key=os.environ.get('SENDGRID_API_KEY'))
18+
print(response.status_code)
19+
print(response.body)
20+
print(response.headers)
21+
except Exception as e:
2022
print(e.read())
21-
print(response.status_code)
22-
print(response.body)
23-
print(response.headers)
2423
```
2524

2625
# Send a Single Email to Multiple Recipients

0 commit comments

Comments
 (0)