socketlabs电子邮件传递python客户端库
socketlabs-injectionapi的Python项目详细描述
socketlabs电子邮件传递python库允许您通过SocketLabs Injection API轻松发送电子邮件。该库使构建和发送api支持的任何类型的消息变得非常容易,从简单消息到单个收件人,再到复杂的批量消息,再到发送到每个收件人具有唯一合并数据的收件人组。
目录
Prerequisites and Installation
Prerequisites
- A supported Python version (3.4, 3.5, 3.6, 3.7)
- A SocketLabs account. If you don't have one yet, you can sign up for a free account开始。
安装
PIP
pip install socketlabs-injectionapi
使用pip
您只需下载软件包并从本地存档文件安装即可。
pip install <path>/socketlabs_injectionapi-1.0.0.tar.gz
从git使用pip
pip install git+https://github.com/socketlabs/socketlabs-python.git#egg=socketlabs_injectionapi
有关详细信息,请参见Installing Packages教程
Getting Started
Obtaining your API Key and SocketLabs ServerId number
In order to get started, you'll need to enable the Injection API feature in the SocketLabs Control Panel。 登录后,导航到socketlabs服务器的仪表板(如果您的帐户上只有一台服务器,则登录后会立即转到此处)。 请记下您的4或5位服务器ID号,因为您需要它和 您的api密钥以便使用注入api。
要启用注入api,请单击顶层导航上的“for developers”下拉列表,然后选择“configure http injection api”选项。 在这里,您可以通过选择 下拉列表。启用该功能还将生成api密钥,您将 需要(连同您的服务器id)开始使用api。一定要点击 “更新”按钮,在完成后保存更改。
基本信息
基本邮件是一封电子邮件,类似于您从个人电子邮件客户端(如outlook)发送的邮件。 基本邮件可以有多个收件人,包括多个收件人地址、抄送地址甚至密件抄送地址。 也可以在基本邮件中发送文件附件。
fromsocketlabs.injectionapiimportSocketLabsClientfromsocketlabs.injectionapi.message.basicmessageimportBasicMessagefromsocketlabs.injectionapi.message.emailaddressimportEmailAddress# Your SocketLabs ServerId and Injection API keyclient=SocketLabsClient(10000,"YOUR-API-KEY");message=BasicMessage()message.subject="Sending A BasicMessage"message.html_body="<html>This is the Html Body of my message.</html>"message.plain_text_body="This is the Plain Text Body of my message.";message.from_email_address=EmailAddress("from@example.com")# A basic message supports up to 50 recipients # and supports several different ways to add recipients# Add a To address by passing the email addressmessage.to_email_address.append(EmailAddress("recipient1@example.com"))message.to_email_address.append(EmailAddress("recipient2@example.com","Recipient #2"))# // Adding CC Recipientsmessage.add_cc_email_address("recipient3@example.com")message.add_cc_email_address("recipient4@example.com","Recipient #4")# Adding Bcc Recipientsmessage.add_bcc_email_address(EmailAddress("recipient5@example.com"))message.add_bcc_email_address(EmailAddress("recipient6@example.com","Recipient #6"))response=client.send(message)
批量消息
大容量邮件通常每封邮件包含一个收件人 通常用于向多个收件人发送相同的内容, 可以选择通过使用mergedata自定义消息。 有关使用合并数据的更多信息,请参见Injection API documentation。
fromsocketlabs.injectionapiimportSocketLabsClientfromsocketlabs.injectionapi.message.bulkmessageimportBulkMessagefromsocketlabs.injectionapi.message.bulkrecipientimportBulkRecipientfromsocketlabs.injectionapi.message.emailaddressimportEmailAddress# Your SocketLabs ServerId and Injection API keyclient=SocketLabsClient(10000,"YOUR-API-KEY");message=BulkMessage()message.plain_text_body="This is the body of my message sent to %%Name%%"message.html_body="<html>This is the HtmlBody of my message sent to %%Name%%</html>"message.subject=testsmessage.from_email_address=EmailAddress("from@example.com")recipient1=BulkRecipient("recipient1@example.com")recipient1.add_merge_data("Name","Recipient1")message.add_to_recipient(recipient1)recipient2=BulkRecipient("recipient2@example.com","Recipient #2")recipient2.add_merge_data("Name","Recipient2")message.add_to_recipient(recipient2)response=client.send(message)
Managing API Keys
For ease of demonstration, many of our examples include the ServerId (SOCKETLABS_SERVER_ID) and API key (SOCKETLABS_INJECTION_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. Depending on your project type, we recommend either storing your credentials using Environment Variables. For more information please see: Using Environment Variables
Examples and Use Cases
In order to demonstrate the many possible use cases for the SDK, we've provided an assortment of code examples. These examples demonstrate many different features available to the Injection API and SDK, including using templates created in the SocketLabs Email Designer,自定义电子邮件头,发送 附件,发送存储在HTML文件中的内容,高级批量 合并,甚至从数据源中提取收件人。
Basic send example
这个例子演示了一个基本的发送。
Basic send async example
基本发送异步示例
Basic send complex example
此示例演示了基本发送的许多功能,包括添加多个收件人、添加邮件和邮件ID以及添加嵌入图像。
Basic send from HTML file
此示例演示如何从HTML文件中读取HTML内容 而不是直接传入字符串。
Basic send from SocketLabs Template
此示例演示如何发送在 Socketlabs电子邮件设计器。这也被称为API Templates功能。
Basic send with specified character set
此示例演示使用特定字符集发送。
Basic send with file attachment
此示例演示如何将文件附件添加到邮件中。
Basic send with custom email headers
此示例演示如何将自定义邮件头添加到电子邮件中。
Basic send with embedded image
此示例演示如何在消息中嵌入图像。
Basic send with a web proxy
这个例子演示了如何在http客户机上使用代理。
Basic send with invalid file attachment
此示例演示尝试使用无效附件执行发送的结果。
Basic send with invalid from address
此示例演示尝试使用无效的发件人地址执行发送的结果。
Basic send with invalid recipients
此示例演示尝试使用无效收件人发送的结果。
Bulk send with multiple recipients
此示例演示如何将大容量邮件发送给多个收件人。
Bulk send with merge data
这个例子演示了如何向多个收件人发送批量邮件 每个收件人的唯一合并数据。
Bulk send with complex merge including attachments
这个例子演示了BulkMessage()
的许多特性,包括
添加多个收件人、合并数据和添加附件。
Bulk send with recipients pulled from a datasource
本例使用一个模拟存储库类来演示如何 从数据库中创建收件人并使用合并数据创建批量邮件。
Bulk send with Ascii charset and special characters
此示例演示如何发送具有指定字符的批量消息 集合和特殊字符。
License
The SocketLabs.EmailDelivery library and all associated code, including any code samples, are MIT Licensed。