Mailgun发送带附件的邮件
问题描述:
我试图用mailgun发送带有附件的邮件。 邮件本身很好,但它缺少附件。 也在邮件日志中显示正常,但附件数组为空。Mailgun发送带附件的邮件
我用example.com替换了我的凭证。 该文件被放置在一个子目录中,并且可读。
$filename = 'directory/example.pdf';
$parameters = array('from' => 'Example <[email protected]>',
'to' => '[email protected]',
'subject' => 'Subject',
'text' => 'This is just a test.',
'attachment[1]' => '@' . $filename);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/mail.example.com/messages');
curl_setopt($ch, CURLOPT_USERPWD, 'api:key-ThisIsJustARandomString');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
我没有得到一个错误,这是$response
:
string(103) "{
"id": "<[email protected]>",
"message": "Queued. Thank you."
}"
的mailgun日志没有附件列出在:
"message": {
"headers": {
"to": "[email protected]",
"message-id": "[email protected]",
"from": "Example <[email protected]>",
"subject": "Subject"
},
"attachments": [],
"size": 349
},
根据所有文件我发现这将是正确的解决方案,但它不起作用。
在此先感谢所有答复。
答
你的第一个代码更改为:
$filename = 'directory/example.pdf';
$parameters = array('from' => 'Example <[email protected]>',
'to' => '[email protected]',
'subject' => 'Subject',
'text' => 'This is just a test.',
'attachment[1]' => curl_file_create($filename, 'application/pdf', 'example.pdf'));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/mail.example.com/messages');
curl_setopt($ch, CURLOPT_USERPWD, 'api:key-ThisIsJustARandomString');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
我改变'@'.$filename
到curl_file_create($filename, 'application/pdf', 'example.pdf')
。
查看文档curl_file_create并检查注意事项PHP < 5.5。
答
这对我有效。
<?php
$path = $filename;
define('MAILGUN_URL', 'https://api.mailgun.net/v3/domainname');
define('MAILGUN_KEY', 'private api key from mail gun');
**function sendmailbymailgun**($to,$toname,$mailfromname,$mailfrom,$subject,
$html,$text,$tag,$replyto, $path){
$array_data = array(
'from'=> $mailfromname .'<'.$mailfrom.'>',
'to'=>$toname.'<'.$to.'>',
'subject'=>$subject,
'html'=>$html,
'text'=>$text,
'o:tracking'=>'yes',
'o:tracking-clicks'=>'yes',
'o:tracking-opens'=>'yes',
'o:tag'=>$tag,
'h:Reply-To'=>$replyto,
'attachment[0]' => curl_file_create(__dir__."\\".$path, 'application/pdf', $path),
'attachment[1]' => curl_file_create(__dir__."\\".$path, 'application/pdf', "example.pdf")
);
$session = curl_init(MAILGUN_URL.'/messages');
curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($session, CURLOPT_USERPWD, 'api:'.MAILGUN_KEY);
curl_setopt($session, CURLOPT_POST, true);
curl_setopt($session, CURLOPT_POSTFIELDS, $array_data);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_ENCODING, 'UTF-8');
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($session);
curl_close($session);
$results = json_decode($response, true);
return $results;
}
//: call the function
$res = sendmailbymailgun("[email protected]","Recipeint Name", "Sender Name", "[email protected]","Email subject","Email body. find two attachment","","tags", "[email protected]", $path);
echo "<pre>".print_r($res, true)."</pre>";
?>
谢谢,这有很大的帮助。 – Vestalis