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
| import SmtpApiHeader
import json
from django.core.mail import EmailMultiAlternatives
hdr = SmtpApiHeader.SmtpApiHeader()
# The list of addresses this message will be sent to
receiver = ['isaac@example.com', 'tim@example.com', 'jose@example.com']
# The names of the recipients
times = ['1pm', '2pm', '3pm']
# Another subsitution variable
names = ['Isaac', 'Tim', 'Jose']
# Set all of the above variables
hdr.addTo(receiver)
hdr.addSubVal('-time-', times)
hdr.addSubVal('-name-', names)
# Specify that this is an initial contact message
hdr.setCategory("initial")
# Enable a text footer and set it
hdr.addFilterSetting('footer', 'enable', 1)
hdr.addFilterSetting('footer', "text/plain", "Thank you for your business")
# fromEmail is your email
# toEmail is recipient's email address
# For multiple recipient e-mails, the 'toEmail' address is irrelivant
fromEmail = 'testing@sendgrid.net'
toEmail = 'sendgrid@hotmail.com'
# Create message container - the correct MIME type is multipart/alternative.
# Using Django's 'EmailMultiAlternatives' class in this case to create and send.
# Create the body of the message (a plain-text and an HTML version).
# text is your plain-text email
# html is your html version of the email
# if the reciever is able to view html emails then only the html
# email will be displayed
subject = 'Contact Response for <-name-> at <-time->'
text_content = 'Hi -name-!\nHow are you?\n'
html = """\
<html>
<head></head>
<body>
<p>Hi! -name-<br>
How are you?<br>
</p>
</body>
</html>
"""
msg = EmailMultiAlternatives(subject, text_content, fromEmail, [toEmail], headers={"X-SMTPAPI": hdr.asJSON()})
msg.attach_alternative(html, "text/html")
msg.send()
|