[英]How to send file as attachment using AWS SES latest SDK 3.33 and PHP7?
I was trying to send file(s) as attachment using PHP AWS SES latest SDK 3.33. 我试图使用PHP AWS SES最新的SDK 3.33发送文件作为附件。
After doing search for solution, I got answer from here but this works on older version of AWS SDK not on current one. 在搜索解决方案后,我从这里得到了答案,但是它适用于旧版本的AWS开发工具包,而不适用于当前版本。
AWS provide two API method
sendEmail
andsendRawEmail
, sendRawEmail is used to send file as attachment to email.AWS提供了两种API方法
sendEmail
和sendRawEmail
,sendRawEmail用于发送文件作为电子邮件的附件。
So below is updated code for latest AWS SDK 3.33 因此,以下是最新的AWS开发工具包3.33的更新代码
require __DIR__ . '/aws/vendor/autoload.php';
$s3_config = [
'region' => 'us-east-1',
'version' => 'latest',
'credentials' => [
'key' => '<keyxxx>',
'secret' => '<secretxxx>'
]
];
$aws = new \Aws\Sdk($s3_config);
$client = $aws->createSes();
$content = "";
$filename = "";
$mailto = "to@test.com"
$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));
$filename = basename($file);
$subject = "Test Email";
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$FILEINFO_MIME_TYPE = finfo_file($finfo, $file);
try {
$separator = md5(time());
$separator_multipart = md5($subject . time());
$message = "MIME-Version: 1.0\n";
$message .= "Subject: $subject\n";
$message .= "From: Test Name <from@test.com>\n";
$message .= "To: $mailto\n";
$message .= "Content-Type: multipart/mixed; boundary=\"$separator_multipart\"\n";
$message .= "\n--$separator_multipart\n";
$message .= "Content-Type: multipart/alternative; boundary=\"$separator\"\n";
$message .= "\n--$separator\n";
$message .= "Content-Type: text/plain; charset=\"UTF-8\"\n";
$message .= "\n$body\n";
$message .= "\n--$separator--\n";
$message .= "--$separator_multipart\n";
$message .= "Content-Type: $FILEINFO_MIME_TYPE; name=\"$filename\"\n";
$message .= "Content-Disposition: attachment; filename=\"$filename\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "$content\n";
$message .= "--$separator_multipart--";
$result = $client->sendRawEmail([
'RawMessage' => [
'Data' => $message
]
]);
echo "\nEmail Sent\n";
} catch (\Exception $e) {
echo $e->getMessage();
}
For those not willing to handle the raw message creation manually, a good alternative is to use PHPMailer to do that for them. 对于那些不愿意手动处理原始消息创建的人,一个不错的选择是使用PHPMailer来为他们做这些。
Step 1: Compile the raw message using PHPMailer 步骤1:使用PHPMailer编译原始消息
$mail=new PHPMailer();
$mail->From="john.doe@example.com";
$mail->FromName="John Doe";
$mail->Subject="SES Email With Attachments";
$mail->Body="<p>Some HTML content</p>";
$mail->isHTML(true);
$mail->addAddress("jane.doe@example.com", "Jane Doe");
//Add attachments one by one
$mail->addStringAttachment(file_get_contents("/path/to/attachment1"), "Attachment1");
$mail->addStringAttachment(file_get_contents("/path/to/attachment2"), "Attachment2");
Step 2: Get the raw MIME message from PHPMailer 步骤2:从PHPMailer获取原始MIME消息
if(!$mail->preSend())
{
die($mail->ErrorInfo);
}
$rawMessage=$mail->getSentMIMEMessage();
Step 3: Send the message using SES 步骤3:使用SES发送消息
try
{
$result=$sesClient->sendRawEmail(array
(
'RawMessage'=>array
(
'Data'=>$rawMessage
)
));
echo "Success! Message has been sent!";
}
catch(\InvalidArgumentException $e)
{
echo "The email was not sent. Error message: ".$e->getMessage();
}
catch(\Aws\Ses\Exception\SesException $e)
{
echo "The email was not sent. Error message: ".$e->getAwsErrorMessage()."\n";
}
catch(\AwsException $e)
{
echo "The email was not sent. Error message: ".$e->getAwsErrorMessage()."\n";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.