Mailgun send mail with attachment

那年仲夏 提交于 2019-12-23 13:24:28

问题


I am trying to send a mail with attachments with mailgun. The mail itself is fine, but it is missing the attachment. Also in the mailgun log it shows up fine, but the attachment array is empty.

I replaced my credentials with example.com. The file is placed in a subdirectory and is readable.

$filename = 'directory/example.pdf';
$parameters = array('from' => 'Example <example@mail.example.com>',
                    'to' => 'example@example.com',
                    '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);

I don't get an error, this is the $response:

string(103) "{
    "id": "<20170514122638.8820.55203.4543B111@mail.example.com>",
    "message": "Queued. Thank you."
}"

Within the mailgun logs no attachments are listed:

"message": {
    "headers": {
        "to": "example@example.com",
        "message-id": "20170514122638.8820.55203.4543B111@mail.example.com",
        "from": "Example <example@mail.example.com>",
        "subject": "Subject"
    },
    "attachments": [],
    "size": 349
},

According to all documentation I found this would be the correct solution, but it is not working.

Thanks in advance for all replies.


回答1:


Change your first code to:

$filename = 'directory/example.pdf';
$parameters = array('from' => 'Example <example@mail.example.com>',
                'to' => 'example@example.com',
                '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);

I changed '@'.$filename to curl_file_create($filename, 'application/pdf', 'example.pdf').

See documentation curl_file_create and check the notes for PHP < 5.5.




回答2:


This worked for me.

<?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("example@yahoo.com","Recipeint Name", "Sender Name", "sender@example.com","Email subject","Email body. find two attachment","","tags", "no-reply@example.com", $path);
  echo "<pre>".print_r($res, true)."</pre>";  


?>


来源:https://stackoverflow.com/questions/43963914/mailgun-send-mail-with-attachment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!