rafaame / php-aws-ses
基于REST的接口,用于Amazon Simple Email Service (SES),用PHP编写。
This package is not auto-updated.
Last update: 2024-09-24 07:22:10 UTC
README
Amazon Simple Email Service提供了一种简单的方法来发送电子邮件,无需维护自己的邮件服务器。这些PHP类使用基于REST的接口来调用该服务。
此存储库是从由Dan Myers开发的原始类的版本0.8.2分支而来。
示例用法
一些示例代码将很好地展示这个类如何简单易用。请阅读这些示例,如果您有任何问题或建议,请随时留言。
首先,您需要创建一个SimpleEmailService类对象
require_once('SimpleEmailService.php');
require_once('SimpleEmailServiceMessage.php');
require_once('SimpleEmailServiceRequest.php');
$ses = new SimpleEmailService('Access Key Here', 'Secret Key Here');
如果您首次使用Simple Email Service,您将需要至少验证一个电子邮件地址,以便您能发送消息
print_r($ses->verifyEmailAddress('user@example.com'));
-------
Array
(
[RequestId] => 1b086469-291d-11e0-85af-df1284f62f28
)
您对SimpleEmailService发出的每个请求都将返回一个请求ID。如果您需要联系AWS处理任何问题,这个ID可能很有用。为了简洁起见,如果服务调用只返回一个值,我将省略请求ID。
在您请求验证后,您将在该地址收到一封包含链接的电子邮件。点击链接以验证您的地址。一旦完成,您就可以将其用作通过SES发送的电子邮件的“发件人”地址。如果您还没有生产访问权限,您还需要验证您想要发送邮件的所有地址。
如果您想查看您账号上已验证的地址,这很简单
print_r($ses->listVerifiedEmailAddresses());
-------
Array
(
[RequestId] => 77128e89-291d-11e0-986f-43f07db0572a
[Addresses] => Array
(
[0] => user@example.com
[1] => recipient@example.com
)
)
从已验证地址列表中删除地址同样简单
$ses->deleteVerifiedEmailAddress('user@example.com');
如果需要,此调用将返回请求ID。
剩下的唯一事情就是发送电子邮件,让我们试试。首先,您需要一个SimpleEmailServiceMessage对象。然后,您将想要设置消息的各种属性。例如
$m = new SimpleEmailServiceMessage();
$m->addTo('recipient@example.com');
$m->setFrom('user@example.com');
$m->setSubject('Hello, world!');
$m->setMessageFromString('This is the message body.');
print_r($ses->sendEmail($m));
-------
Array
(
[MessageId] => 0000012dc5e4b4c0-b2c566ad-dcd0-4d23-bea5-f40da774033c-000000
[RequestId] => 4953a96e-29d4-11e0-8907-21df9ed6ffe3
)
就是这样!
您可以使用这个类做更多的事情。您可以进行两个信息查询,试试这些
print_r($ses->getSendQuota());
print_r($ses->getSendStatistics());
为了简洁起见,我不会显示这两个API调用的输出。这些信息将帮助您跟踪您的账户健康状况。有关这些调用更多信息,请参阅GetSendQuota和GetSendStatistics的简单电子邮件服务文档。
您可以设置多个收件人/抄送/密送地址,可以单独设置或一次性全部设置
$m->addTo('jim@example.com');
$m->addTo(array('dwight@example.com', 'angela@example.com'));
$m->addCC('holly@example.com');
$m->addCC(array('kelly@example.com', 'ryan@example.com'));
$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'));
您可以设置一个回退路径地址
$m->setReturnPath('noreply@example.com');
您可以使用文件的内容作为消息文本,而不是字符串
$m->setMessageFromFile('/path/to/some/file.txt');
// or from a URL, if allow_url_fopen is enabled:
$m->setMessageFromURL('http://example.com/somefile.txt');
如果您的消息既有文本版本又有HTML版本,您可以同时设置两者
$m->setMessageFromString($text, $html);
或从一对文件中设置
$m->setMessageFromFile($textfilepath, $htmlfilepath);
// or from a URL, if allow_url_fopen is enabled:
$m->setMessageFromURL($texturl, $htmlurl);
请记住,setMessageFromString、setMessageFromFile和setMessageFromURL是互斥的。如果您调用多个,则最后调用的调用将使用该消息。
最后,如果您需要指定主题或消息中使用的字符集
$m->setSubjectCharset('ISO-8859-1');
$m->setMessageCharset('ISO-8859-1');
如果没有指定字符集,默认为UTF-8,这通常是正确的设置。您可以在SES API文档中了解更多信息。
这个库现在支持 SendRawEmail
调用,这意味着您 可以发送带附件的邮件 或自定义头信息,不过很遗憾,我无法添加这项支持。
当存在附件时,将自动使用 SendRawEmail
调用。
$m->addAttachmentFromData('my_text_file.txt', 'Simple content', 'text/plain');
$m->addAttachmentFromFile('my_PFD_file.pdf', '/path/to/pdf/file', 'application/pdf');
// SendRawEmail call will now be used:
$ses->sendEmail($m);
变更日志
v.0.8.3
- 当存在附件时,自动使用
SendRawEmail
REST API 调用。
v.0.8.2.
- 初始导入
待办事项列表
- 使用 phpdoc 标签完整记录类方法
- 使用 phpDocumentor 构建文档
- 将示例移动到文件中