forked from sendgrid/sendgrid-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.py
More file actions
98 lines (79 loc) · 2.78 KB
/
Copy pathmessage.py
File metadata and controls
98 lines (79 loc) · 2.78 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import io
import sys
try:
import rfc822
except Exception as e:
import email.utils as rfc822
from smtpapi import SMTPAPIHeader
class Mail(SMTPAPIHeader):
"""SendGrid Message."""
def __init__(self, **opts):
"""
Constructs SendGrid Message object.
Args:
to: Recipient
to_name: Recipient name
from: Sender
subject: Email title
text: Email body
html: Email body
bcc: Recipient
reply_to: Reply address
date: Set date
headers: Set headers
x-smtpapi: Set SG custom header
files: Attachments
"""
super(Mail, self).__init__()
self.to = opts.get('to', [])
self.to_name = opts.get('to_name', [])
self.from_email = opts.get('from_email', '')
self.from_name = opts.get('from_name', '')
self.subject = opts.get('subject', '')
self.text = opts.get('text', '')
self.html = opts.get('html', '')
self.bcc = opts.get('bcc', [])
self.reply_to = opts.get('reply_to', '')
self.files = opts.get('files', {})
self.headers = opts.get('headers', '')
self.date = opts.get('date', rfc822.formatdate())
def add_to(self, to):
name, email = rfc822.parseaddr(to.replace(',', ''))
if email:
self.to.append(email)
if name:
self.add_to_name(name)
def add_to_name(self, to_name):
self.to_name.append(to_name)
def set_from(self, from_email):
name, email = rfc822.parseaddr(from_email.replace(',', ''))
if email:
self.from_email = email
if name:
self.set_from_name(name)
def set_from_name(self, from_name):
self.from_name = from_name
def set_subject(self, subject):
self.subject = subject
def set_text(self, text):
self.text = text
def set_html(self, html):
self.html = html
def add_bcc(self, bcc):
email = rfc822.parseaddr(bcc.replace(',', ''))[1]
self.bcc.append(email)
def set_replyto(self, replyto):
self.reply_to = replyto
def add_attachment(self, name, filepath):
self.files[name] = open(filepath, "r").read()
def add_attachment_stream(self, name, string):
if isinstance(string, str):
self.files[name] = string
elif isinstance(string, io.BytesIO):
self.files[name] = string.read()
elif sys.hexversion < 0x03000000 and isinstance(string, unicode):
self.files[name] = string
def set_headers(self, headers):
self.headers = headers
def set_date(self, date):
self.date = date