php 版aws PinPoint发附件demo
Laravel8框架,安装了"aws/aws-sdk-php": "^3.257"
主要代码:
public function sendRawMail(Request $request)
{
$file = $request->file('attachment');
/*echo count($file);
dd($file);*/
$filename = $file[0]->getClientOriginalName();
$filepath = $file[0]->getPathname();
$filetype=$file[0]->getMimeType();
$filename1 = $file[1]->getClientOriginalName();
$filepath1 = $file[1]->getPathname();
$filetype1=$file[1]->getMimeType();
$to='recive@xxxxx.com';//收件人1
$to1='recive2@xxxxx.com';//收件人2
$fromAddress = 'xxxxx@xxxxx.com';//发件人(必须是在aws认证过身份的) mb_encode_mimeheader('发件人名称','UTF-8')<xxxxx@xxxxx.com> 这种格式发出去的 会带上发件人名称
$subject='test subject';//邮件主题
//邮件内容
$body='<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div class="main">
姓名:sssssssssssss
<a href="https://ww.baidu.com">点击跳转</a>
</div>
</body>
</html>';
$client = new PinpointClient([
'region' => 'ap-northeast-1',
'version' => 'latest',
]);
$message= "Subject: ".$subject."\n";
$message.= "MIME-Version: 1.0\n";
$message.= 'Content-Type: multipart/mixed; boundary="aRandomString_with_signs_or_9879497q8w7r8number"';
$message.= "\n\n";
$message.= "--aRandomString_with_signs_or_9879497q8w7r8number\n";
$message.= 'Content-Type: text/html; charset="utf-8"';//发送html类型的body
//$message.= 'Content-Type: text/plain; charset="utf-8"';//发送text类型的body
$message.= "\n";
$message.= "Content-Transfer-Encoding: 7bit\n";
$message.= "Content-Disposition: inline\n";
$message.= "\n";
$message.= $body;
$message.= "\n\n";
$message.= "--aRandomString_with_signs_or_9879497q8w7r8number\n"; //分割符
//解决附件名称汉字乱码
$filename=mb_encode_mimeheader($filename,'UTF-8');
$filetype=mb_encode_mimeheader($filetype,'UTF-8');
$message.= 'Content-Type: '.$filetype.'; charset="utf-8"; name="'.$filename.'"';
$message.= "\n";
$message.= "Content-Transfer-Encoding: base64\n";
$message.= 'Content-Disposition: attachment; filename="'.$filename.'"';
$message.= "\n\n\n";
$message.= base64_encode(file_get_contents($filepath));
$message.= "\n\n\n";
$message.= "--aRandomString_with_signs_or_9879497q8w7r8number\n";
//解决附件名称汉字乱码
$filename1=mb_encode_mimeheader($filename1,'UTF-8');
$filetype1=mb_encode_mimeheader($filetype1,'UTF-8');
$message.= 'Content-Type: '.$filetype1.'; charset="utf-8"; name="'.$filename1.'"';
$message.= "\n";
$message.= "Content-Transfer-Encoding: base64\n";
$message.= 'Content-Disposition: attachment; filename="'.$filename1.'"';
$message.= "\n\n\n";
$message.= base64_encode(file_get_contents($filepath1));
$message.= "\n\n\n";
$message.= "--aRandomString_with_signs_or_9879497q8w7r8number--\n";
file_put_contents('/data1/client/public/pdf/a.txt',$message);
$messageRequest = [
'ApplicationId' => 'aws ApplicationId', //已创建的pinPoint上取
'MessageRequest' => [
'Addresses' => [
$to => [
'ChannelType' => 'EMAIL',
],
$to1 => [
'ChannelType' => 'EMAIL',
],
],
'MessageConfiguration' => [
'EmailMessage' => [
'FromAddress' => $fromAddress,
'ReplyToAddresses' => ['xxxxxx@xxxxx.com','xxxxx@xxxxx.com'], //用户回复的地址
'RawEmail'=>['Data'=>$message],
]
],
],
];
$result = $client->sendMessages($messageRequest);
dd($result);
}
postmain测试截图: