Skip to content

Commit e07db8d

Browse files
Initial working version of Send a Single Email to a Single Recipient
1 parent 1ac58c6 commit e07db8d

10 files changed

Lines changed: 170 additions & 9 deletions

File tree

live_test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Send a Single Email to a Single Recipient
2+
import os
3+
from sendgrid import SendGridAPIClient # import sendgrid
4+
from sendgrid.helpers.mail import Mail, From, To, Subject, PlainTextContent, HtmlContent, SendGridException # from sendgrid.helpers.mail
5+
6+
msg = Mail(from_email=From('dx@sendgrid.com', 'DX'),
7+
to_emails=To('elmer.thomas@sendgrid.com', 'Elmer Thomas'),
8+
subject=Subject('Sending with SendGrid is Fun'),
9+
plain_text_content=PlainTextContent('and easy to do anywhere, even with Python'),
10+
html_content=HtmlContent('<strong>and easy to do anywhere, even with Python</strong>'))
11+
12+
try:
13+
sg_client = SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
14+
response = sg_client.send(request_body=msg)
15+
print(response.status_code)
16+
print(response.body)
17+
print(response.headers)
18+
except SendGridException as e:
19+
print(e.message)

proposals/mail-helper-refactor.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ msg = Mail(from_email=From('from@example.com', 'From Name'),
1616
html_content=HtmlContent('<strong>and easy to do anywhere, even with Python</strong>'))
1717

1818
try:
19-
response = sendgrid.send(msg, apikey=os.environ.get('SENDGRID_apikey'))
19+
sg_client = SendGridAPIClient()
20+
response = sg_client.send(msg, apikey=os.environ.get('SENDGRID_apikey'))
2021
print(response.status_code)
2122
print(response.body)
2223
print(response.headers)

sendgrid/helpers/mail/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,21 @@
99
from .email import Email
1010
from .exceptions import SendGridException, APIKeyIncludedException
1111
from .footer_settings import FooterSettings
12+
from .from_email import From
1213
from .ganalytics import Ganalytics
1314
from .header import Header
15+
from .html_content import HtmlContent
1416
from .mail_settings import MailSettings
1517
from .mail import Mail
1618
from .open_tracking import OpenTracking
1719
from .personalization import Personalization
20+
from .plain_text_content import PlainTextContent
1821
from .sandbox_mode import SandBoxMode
1922
from .section import Section
2023
from .spam_check import SpamCheck
24+
from .subject import Subject
2125
from .subscription_tracking import SubscriptionTracking
2226
from .substitution import Substitution
2327
from .tracking_settings import TrackingSettings
28+
from .to_email import To
2429
from .validators import ValidateAPIKey
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .email import Email
2+
3+
class From(Email):
4+
"""A from email address with an optional name."""
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from .content import Content
2+
from .validators import ValidateAPIKey
3+
4+
class HtmlContent(Content):
5+
"""HTML content to be included in your email.
6+
"""
7+
8+
def __init__(self, value):
9+
"""Create a HtmlContent with the specified MIME type and value.
10+
11+
:param value: The actual HTML content.
12+
:type value: string, optional
13+
"""
14+
self._type = "text/html"
15+
self._value = value
16+
self._validator = ValidateAPIKey()
17+
18+
@property
19+
def value(self):
20+
"""The actual HTML content.
21+
22+
:rtype: string
23+
"""
24+
return self._value
25+
26+
@value.setter
27+
def value(self, value):
28+
self._validator.validate_message_dict(value)
29+
self._value = value
30+
31+
def get(self):
32+
"""
33+
Get a JSON-ready representation of this HtmlContent.
34+
35+
:returns: This HtmlContent, ready for use in a request body.
36+
:rtype: dict
37+
"""
38+
content = {}
39+
content["type"] = "text/html"
40+
content["value"] = self._value
41+
return content

