forked from sendgrid/sendgrid-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmail.py
More file actions
76 lines (68 loc) · 3.68 KB
/
Copy pathmail.py
File metadata and controls
76 lines (68 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
import sendgrid
sg = sendgrid.SendGridAPIClient(os.environ.get("SENDGRID_API_KEY"))
##################################################
# Create a batch ID #
# POST /mail/batch #
response = sg.client.mail.batch.post()
print(response.status_code)
print(response.body)
print(response.headers)
##################################################
# Validate batch ID #
# GET /mail/batch/{batch_id} #
batch_id = "test_url_param"
response = sg.client.mail.batch._(batch_id).get()
print(response.status_code)
print(response.body)
print(response.headers)
##################################################
# v3 Mail Send #
# POST /mail/send #
# This endpoint has a helper, check it out
# [here](https://github.com/sendgrid/sendgrid-python/blob/HEAD/use_cases/README.md).
data = {
"asm": {"group_id": 1, "groups_to_display": [1, 2, 3]},
"attachments": [{"content": "[BASE64 encoded content block here]", "content_id": "ii_139db99fdb5c3704", "disposition": "inline", "filename": "file1.jpg", "name": "file1", "type": "jpg"}],
"batch_id": "[YOUR BATCH ID GOES HERE]",
"categories": ["category1", "category2"],
"content": [{"type": "text/html", "value": "<html><p>Hello, world!</p><img src=[CID GOES HERE]></img></html>"}],
"custom_args": {"New Argument 1": "New Value 1", "activationAttempt": "1", "customerAccountNumber": "[CUSTOMER ACCOUNT NUMBER GOES HERE]"},
"from": {"email": "sam.smith@example.com", "name": "Sam Smith"},
"headers": {},
"ip_pool_name": "[YOUR POOL NAME GOES HERE]",
"mail_settings": {
"bcc": {"email": "ben.doe@example.com", "enable": True},
"bypass_list_management": {"enable": True},
"footer": {"enable": True, "html": "<p>Thanks</br>The SendGrid Team</p>", "text": "Thanks,/n The SendGrid Team"},
"sandbox_mode": {"enable": False},
"spam_check": {"enable": True, "post_to_url": "http://example.com/compliance", "threshold": 3},
},
"personalizations": [
{
"bcc": [{"email": "sam.doe@example.com", "name": "Sam Doe"}],
"cc": [{"email": "jane.doe@example.com", "name": "Jane Doe"}],
"custom_args": {"New Argument 1": "New Value 1", "activationAttempt": "1", "customerAccountNumber": "[CUSTOMER ACCOUNT NUMBER GOES HERE]"},
"headers": {"X-Accept-Language": "en", "X-Mailer": "MyApp"},
"send_at": 1409348513,
"subject": "Hello, World!",
"substitutions": {"id": "substitutions", "type": "object"},
"to": [{"email": "john.doe@example.com", "name": "John Doe"}],
}
],
"reply_to": {"email": "sam.smith@example.com", "name": "Sam Smith"},
"sections": {"section": {":sectionName1": "section 1 text", ":sectionName2": "section 2 text"}},
"send_at": 1409348513,
"subject": "Hello, World!",
"template_id": "[YOUR TEMPLATE ID GOES HERE]",
"tracking_settings": {
"click_tracking": {"enable": True, "enable_text": True},
"ganalytics": {"enable": True, "utm_campaign": "[NAME OF YOUR REFERRER SOURCE]", "utm_content": "[USE THIS SPACE TO DIFFERENTIATE YOUR EMAIL FROM ADS]", "utm_medium": "[NAME OF YOUR MARKETING MEDIUM e.g. email]", "utm_name": "[NAME OF YOUR CAMPAIGN]", "utm_term": "[IDENTIFY PAID KEYWORDS HERE]"},
"open_tracking": {"enable": True, "substitution_tag": "%opentrack"},
"subscription_tracking": {"enable": True, "html": "If you would like to unsubscribe and stop receiving these emails <% clickhere %>.", "substitution_tag": "<%click here%>", "text": "If you would like to unsubscribe and stop receiving these emails <% click here %>."},
},
}
response = sg.client.mail.send.post(request_body=data)
print(response.status_code)
print(response.body)
print(response.headers)