Skip to content

Commit b883351

Browse files
Merge pull request #1 from ryan-lydz/main
Adding AMP functionality
2 parents 0534445 + f4aa87a commit b883351

File tree

8 files changed

+175
-20
lines changed

8 files changed

+175
-20
lines changed

examples/basic/BasicComplex/BasicSendComplex.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package main
33
import (
44
"fmt"
55

6-
"github.com/socketlabs/socketlabs-go/examples"
6+
exampleconfig "github.com/socketlabs/socketlabs-go/examples"
77
"github.com/socketlabs/socketlabs-go/injectionapi"
88
"github.com/socketlabs/socketlabs-go/injectionapi/message"
99
)
@@ -25,9 +25,26 @@ func main() {
2525
//Set reply to
2626
basic.ReplyTo = message.EmailAddress{EmailAddress: "replyto@example.com", FriendlyName: "Reply Address"}
2727

28-
//set html and text body parts
29-
basic.HtmlBody = "<body><p><strong>Lorem Ipsum</strong></p><br /><img src=\"cid:Bus\" /></body>"
28+
//set html, amp, and text body parts
29+
basic.HtmlBody = "<body><p><strong>Html Body</strong></p><br /><img src=\"cid:Bus\" /></body>"
3030
basic.PlainTextBody = "Lorem Ipsum"
31+
basic.AmpBody = "<!doctype html>" +
32+
"<html amp4email>" +
33+
" <head>" +
34+
" <meta charset=\"utf-8\">" +
35+
" <script async src=\"https://cdn.ampproject.org/v0.js\"></script>" +
36+
" <style amp4email-boilerplate>body{visibility:hidden}</style>" +
37+
" <style amp-custom>" +
38+
" h1 {" +
39+
" margin: 1rem;" +
40+
" }" +
41+
" </style>" +
42+
" </head>" +
43+
" <body>" +
44+
" <h1>This is the AMP Html Body of my message</h1>" +
45+
" </body>" +
46+
"</html>"
47+
3148
basic.CharSet = "utf-8"
3249

3350
//Tag message with mailing and message ids.
@@ -38,12 +55,6 @@ func main() {
3855
//Configure custom message headers
3956
basic.CustomHeaders = append(basic.CustomHeaders, message.NewCustomHeader("MyMessageHeader", "I am a message header"))
4057

41-
//Add an attachment
42-
attachment, _ := message.NewAttachment("../../Img/bus.png")
43-
attachment.ContentID = "bus" // specify the cid as 'Bus' so it can be embedded
44-
attachment.CustomHeaders = append(attachment.CustomHeaders, message.CustomHeader{Name: "MyAttachmentHeader", Value: "I am an attachment header"})
45-
basic.Attachments = append(basic.Attachments, attachment)
46-
4758
//Send the message
4859
sendResponse, _ := client.SendBasic(&basic)
4960

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
exampleconfig "github.com/socketlabs/socketlabs-go/examples"
7+
"github.com/socketlabs/socketlabs-go/injectionapi"
8+
"github.com/socketlabs/socketlabs-go/injectionapi/message"
9+
)
10+
11+
// For more info on AMP Html, visit: https://amp.dev/
12+
func main() {
13+
client := injectionapi.CreateClient(exampleconfig.ServerId(), exampleconfig.APIKey())
14+
15+
basic := message.BasicMessage{}
16+
17+
basic.Subject = "Sending a Basic AMP Message"
18+
basic.HtmlBody = "<html>This HTML will show if AMP is not supported on the receiving end of the email.</html>"
19+
basic.AmpBody = "<!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+
basic.From = message.EmailAddress{EmailAddress: "from@example.com"}
37+
basic.ReplyTo = message.EmailAddress{EmailAddress: "replyto@example.com"}
38+
39+
//A basic message supports up to 50 recipients and supports several different ways to add recipients
40+
basic.AddToEmailAddress("recipient1@example.com") //Add a To address by passing the email address
41+
basic.AddCcEmailAddress("recipient2@example.com") //Add a CC address by passing the email address and a friendly name
42+
basic.AddBccEmailAddress("recipient3@example.com") //Add a BCC address by passing the email address
43+
44+
//Send the message
45+
sendResponse, _ := client.SendBasic(&basic)
46+
47+
//Output the results
48+
fmt.Println(sendResponse.Result.ToString())
49+
fmt.Println(sendResponse.ResponseMessage)
50+
}

examples/bulk/BulkComplex/BulkComplex.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"bytes"
55
"fmt"
66

7-
"github.com/socketlabs/socketlabs-go/examples"
7+
exampleconfig "github.com/socketlabs/socketlabs-go/examples"
88
"github.com/socketlabs/socketlabs-go/injectionapi"
99
"github.com/socketlabs/socketlabs-go/injectionapi/message"
1010
)
@@ -68,13 +68,42 @@ func main() {
6868
htmlBody.WriteString("</html>")
6969
bulk.HtmlBody = htmlBody.String()
7070

71+
var ampBody bytes.Buffer
72+
ampBody.WriteString("<!doctype html>")
73+
ampBody.WriteString("<html amp4email>")
74+
ampBody.WriteString("<head>")
75+
ampBody.WriteString("<title>Sending an Bulk AMP Message</title>")
76+
ampBody.WriteString(" <meta charset=\"utf-8\">")
77+
ampBody.WriteString(" <script async src=\"https://cdn.ampproject.org/v0.js\"></script>")
78+
ampBody.WriteString(" <style amp4email-boilerplate>body{visibility:hidden}</style>")
79+
ampBody.WriteString(" <style amp-custom>")
80+
ampBody.WriteString(" h1 {")
81+
ampBody.WriteString(" margin: 1rem;")
82+
ampBody.WriteString(" }")
83+
ampBody.WriteString(" </style>")
84+
ampBody.WriteString("</head>")
85+
ampBody.WriteString("<body>")
86+
ampBody.WriteString(" <h1>Sending An AMP Complex Test Message</h1>")
87+
ampBody.WriteString(" <h2>Merge Data</h2>")
88+
ampBody.WriteString(" <p>")
89+
ampBody.WriteString(" Motto = <b>%%Motto%%</b> </br>")
90+
ampBody.WriteString(" Birthday = <b>%%Birthday%%</b> </br>")
91+
ampBody.WriteString(" Age = <b>%%Age%%</b> </br>")
92+
ampBody.WriteString(" UpSell = <b>%%UpSell%%</b>")
93+
ampBody.WriteString(" </p>")
94+
ampBody.WriteString(" <h2>Example of Merge Usage</h2>")
95+
ampBody.WriteString(" <p>")
96+
ampBody.WriteString(" Our company motto is '<b>%%Motto%%</b>'. </br>")
97+
ampBody.WriteString(" Your birthday is <b>%%Birthday%%</b> and you are <b>%%Age%%</b> years old.")
98+
ampBody.WriteString(" </p>")
99+
ampBody.WriteString(" <h2>UTF-8 Characters:</h2>")
100+
ampBody.WriteString(" <p>✔ - Check</p>")
101+
ampBody.WriteString(" </body>")
102+
ampBody.WriteString(" </html>")
103+
bulk.AmpBody = ampBody.String()
71104
//skipping plain text for this example
72105
//bulk.PlainTextBody = "Some Text"
73106

74-
attachment, _ := message.NewAttachment("../../Img/bus.png")
75-
attachment.AddCustomHeader("Atachment-Header", "I Am A Bus")
76-
bulk.Attachments = append(bulk.Attachments, attachment)
77-
78107
//Send the message
79108
sendResponse, _ := client.SendBulk(&bulk)
80109

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
exampleconfig "github.com/socketlabs/socketlabs-go/examples"
7+
"github.com/socketlabs/socketlabs-go/injectionapi"
8+
"github.com/socketlabs/socketlabs-go/injectionapi/message"
9+
)
10+
11+
// For more info on AMP Html, visit: https://amp.dev/
12+
func main() {
13+
14+
client := injectionapi.CreateClient(exampleconfig.ServerId(), exampleconfig.APIKey())
15+
16+
//Build the message
17+
bulk := message.BulkMessage{}
18+
19+
bulk.Subject = "Sending A Bulk AMP Message"
20+
bulk.HtmlBody = "<html>This HTML will show if AMP is not supported on the receiving end of the email.</html>"
21+
bulk.AmpBody = "<!doctype html>" +
22+
"<html amp4email>" +
23+
" <head>" +
24+
" <meta charset=\"utf-8\">" +
25+
" <script async src=\"https://cdn.ampproject.org/v0.js\"></script>" +
26+
" <style amp4email-boilerplate>body{visibility:hidden}</style>" +
27+
" <style amp-custom>" +
28+
" h1 {" +
29+
" margin: 1rem;" +
30+
" }" +
31+
" </style>" +
32+
" </head>" +
33+
" <body>" +
34+
" <h1>This is the AMP Html Body of my message</h1>" +
35+
" </body>" +
36+
"</html>"
37+
bulk.From = message.EmailAddress{EmailAddress: "from@example.com"}
38+
bulk.ReplyTo = message.EmailAddress{EmailAddress: "replyto@example.com"}
39+
40+
//Add Bulk Recipients
41+
bulk.AddToFriendlyBulkRecipient("recipient1@example.com", "Recipient #1")
42+
bulk.AddToFriendlyBulkRecipient("recipient2@example.com", "Recipient #2")
43+
bulk.AddToFriendlyBulkRecipient("recipient3@example.com", "Recipient #3")
44+
bulk.AddToFriendlyBulkRecipient("recipient4@example.com", "Recipient #4")
45+
46+
//Send the message
47+
sendResponse, _ := client.SendBulk(&bulk)
48+
49+
//Output the results
50+
fmt.Println(sendResponse.Result.ToString())
51+
fmt.Println(sendResponse.ResponseMessage)
52+
}

