angeljunior / aws-ses
通过REST-like接口使用Amazon Simple Email Service (AWS SES)发送电子邮件
1.1.0
2021-03-23 02:10 UTC
Requires
- php: >=5.6.0
- ext-curl: *
- ext-simplexml: *
This package is auto-updated.
Last update: 2024-09-23 00:17:53 UTC
README
Amazon Simple Email Service提供了一种简单的方法来发送电子邮件,无需维护自己的邮件服务器。这些PHP类使用基于REST的服务接口。
默认支持版本4。
目录
安装
使用以下命令安装最新版本
composer require angeljunior/aws-ses
基本用法
<?php require_once 'vendor/autoload.php'; $m = new SimpleEmailServiceMessage(); $m->addTo('recipient@example.com'); $m->setFrom('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); $m->setMessageFromHTMLString($html); // 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' />
发送大量消息
当需要批量发送数百封电子邮件时,最好使用批量模式,该模式本质上重用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);
变更日志
v.1.0.5。
- 签名v4
v.1.0.0。
- 初始导入