Skip to content

Commit c2b5508

Browse files
Merge pull request #1 from ryan-lydz/main
Adding AMP functionality
2 parents aa9cd17 + f41cdb5 commit c2b5508

File tree

11 files changed

+260
-5
lines changed

11 files changed

+260
-5
lines changed

python-examples/basic/basic_send_complex.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@
2121
"</body></html>"
2222
message.plain_text_body = "This is the Plain Text Body of my message."
2323

24+
# Setting AMP Body
25+
message.setAmpBody = "<!doctype html>" \
26+
"<html amp4email>" \
27+
" <head>" \
28+
" <meta charset=\"utf-8\">" \
29+
" <script async src=\"https://cdn.ampproject.org/v0.js\"></script>" \
30+
" <style amp4email-boilerplate>body{visibility:hidden}</style>" \
31+
" <style amp-custom>" \
32+
" h1 {" \
33+
" margin: 1rem;" \
34+
" }" \
35+
" </style>" \
36+
" </head>" \
37+
" <body>" \
38+
" <h1>This is the AMP Html Body of my message</h1>" \
39+
" </body>" \
40+
"</html>"
41+
2442
message.from_email_address = EmailAddress("[email protected]")
2543
message.reply_to_email_address = EmailAddress("[email protected]")
2644

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import json
2+
import os
3+
4+
from socketlabs.injectionapi import SocketLabsClient
5+
from socketlabs.injectionapi.message.__imports__ import \
6+
BasicMessage, EmailAddress
7+
8+
# For more information on AMP Html, visit the following link: amp.dev/documentation
9+
10+
# build the message
11+
message = BasicMessage()
12+
13+
message.subject = "Sending A Test Message (Basic Send)"
14+
15+
message.html_body = "<html><body>" \
16+
"<h1>This HTML will show if AMP is not supported on the receiving end of the email.</h1>" \
17+
"<p>This is the Html Body of my message.</p>" \
18+
"</body></html>"
19+
message.amp_body = "<!doctype html>" \
20+
"<html amp4email>" \
21+
"<head>" \
22+
" <meta charset=\"utf-8\">" \
23+
" <script async src=\"https://cdn.ampproject.org/v0.js\"></script>" \
24+
" <style amp4email-boilerplate>body{visibility:hidden}</style>" \
25+
" <style amp-custom>" \
26+
" h1 {" \
27+
" margin: 1rem;" \
28+
" }" \
29+
" </style>" \
30+
"</head>" \
31+
"<body>" \
32+
" <h1>This is the AMP Html Body of my message</h1>" \
33+
"</body>" \
34+
"</html>"
35+
36+
message.from_email_address = EmailAddress("[email protected]")
37+
message.add_to_email_address("[email protected]")
38+
message.add_to_email_address("[email protected]", "Recipient #2")
39+
message.add_to_email_address(EmailAddress("[email protected]"))
40+
message.add_to_email_address(EmailAddress("[email protected]", "Recipient #4"))
41+
42+
43+
# get credentials from environment variables
44+
server_id = int(os.environ.get('SOCKETLABS_SERVER_ID'))
45+
api_key = os.environ.get('SOCKETLABS_INJECTION_API_KEY')
46+
47+
# create the client
48+
client = SocketLabsClient(server_id, api_key)
49+
50+
# send the message
51+
response = client.send(message)
52+
53+
print(json.dumps(response.to_json(), indent=2))

python-examples/bulk/bulk_send_complex.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
message.charset = "UTF-8"
1616

17-
message.subject = "Sending A Complex Test Message"
17+
message.subject = "Sending A Complex Bulk Test Message"
1818
message.html_body = "<html>" \
19-
" <head><title>Sending A Complex Test Message</title></head>" \
19+
" <head><title>Sending A Complex Bulk Test Message</title></head>" \
2020
" <body>" \
2121
" <h1>Sending A Complex Test Message</h1>" \
2222
" <h2>Merge Data</h2>" \
@@ -37,7 +37,7 @@
3737
" <p><img src='cid:bus' /></p>" \
3838
" </body>" \
3939
"</html>"
40-
message.plain_text_body = "Sending A Complex Test Message" \
40+
message.plain_text_body = "Sending A Complex Bulk Test Message" \
4141
" Merged Data" \
4242
" Motto = %%Motto%%" \
4343
" Birthday = %%Birthday%%" \
@@ -47,6 +47,37 @@
4747
" Example of Merge Usage" \
4848
" Our company motto is '%%Motto%%'." \
4949
" Your birthday is %%Birthday%% and you are %%Age%% years old."
50+
message.amp_body = "<!doctype html>"\
51+
"<html amp4email>" \
52+
"<head>"\
53+
"<title>Sending an AMP Test Message</title>"\
54+
" <meta charset=\"utf-8\">"\
55+
" <script async src=\"https://cdn.ampproject.org/v0.js\"></script>"\
56+
" <style amp4email-boilerplate>body{visibility:hidden}</style>"\
57+
" <style amp-custom>"\
58+
" h1 {"\
59+
" margin: 1rem;"\
60+
" }"\
61+
" </style>"\
62+
"</head>"\
63+
"<body>"\
64+
" <h1>Sending An AMP Complex Test Message</h1>"\
65+
" <h2>Merge Data</h2>"\
66+
" <p>"\
67+
" Motto = <b>%%Motto%%</b> </br>"\
68+
" Birthday = <b>%%Birthday%%</b> </br>"\
69+
" Age = <b>%%Age%%</b> </br>"\
70+
" UpSell = <b>%%UpSell%%</b>"\
71+
" </p>"\
72+
" <h2>Example of Merge Usage</h2>"\
73+
" <p>"\
74+
" Our company motto is '<b>%%Motto%%</b>'. </br>"\
75+
" Your birthday is <b>%%Birthday%%</b> and you are <b>%%Age%%</b> years old."\
76+
" </p>"\
77+
" <h2>UTF-8 Characters:</h2>"\
78+
" <p>✔ - Check</p>"\
79+
" </body>"\
80+
" </html>"
5081

5182
message.from_email_address = EmailAddress("[email protected]", "FromMe")
5283
message.reply_to_email_address = EmailAddress("[email protected]")
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import json
2+
import os
3+
4+
from socketlabs.injectionapi import SocketLabsClient
5+
from socketlabs.injectionapi.message.__imports__ import \
6+
BulkMessage, BulkRecipient, EmailAddress
7+
8+
# For more information on AMP Html, visit the following link: amp.dev/documentation
9+
10+
# build the message
11+
message = BulkMessage()
12+
13+
14+
message.subject = "Sending A Test Message (Bulk Send)"
15+
16+
message.html_body = "<html><body>" \
17+
"<h1>This HTML will show if AMP is not supported on the receiving end of the email.</h1>" \
18+
"<p>This is the Html Body of my message.</p>" \
19+
"</body></html>"
20+
message.amp_body = "<!doctype html>" \
21+
"<html amp4email>" \
22+
"<head>" \
23+
" <meta charset=\"utf-8\">" \
24+
" <script async src=\"https://cdn.ampproject.org/v0.js\"></script>" \
25+
" <style amp4email-boilerplate>body{visibility:hidden}</style>" \
26+
" <style amp-custom>" \
27+
" h1 {" \
28+
" margin: 1rem;" \
29+
" }" \
30+
" </style>" \
31+
"</head>" \
32+
"<body>" \
33+
" <h1>This is the AMP Html Body of my message</h1>" \
34+
"</body>" \
35+
"</html>"
36+
37+
message.from_email_address = EmailAddress("[email protected]")
38+
message.add_to_recipient("[email protected]")
39+
message.add_to_recipient("[email protected]", "Recipient #2")
40+
message.add_to_recipient(BulkRecipient("[email protected]"))
41+
message.add_to_recipient(BulkRecipient("[email protected]", "Recipient #4"))
42+
43+
44+
# get credentials from environment variables
45+
server_id = int(os.environ.get('SOCKETLABS_SERVER_ID'))
46+
api_key = os.environ.get('SOCKETLABS_INJECTION_API_KEY')
47+
48+
# create the client
49+
client = SocketLabsClient(server_id, api_key)
50+
51+
# send the message
52+
response = client.send(message)
53+
54+
print(json.dumps(response.to_json(), indent=2))

socketlabs/injectionapi/core/injectionrequestfactory.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ def generate_base_message(message: MessageBase):
142142
message_json.subject = message.subject
143143
message_json.plain_text_body = message.plain_text_body
144144
message_json.html_body = message.html_body
145+
message_json.amp_body = message.amp_body
145146
message_json.mailing_id = message.mailing_id
146147
message_json.message_id = message.message_id
147148
message_json.charset = message.charset

socketlabs/injectionapi/core/sendvalidator.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ def has_subject(message: MessageBase):
5555