injectionapi/core/injectionrequestfactory.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func (factory InjectionRequestFactory) GenerateBasicRequest(message *message.Bas
2424
Subject: message.Subject,
2525
TextBody: message.PlainTextBody,
2626
HTMLBody: message.HtmlBody,
27+
AMPBody: message.AmpBody,
2728
MailingID: message.MailingId,
2829
MessageID: message.MessageId,
2930
CharSet: message.CharSet,
@@ -72,6 +73,7 @@ func (factory InjectionRequestFactory) GenerateBulkRequest(message *message.Bulk
7273
Subject: message.Subject,
7374
TextBody: message.PlainTextBody,
7475
HTMLBody: message.HtmlBody,
76+
AMPBody: message.AmpBody,
7577
MailingID: message.MailingId,
7678
MessageID: message.MessageId,
7779
CharSet: message.CharSet,

injectionapi/core/serialization/messageJson.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ type MessageJson struct {
2828
// HTML portion of the message body.
2929
HTMLBody string `json:"HtmlBody,omitempty"`
3030

31+
//AMP portion of the message body.
32+
AMPBody string `json:"AmpBody,omitempty"`
33+
3134
// Api Template for the message.
3235
APITemplate string `json:"ApiTemplate,omitempty"`
3336

injectionapi/message/basicmessage.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@ type BasicMessage struct {
99
Subject string
1010

1111
// Plain text portion of the message body.
12-
// (Optional) Either PlainTextBody, HtmlBody, both or ApiTemplate must be used to set the body.
12+
// (Optional) Either TextBody or HtmlBody must be used with the AmpBody or use a ApiTemplate
1313
PlainTextBody string
1414

1515
// HTML portion of the message body.
16-
// (Optional) Either PlainTextBody, HtmlBody, both or ApiTemplate must be used to set the body.
16+
// (Optional) Either TextBody or HtmlBody must be used with the AmpBody or use a ApiTemplate
1717
HtmlBody string
1818

19+
// AMP portion of the message body.
20+
// (Optional) Either TextBody or HtmlBody must be used with the AmpBody or use a ApiTemplate
21+
AmpBody string
22+
1923
// Api Template for the message body.
20-
// (Optional) Either PlainTextBody, HtmlBody, both or ApiTemplate must be used to set the body.
24+
// (Optional) Either TextBody or HtmlBody must be used with the AmpBody or use a ApiTemplate
2125
ApiTemplate string
2226

2327
//Custom MailingId for the message.

injectionapi/message/bulkmessage.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@ type BulkMessage struct {
99
Subject string
1010

1111
// Plain text portion of the message body.
12-
// (Optional) Either PlainTextBody, HtmlBody, both or ApiTemplate must be used to set the body.
12+
// (Optional) Either TextBody or HtmlBody must be used with the AmpBody or use a ApiTemplate
1313
PlainTextBody string
1414

1515
// HTML portion of the message body.
16-
// (Optional) Either PlainTextBody, HtmlBody, both or ApiTemplate must be used to set the body.
16+
// (Optional) Either TextBody or HtmlBody must be used with the AmpBody or use a ApiTemplate
1717
HtmlBody string
1818

19+
// AMP portion of the message body.
20+
// (Optional) Either TextBody or HtmlBody must be used with the AmpBody or use a ApiTemplate
21+
AmpBody string
22+
1923
// Api Template for the message body.
20-
// (Optional) Either PlainTextBody, HtmlBody, both or ApiTemplate must be used to set the body.
24+
// (Optional) Either TextBody or HtmlBody must be used with the AmpBody or use a ApiTemplate
2125
ApiTemplate string
2226

2327
//Custom MailingId for the message.

0 commit comments

Comments
 (0)