|
| 1 | +[](https://www.socketlabs.com) |
| 2 | +# [](https://twitter.com/socketlabs) [](./LICENSE) [](https://github.com/socketlabs/socketlabs-go/blob/master/CONTRIBUTING.md) |
| 3 | + |
| 4 | +The SocketLabs Email Delivery Go package allows you to easily send email messages via the [SocketLabs Injection API](https://www.socketlabs.com/api-reference/injection-api/). The library makes it easy to build and send any type of message supported by the API, from a simple message to a single recipient all the way to a complex bulk message sent to a group of recipients with unique merge data per recipient. |
| 5 | + |
| 6 | +# Table of Contents |
| 7 | +* [Prerequisites and Installation](#prerequisites-and-installation) |
| 8 | +* [Getting Started](#getting-started) |
| 9 | +* [Managing API Keys](#managing-api-keys) |
| 10 | +* [Examples and Use Cases](#examples-and-use-cases) |
| 11 | +* [License](#license) |
| 12 | + |
| 13 | + |
| 14 | +<a name="prerequisites-and-installation"></a> |
| 15 | +# Prerequisites and Installation |
| 16 | +## Prerequisites |
| 17 | +* Go 1.2.2 or higher |
| 18 | +* A SocketLabs account. If you don't have one yet, you can [sign up for a free account](https://signup.socketlabs.com/step-1?plan=free) to get started. |
| 19 | + |
| 20 | +## Installation |
| 21 | +``` |
| 22 | +go get github.com/socketlabs/socketlabs-go/injectionapi |
| 23 | +``` |
| 24 | + |
| 25 | + |
| 26 | +<a name="getting-started"></a> |
| 27 | +# Getting Started |
| 28 | +## Obtaining your API Key and SocketLabs ServerId number |
| 29 | +In order to get started, you'll need to enable the Injection API feature in the [SocketLabs Control Panel](https://cp.socketlabs.com). |
| 30 | +Once logged in, navigate to your SocketLabs server's dashboard (if you only have one server on your account you'll be taken here immediately after logging in). |
| 31 | +Make note of your 4 or 5 digit ServerId number, as you'll need this along with |
| 32 | +your API key in order to use the Injection API. |
| 33 | + |
| 34 | +To enable the Injection API, click on the "For Developers" dropdown on the top-level navigation, then choose the "Configure HTTP Injection API" option. |
| 35 | +Once here, you can enable the feature by choosing the "Enabled" option in the |
| 36 | +dropdown. Enabling the feature will also generate your API key, which you'll |
| 37 | +need (along with your ServerId) to start using the API. Be sure to click the |
| 38 | +"Update" button to save your changes once you are finished. |
| 39 | + |
| 40 | +## Basic Message |
| 41 | +A basic message is an email message like you'd send from a personal email client such as Outlook. |
| 42 | +A basic message can have many recipients, including multiple To addresses, CC addresses, and even BCC addresses. |
| 43 | +You can also send a file attachment in a basic message. |
| 44 | + |
| 45 | +```GO |
| 46 | + |
| 47 | +import ( |
| 48 | + "github.com/socketlabs/socketlabs-go/injectionapi" |
| 49 | + "github.com/socketlabs/socketlabs-go/injectionapi/message" |
| 50 | +) |
| 51 | + |
| 52 | +func main() { |
| 53 | + client := injectionapi.CreateClient(000001, "YOUR-API-KEY") |
| 54 | + |
| 55 | + basic := message.BasicMessage{} |
| 56 | + |
| 57 | + basic.Subject = "Sending a Basic Message" |
| 58 | + basic.HtmlBody = "<html>This is the Html Body of my message.</html>" |
| 59 | + basic.PlainTextBody = "This is the Plain Text Body of my message." |
| 60 | + |
| 61 | + basic.From = message.EmailAddress{EmailAddress: "from@example.com"} |
| 62 | + basic.ReplyTo = message.EmailAddress{EmailAddress: "replyto@example.com"} |
| 63 | + |
| 64 | + //A basic message supports up to 50 recipients and supports several different ways to add recipients |
| 65 | + basic.AddToEmailAddress("recipient@example.com") //Add a To address by passing the email address |
| 66 | + basic.AddCcEmailAddress("recipient2@example.com") //Add a CC address by passing the email address and a friendly name |
| 67 | + basic.AddBccEmailAddress("recipient3@example.com") //Add a BCC address by passing the email address |
| 68 | + |
| 69 | + //Send the message |
| 70 | + _, _ = client.SendBasic(&basic) |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +## Bulk Message |
| 75 | +A bulk message usually contains a single recipient per message |
| 76 | +and is generally used to send the same content to many recipients, |
| 77 | +optionally customizing the message via the use of MergeData. |
| 78 | +For more information about using Merge data, please see the [Injection API documentation](https://www.socketlabs.com/api-reference/injection-api/#merging). |
| 79 | +```GO |
| 80 | +import ( |
| 81 | + "github.com/socketlabs/socketlabs-go/injectionapi" |
| 82 | + "github.com/socketlabs/socketlabs-go/injectionapi/message" |
| 83 | +) |
| 84 | + |
| 85 | +func main() { |
| 86 | + |
| 87 | + client := injectionapi.CreateClient(000001, "YOUR-API-KEY") |
| 88 | + |
| 89 | + //Build the message |
| 90 | + bulk := message.BulkMessage{} |
| 91 | + |
| 92 | + bulk.Subject = "Sending A Bulk Message" |
| 93 | + bulk.HtmlBody = "<html>This is the Html Body of my message sent to the recipient with %%EyeColor%% eyes.</html>"; |
| 94 | + bulk.PlainTextBody = "This is the Plain Text Body of my message sent to the recipient with %%EyeColor%% eyes."; |
| 95 | + bulk.From = message.EmailAddress{EmailAddress: "from@example.com"} |
| 96 | + bulk.ReplyTo = message.EmailAddress{EmailAddress: "replyto@example.com"} |
| 97 | + |
| 98 | + //Add Bulk Recipients |
| 99 | + recipient1 := bulk.AddToFriendlyBulkRecipient("recipient1@example.com", "Recipient #1") |
| 100 | + recipient1.AddMergeData("EyeColor", "Green") |
| 101 | + |
| 102 | + recipient2 := bulk.AddToFriendlyBulkRecipient("recipient2@example.com", "Recipient #2") |
| 103 | + recipient2.AddMergeData("EyeColor", "Blue") |
| 104 | + |
| 105 | + //Send the message |
| 106 | + _, _ = client.SendBulk(&bulk) |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +<a name="managing-api-keys"></a> |
| 111 | +## Managing API Keys |
| 112 | +For ease of demonstration, some of our examples may include the ServerId and API key directly in our code sample. Generally it is not considered a good practice to store sensitive information like this directly in your code. In most cases we recommend the use of [Environment Variables](https://flaviocopes.com/golang-environment-variables/). |
| 113 | + |
| 114 | +<a name="examples-and-use-cases"></a> |
| 115 | +# Examples and Use Cases |
| 116 | +In order to demonstrate the many possible use cases for the SDK, we've provided |
| 117 | +an assortment of code examples. These examples demonstrate many different |
| 118 | +features available to the Injection API and SDK, including using templates |
| 119 | +created in the [SocketLabs Email Designer](https://www.socketlabs.com/blog/introducing-new-email-designer/), custom email headers, sending |
| 120 | +attachments, sending content that is stored in an HTML file, advanced bulk |
| 121 | +merging, and even pulling recipients from a datasource. |
| 122 | + |
| 123 | + |
| 124 | +### [Basic send example](https://github.com/socketlabs/socketlabs-go/blob/master/examples/basic/Basic/BasicSend.go) |
| 125 | +This example demonstrates a Basic Send. |
| 126 | + |
| 127 | +### [Basic send complex example](https://github.com/socketlabs/socketlabs-go/blob/master/examples/basic/BasicComplex/BasicSendComplex.go) |
| 128 | +This example demonstrates many features of the Basic Send, including adding multiple recipients, adding message and mailing id's, and adding an embedded image. |
| 129 | + |
| 130 | +### [Basic send from HTML file](https://github.com/socketlabs/socketlabs-go/blob/master/examples/basic/BasicFromHtmlFile/BasicFromHtmlFile.go) |
| 131 | +This example demonstrates how to read in your HTML content from an HTML file |
| 132 | +rather than passing in a string directly. |
| 133 | + |
| 134 | +### [Basic send from SocketLabs API Template](https://github.com/socketlabs/socketlabs-go/blob/master/examples/basic/BasicWithApiTemplate/BasicWithApiTemplate.go) |
| 135 | +This example demonstrates the sending of a piece of content that was created in the |
| 136 | +SocketLabs Email Designer. This is also known as the [API Templates](https://www.socketlabs.com/blog/introducing-api-templates/) feature. |
| 137 | + |
| 138 | +### [Basic send with specified character set](https://github.com/socketlabs/socketlabs-go/blob/master/examples/basic/BasicWithAsciiCharset/BasicWithAsciiCharset.go) |
| 139 | +This example demonstrates sending with a specific character set. |
| 140 | + |
| 141 | +### [Basic send with file attachment](https://github.com/socketlabs/socketlabs-go/blob/master/examples/basic/BasicWithAttachment/BasicWithAttachment.go) |
| 142 | +This example demonstrates how to add a file attachment to your message. |
| 143 | + |
| 144 | +### [Basic send with custom email headers](https://github.com/socketlabs/socketlabs-go/blob/master/examples/basic/BasicWithCustomHeaders/BasicWithCustomHeaders.go) |
| 145 | +This example demonstrates how to add custom headers to your email message. |
| 146 | + |
| 147 | +### [Basic send with embedded image](https://github.com/socketlabs/socketlabs-go/blob/master/examples/basic/BasicWithEmbeddedImage/BasicWithEmbeddedImage.go) |
| 148 | +This example demonstrates how to embed an image in your message. |
| 149 | + |
| 150 | +### [Basic send with a web proxy](https://github.com/socketlabs/socketlabs-go/blob/master/examples/basic/BasicSendWithProxy/BasicSendWithProxy.go) |
| 151 | +This example demonstrates how to use a proxy with your HTTP client. |
| 152 | + |
| 153 | + |
| 154 | +### [Basic send with invalid file attachment](https://github.com/socketlabs/socketlabs-go/blob/master/examples/basic/Invalid/BasicWithInvalidAttachment/BasicWithInvalidAttachment.go) |
| 155 | +This example demonstrates the results of attempting to do a send with an invalid attachment. |
| 156 | + |
| 157 | +### [Basic send with invalid from address](https://github.com/socketlabs/socketlabs-go/blob/master/examples/basic/Invalid/BasicWithInvalidFrom/BasicWithInvalidFrom.go) |
| 158 | +This example demonstrates the results of attempting to do a send with an invalid from address. |
| 159 | + |
| 160 | +### [Basic send with invalid recipients](https://github.com/socketlabs/socketlabs-go/blob/master/examples/basic/Invalid/BasicWithInvalidRecipient/BasicWithInvalidRecipient.go) |
| 161 | +This example demonstrates the results of attempting to do a send with invalid recipients. |
| 162 | + |
| 163 | + |
| 164 | +### [Bulk send with multiple recipients](https://github.com/socketlabs/socketlabs-go/blob/master/examples/bulk/bulk/bulk.go) |
| 165 | +This example demonstrates how to send a bulk message to multiple recipients. |
| 166 | + |
| 167 | +### [Bulk send with complex merge including attachments](https://github.com/socketlabs/socketlabs-go/blob/master/examples/bulk/BulkComplex/BulkComplex.go) |
| 168 | +This example demonstrates many features of the `BulkMessage()`, including |
| 169 | +adding multiple recipients, merge data, and adding an attachment. |
| 170 | + |
| 171 | +### [Bulk send with recipients pulled from a datasource](https://github.com/socketlabs/socketlabs-go/blob/master/examples/bulk/BulkFromDataSourceWithMerge/BulkFromDataSourceWithMerge.go) |
| 172 | +This example uses a mock repository class to demonstrate how you would pull |
| 173 | +your recipients from a database and create a bulk mailing with merge data. |
| 174 | + |
| 175 | +### [Bulk send with Ascii charset and special characters](https://github.com/socketlabs/socketlabs-go/blob/master/examples/bulk/BulkWithAsciiCharsetMergeData/BulkWithAsciiCharsetMergeData.go) |
| 176 | +This example demonstrates how to send a bulk message with a specified character |
| 177 | +set and special characters. |
| 178 | + |
| 179 | +### [Bulk send with merge data](https://github.com/socketlabs/socketlabs-go/blob/master/examples/bulk/BulkWithMergeData/BulkWithMergeData.go) |
| 180 | +This example demonstrates how to send a bulk message to multiple recipients with |
| 181 | +unique merge data per recipient. |
| 182 | + |
| 183 | + |
| 184 | +<a name="license"></a> |
| 185 | +# License |
| 186 | +The SocketLabs.EmailDelivery library and all associated code, including any code samples, are [MIT Licensed](https://github.com/socketlabs/socketlabs-go/blob/master/LICENSE.MD). |
0 commit comments