daniel-zahariev / php-aws-ses
使用类似REST的界面通过Amazon Simple Email Service (SES)发送电子邮件
Requires
- php: >=5.0.0
- ext-curl: *
- ext-simplexml: *
Requires (Dev)
- fzaninotto/faker: ^1.7
- phpspec/prophecy: ~1.0
- phpunit/php-code-coverage: ^6.0
- phpunit/phpunit: ^7.1
README
Amazon Simple Email Service提供了一种简单的方式来发送电子邮件,无需维护自己的邮件服务器。这些PHP类使用基于REST的服务接口。
目录
安装
使用以下命令安装最新版本
composer require daniel-zahariev/php-aws-ses
基本用法
<?php require_once 'vendor/autoload.php'; $m = new SimpleEmailServiceMessage(); $m->addTo('Recipient Name <recipient@example.com>'); $m->setFrom('Sender <user@example.com>'); $m->setSubject('Hello, world!'); $m->setMessageFromString('This is the message body.'); $ses = new SimpleEmailService('AccessKey', 'SecretKey'); print_r($ses->sendEmail($m)); // Successful response should print something similar to: //Array( // [MessageId] => 0000012dc5e4b4c0-b2c566ad-dcd0-4d23-bea5-f40da774033c-000000 // [RequestId] => 4953a96e-29d4-11e0-8907-21df9ed6ffe3 //)
收件人
<?php $m = new SimpleEmailServiceMessage(); // Add many Recipients $m->addTo(array('dwight@example.com', 'angela@example.com')); // You can either add one by one or pass an array to 'To' and 'CC' $m->addCC('holly@example.com'); $m->addCC(array('kelly@example.com', 'ryan@example.com')); // And 'BCC' and 'Reply-To' as well $m->addBCC('michael@example.com'); $m->addBCC(array('kevin@example.com', 'oscar@example.com')); $m->addReplyTo('andy@example.com'); $m->addReplyTo(array('stanley@example.com', 'erin@example.com')); // Also add names to any of the Recipients lists $m->addTo('Jim Carrey <jim@example.com>');
消息正文
<?php // Additionally you can set the content of the email via: $m->setMessageFromFile('/path/to/some/file.txt'); $m->setMessageFromURL('http://example.com/somefile.txt'); // And have both Text and HTML version with: $m->setMessageFromString($text, $html); $m->setMessageFromFile($textfilepath, $htmlfilepath); $m->setMessageFromURL($texturl, $htmlurl); // Remember that setMessageFromString, setMessageFromFile, and setMessageFromURL are mutually exclusive. // If you call more than one, then whichever call you make last will be the message used. // You can also set the encoding of the Subject and the Message Body $m->setSubjectCharset('ISO-8859-1'); $m->setMessageCharset('ISO-8859-1');
如果不指定字符集,默认为UTF-8,这通常是正确的设置。您可以在SES API文档中阅读更多信息。
附件
<?php $m->addAttachmentFromData('my_text_file.txt', 'Simple content', 'text/plain'); $m->addAttachmentFromFile('my_PFD_file.pdf', '/path/to/pdf/file', 'application/pdf'); // SendRawEmail is explicitly used when there are attachments: $ses->sendEmail($m); // Sending raw email can be enforsed with: $ses->sendEmail($m, $use_raw_request = true); // Now you can add an inline file in the message $m->addAttachmentFromFile('logo.png','path/to/logo.png','application/octet-stream', '<logo.png>' , 'inline'); // and use it in the html version of the e-mail: <img src='cid:logo.png' />
配置集和消息标签
<?php // Set the configuration set $m->setConfigurationSet('myConfigurationSet'); // Reset the configuration set $m->setConfigurationSet(null); // Set message tag $m->setMessageTag('key', 'value'); // Get message tag $tag = $m->getMessageTag('key'); // Remove message tag $m->removeMessageTag('key'); // Set message tags in bulk - performs merge with current tags $m->setMessageTags(array('key1' => 'value1', 'key2' => 'value2')); // Get message tags $tags = $m->getMessageTags(); // Remove all message tags $m->removeMessageTags();
发送批量消息
当需要批量发送数百封电子邮件时,最好使用批量模式,它实际上会重用CURL处理器,减少SSL握手次数,从而提高性能。
<?php // Enable bulk sending mode (reuse of CURL handler) $ses->setBulkMode(true); // Send the messages foreach($messages as $message) { $ses->sendEmail($message); } // Disable bulk sending mode $ses->setBulkMode(false);
API端点
有一些地区和Amazon SES端点可用,可以使用如下方式使用它们。
<?php $region_endpoint = SimpleEmailService::AWS_US_EAST_1; $ses = new SimpleEmailService('AccessKey', 'SecretKey', $region_endpoint);
辅助方法
<?php // Get the addresses that have been verified in your AWS SES account $ses->listVerifiedEmailAddresses(); // Delete a verified address $ses->deleteVerifiedEmailAddress('user@example.com'); // Send a confirmation email in order to verify a new email $ses->verifyEmailAddress('user@example.com'); // Get Send Quota $ses->getSendQuota(); // Get Send Statistics $ses->getSendStatistics()
有关这些调用的更多信息,请参阅GetSendQuota和GetSendStatistics的文档。
错误
默认情况下,当Amazon SES API返回错误时,将触发trigger_error
<?php // Set the default behaviour for handling errors $trigger_error = true; $ses = new SimpleEmailService('AccessKey', 'SecretKey', $region_endpoint, $trigger_error); // Or overwrite the main setting on a single call $use_raw_request = false; $trigger_error = false; $ses->sendEmail($m, $use_raw_request, $trigger_error);
请求签名版本
您可以配置应使用请求签名的哪个版本。现在支持并默认使用版本4。
<?php $signature_version = SimpleEmailService::REQUEST_SIGNATURE_V4; $ses = new SimpleEmailService('AccessKey', 'SecretKey', $region_endpoint, $trigger_error, $signature_version);
变更日志
v.0.9.5
- 修复请求查询参数中的数组问题(#83)
v.0.9.4
- 修复PHP8错误(#81)
v.0.9.3
- AWS签名版本4现在是默认版本(感谢VincentNikkelen)
- 添加了对更多地区的支持(感谢VincentNikkelen)
v.0.9.2
- 添加了对AWS签名版本4的支持
v.0.9.1
- 添加了对AWS SES配置集和消息标签的支持
- 在
SimpleEmailServiceMessage
中添加了缓存机制,以加快批量发送模式
v.0.9.0
- 添加了原始消息编码参数
v.0.8.9
- 合并pull request 32 from hlev/remove-to-requirement
v.0.8.8
- 修复的问题:#24, #25, #30, #31
- 在
SimpleEmailService
中添加了setBulkMode
方法,该方法可以启用重复使用SimpleEmailServiceRequest
对象以批量发送请求到AWS SES SimpleEmailService
中添加了新方法:getVerifyPeer
、setVerifyPeer
、getVerifyHost
、setVerifyHost
、getBulkMode
、setBulkMode
、getRequestHandler
(受保护)- 在
SimpleEmailService
中标记为已弃用的方法:enableVerifyHost
、enableVerifyPeer
、verifyHost
、verifyPeer
- 在
SimpleEmailServiceMessage
中新增方法:clearTo
、clearCC
、clearBCC
、clearReplyTo
、clearRecipients
- 在
SimpleEmailServiceRequest
中新增方法:setVerb
、clearParameters
、getCurlHandler
(受保护) - 更新了
SimpleEmailServiceMessage
中的validate
方法 - 添加了一些phpDocumentor注释块
v.0.8.7
- 一些小更新
v.0.8.6
- 删除了示例代码
- 从源文件中删除了版本号
v.0.8.5
- 修复了一些问题 #9, #10, #10
- 合并了添加内联文件的Pull request
- 合并了修复原始消息中'From: '字段错误的Pull request
- 添加了Composer文件并提交到Packagist.org
- 触发错误现在是可选的(默认开启)
- 在
SimpleEmailService
中添加了类常量,以便于选择区域API端点
v.0.8.4
- 在
SimpleEmailServiceMessage
类中添加了addCustomHeader
方法,用于在调用SendRawEmail
时添加自定义头部 (#7) SendRawEmail
方法可以通过sendEmail
函数的新参数强制执行- 当格式为
Name <Email>
时,接收者现在默认使用base64编码 (#3) - 现在应该已经清除了大部分的警告 (#5)
v.0.8.3
- 当存在附件时,自动使用
SendRawEmail
REST API调用
v.0.8.2.
- 初始导入
待办事项列表
- 使用phpdoc标签全面记录类方法
- 使用phpDocumentor构建文档
- 将示例移动到文件中
- 创建一个Composer包
- 允许使用X-Headers
贡献者
代码贡献者
本项目由所有贡献者共同创建。[贡献].
财务贡献者
成为财务贡献者,帮助我们维持社区。[贡献]
个人
组织
使用您的组织支持此项目。您的logo将在此处显示,并带有链接到您的网站。[贡献]
许可
MIT许可
版权所有 (c) 2011 Dan Mayers 版权所有 (c) 2014 Daniel Zahariev
特此授予任何人获得此软件及其相关文档文件(“软件”)副本的许可,免费使用该软件,不受限制,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本,并允许向提供软件的人员使用该软件,前提是遵守以下条件
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
本软件按“现状”提供,不提供任何明示或暗示的保证,包括但不限于适销性、针对特定目的的适用性和非侵权性。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论该索赔、损害或其他责任是由于合同、侵权或其他方式引起的,与软件或其使用或其他交易有关。