sendgrid/helpers/mail/mail.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
from .personalization import Personalization
33
from .header import Header
44

5-
65
class Mail(object):
76
"""A request to be sent with the SendGrid v3 Mail Send API (v3/mail/send).
87
98
Use get() to get the request body.
109
"""
1110
def __init__(self,
1211
from_email=None,
12+
to_emails=None,
1313
subject=None,
14-
to_email=None,
15-
content=None):
14+
plain_text_content=None,
15+
html_content=None):
1616
"""Create a Mail object.
1717
1818
If any parameters are not supplied, they must be set after initialization.
@@ -47,15 +47,18 @@ def __init__(self,
4747
self.from_email = from_email
4848

4949
if subject:
50-
self.subject = subject
50+
self.subject = subject.get()
5151

52-
if to_email:
52+
if to_emails:
5353
personalization = Personalization()
54-
personalization.add_to(to_email)
54+
personalization.add_to(to_emails)
5555
self.add_personalization(personalization)
5656

57-
if content:
58-
self.add_content(content)
57+
if plain_text_content:
58+
self.add_content(plain_text_content)
59+
60+
if html_content:
61+
self.add_content(html_content)
5962

6063
def __str__(self):
6164
"""Get a JSON representation of this Mail request.
@@ -395,3 +398,4 @@ def add_custom_arg(self, custom_arg):
395398
if self._custom_args is None:
396399
self._custom_args = []
397400
self._custom_args.append(custom_arg)
401+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from .content import Content
2+
from .validators import ValidateAPIKey
3+
4+
class PlainTextContent(Content):
5+
"""Plain text content to be included in your email.
6+
"""
7+
8+
def __init__(self, value):
9+
"""Create a PlainTextContent with the specified MIME type and value.
10+
11+
:param value: The actual text content.
12+
:type value: string, optional
13+
"""
14+
self._type = "text/plain"
15+
self._value = value
16+
self._validator = ValidateAPIKey()
17+
18+
@property
19+
def value(self):
20+
"""The actual text content.
21+
22+
:rtype: string
23+
"""
24+
return self._value
25+
26+
@value.setter
27+
def value(self, value):
28+
self._validator.validate_message_dict(value)
29+
self._value = value
30+
31+
def get(self):
32+
"""
33+
Get a JSON-ready representation of this PlainTextContent.
34+
35+
:returns: This PlainTextContent, ready for use in a request body.
36+
:rtype: dict
37+
"""
38+
content = {}
39+
content["type"] = "text/plain"
40+
content["value"] = self._value
41+
return content

sendgrid/helpers/mail/subject.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Subject(object):
2+
"""A subject for an email message."""
3+
4+
def __init__(self, subject):
5+
"""Create a Subjuct.
6+
7+
:param subject: The subject for an email
8+
:type subject: string
9+
"""
10+
self._subject = subject
11+
12+
@property
13+
def subject(self):
14+
"""The subject of an email.
15+
16+
:rtype: string
17+
"""
18+
return self._subject
19+
20+
@subject.setter
21+
def subject(self, value):
22+
self._subject = value
23+
24+
def __str__(self):
25+
"""Get a JSON representation of this Mail request.
26+
27+
:rtype: string
28+
"""
29+
return str(self.get())
30+
31+
def get(self):
32+
"""
33+
Get a JSON-ready representation of this Subject.
34+
35+
:returns: This Subject, ready for use in a request body.
36+
:rtype: string
37+
"""
38+
return self._subject

sendgrid/helpers/mail/to_email.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .email import Email
2+
3+
class To(Email):
4+
"""A to email address with an optional name."""

sendgrid/sendgrid.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,7 @@ def api_key(self):
103103
@api_key.setter
104104
def api_key(self, value):
105105
self.apikey = value
106+
107+
def send(self, request_body):
108+
response = self.client.mail.send.post(request_body=request_body.get())
109+
return response

0 commit comments

Comments
 (0)