Skip to content

Commit 0026089

Browse files
Send a Single Email to a Single Recipient
1 parent 9655794 commit 0026089

File tree

5 files changed

+41
-33
lines changed

5 files changed

+41
-33
lines changed

TROUBLESHOOTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ When debugging or testing, it may be useful to examine the raw request body to c
105105
You can do this right before you call `response = sg.client.mail.send.post(request_body=mail.get())` like so:
106106

107107
```python
108-
print mail.get()
108+
print(json.dumps(message.get(), sort_keys=True, indent=4))
109109
```
110110

111111
<a name="error-handling"></a>

live_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Send a Single Email to a Single Recipient
22
import os
3+
import json
34
from sendgrid import SendGridAPIClient
45
from sendgrid.helpers.mail import Mail, From, To, Subject, PlainTextContent, HtmlContent, SendGridException
56

@@ -11,6 +12,7 @@
1112

1213
try:
1314
sendgrid_client = SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
15+
print(json.dumps(message.get(), sort_keys=True, indent=4))
1416
response = sendgrid_client.send(message=message)
1517
print(response.status_code)
1618
print(response.body)
@@ -21,4 +23,3 @@
2123
# ToDo
2224

2325
## The Mail constructor should also support passing in tuples and strings
24-
## The send function parameter should be better named, maybe email_message or simply message

sendgrid/helpers/mail/html_content.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ def __init__(self, value = None):
1717
if value is not None:
1818
self.value = value
1919

20+
@property
21+
def type(self):
22+
"""The actual text content.
23+
24+
:rtype: string
25+
"""
26+
return "text/html"
27+
2028
@property
2129
def value(self):
2230
"""The actual HTML content.
@@ -38,6 +46,6 @@ def get(self):
3846
:rtype: dict
3947
"""
4048
content = {}
41-
content["type"] = "text/html"
42-
content["value"] = self._value
49+
content["type"] = self.type
50+
content["value"] = self.value
4351
return content

sendgrid/helpers/mail/mail.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,25 @@
88
class Mail(object):
99
"""Creates the response body for v3/mail/send"""
1010
def __init__(
11-
self, from_email=None, subject=None, to_email=None, content=None):
11+
self,
12+
from_email=None,
13+
subject=None,
14+
to_emails=None,
15+
plain_text_content=None,
16+
html_content=None
17+
):
1218
"""Create Mail object
1319
1420
:param from_email: The email address of the sender
15-
:type from_email: string, optional
21+
:type from_email: From, optional
1622
:param subject: The subject of the email
17-
:type subject: string, optional
18-
:param to_email: The email address of the recipient
19-
:type to_email: string, optional
20-
:param content: The body of the email
21-
:type content: string, optional
23+
:type subject: Subject, optional
24+
:param to_emails: The email address of the recipient
25+
:type to_emails: string, optional
26+
:param plain_text_content: The plain text body of the email
27+
:type plain_text_content: string, optional
28+
:param html_content: The html body of the email
29+
:type html_content: string, optional
2230
"""
2331
self._attachments = None
2432
self._categories = None
@@ -43,12 +51,14 @@ def __init__(
4351
self.from_email = from_email
4452
if subject is not None:
4553
self.subject = subject
46-
if to_email is not None:
54+
if to_emails is not None:
4755
personalization = Personalization()
48-
personalization.add_to(to_email)
56+
personalization.add_to(to_emails)
4957
self.add_personalization(personalization)
50-
if content is not None:
51-
self.add_content(content)
58+
if plain_text_content is not None:
59+
self.add_content(plain_text_content)
60+
if html_content is not None:
61+
self.add_content(html_content)
5262

5363
def __str__(self):
5464
return str(self.get())
@@ -97,7 +107,7 @@ def contents(self):
97107

98108
def add_content(self, content):
99109
# Text content should be before HTML content
100-
if content._type == "text/plain":
110+
if content.type == "text/plain":
101111
self._contents = self._ensure_insert(content, self._contents)
102112
else:
103113
self._contents = self._ensure_append(content, self._contents)
@@ -214,7 +224,7 @@ def get(self):
214224
"""
215225
mail = {
216226
'from': self._get_or_none(self.from_email),
217-
'subject': self.subject,
227+
'subject': self._get_or_none(self.subject),
218228
'personalizations': [p.get() for p in self.personalizations or []],
219229
'content': [c.get() for c in self.contents or []],
220230
'attachments': [a.get() for a in self.attachments or []],
@@ -245,7 +255,7 @@ def from_EmailMessage(cls, message):
245255
mail = cls(
246256
from_email=Email(message.get('From')),
247257
subject=message.get('Subject'),
248-
to_email=Email(message.get('To')),
258+
to_emails=Email(message.get('To')),
249259
)
250260
try:
251261
body = message.get_content()

sendgrid/helpers/mail/plain_text_content.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,17 @@ class PlainTextContent(Content):
66
"""Plain text content to be included in your email.
77
"""
88

9-
def __init__(self, value, validator=None):
9+
def __init__(self, value):
1010
"""Create a PlainTextContent with the specified MIME type and value.
1111
1212
:param value: The actual text content.
1313
"""
1414
self._value = None
15-
self._validator = None
15+
self._validator = ValidateAPIKey()
1616

1717
if value is not None:
1818
self.value = value
19-
if validator is not None:
20-
self.validator = validator
21-
else:
22-
self.validator = ValidateAPIKey()
19+
2320

2421
@property
2522
def type(self):
@@ -39,17 +36,9 @@ def value(self):
3936

4037
@value.setter
4138
def value(self, value):
42-
self.validator.validate_message_dict(value)
39+
self._validator.validate_message_dict(value)
4340
self._value = value
4441

45-
@property
46-
def validator(self):
47-
return self._validator
48-
49-
@validator.setter
50-
def validator(self, value):
51-
self._validator = value
52-
5342
def get(self):
5443
"""
5544
Get a JSON-ready representation of this PlainTextContent.

0 commit comments

Comments
 (0)