5656
def has_message_body(message: MessageBase):
5757
"""
58-
Check if the message has a valid From Email Address
58+
Check if the message has a Message Body.
59+
If an Api Template is specified it will override the HtmlBody, AmpBody, and/or the textBody.
60+
If no Api Template is specified the HtmlBody, AmpBody, and/or the textBody must be set
5961
:param message: message to validate
6062
:type message: MessageBase
6163
:return the result

socketlabs/injectionapi/core/serialization/messagejson.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def __init__(self):
1414
self._subject = None
1515
self._plain_text_body = None
1616
self._html_body = None
17+
self._amp_body = None
1718
self._api_template = None
1819
self._mailing_id = None
1920
self._message_id = None
@@ -147,6 +148,24 @@ def html_body(self, val: str):
147148
"""
148149
self._html_body = val
149150

151+
@property
152+
def amp_body(self):
153+
"""
154+
Get the AMP portion of the message body.
155+
:return the AMP body
156+
:rtype str
157+
"""
158+
return self._amp_body
159+
160+
@amp_body.setter
161+
def amp_body(self, val: str):
162+
"""
163+
Set the AMP portion of the message body.
164+
:param val: the AMP body
165+
:type val: str
166+
"""
167+
self._amp_body = val
168+
150169
@property
151170
def api_template(self):
152171
"""
@@ -353,6 +372,9 @@ def to_json(self):
353372
if self.html_body is not None and self.html_body.strip() != '':
354373
json["htmlBody"] = self.html_body
355374

375+
if self.amp_body is not None and self._amp_body.strip() != '':
376+
json["ampBody"] = self.amp_body
377+
356378
if self.plain_text_body is not None and self.plain_text_body.strip() != '':
357379
json["textBody"] = self.plain_text_body
358380

socketlabs/injectionapi/message/basicmessage.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def __init__(self):
3535
self._subject = None
3636
self._plain_text_body = None
3737
self._html_body = None
38+
self._amp_body = None
3839
self._api_template = None
3940
self._mailing_id = None
4041
self._message_id = None
@@ -181,6 +182,7 @@ def subject(self, val: str):
181182
def plain_text_body(self):
182183
"""
183184
Get the plain text portion of the message body.
185+
(Optional) Either TextBody or HtmlBody must be used with the AmpBody or use a ApiTemplate
184186
:return the plain text body
185187
:rtype str
186188
"""
@@ -190,6 +192,7 @@ def plain_text_body(self):
190192
def plain_text_body(self, val: str):
191193
"""
192194
Set the plain text portion of the message body.
195+
(Optional) Either TextBody or HtmlBody must be used with the AmpBody or use a ApiTemplate
193196
:param val: the plain text body
194197
:type val: str
195198
"""
@@ -199,6 +202,7 @@ def plain_text_body(self, val: str):
199202
def html_body(self):
200203
"""
201204
Get the HTML portion of the message body.
205+
(Optional) Either TextBody or HtmlBody must be used with the AmpBody or use a ApiTemplate
202206
:return the HTML body
203207
:rtype str
204208
"""
@@ -208,15 +212,37 @@ def html_body(self):
208212
def html_body(self, val: str):
209213
"""
210214
Set the HTML portion of the message body.
215+
(Optional) Either TextBody or HtmlBody must be used with the AmpBody or use a ApiTemplate
211216
:param val: the HTML body
212217
:type val: str
213218
"""
214219
self._html_body = val
215220

221+
@property
222+
def amp_body(self):
223+
"""
224+
Get the AMP portion of the message body.
225+
(Optional) Either TextBody or HtmlBody must be used with the AmpBody or use a ApiTemplate
226+
:return the AMP body
227+
:rtype str
228+
"""
229+
return self._amp_body
230+
231+
@amp_body.setter
232+
def amp_body(self, val: str):
233+
"""
234+
Set the AMP portion of the message body.
235+
(Optional) Either TextBody or HtmlBody must be used with the AmpBody or use a ApiTemplate
236+
:param val: the AMP body
237+
:type val: str
238+
"""
239+
self._amp_body = val
240+
216241
@property
217242
def api_template(self):
218243
"""
219244
Get the api template.
245+
(Optional) Either TextBody or HtmlBody must be used with the AmpBody or use a ApiTemplate
220246
:return the api template
221247
:rtype str
222248
"""
@@ -226,6 +252,7 @@ def api_template(self):
226252
def api_template(self, val: str):
227253
"""
228254
Set the api template.
255+
(Optional) Either TextBody or HtmlBody must be used with the AmpBody or use a ApiTemplate
229256
:param val: the api template
230257
:rtype str
231258
"""

0 commit comments

Comments
 (0)