Mailgun 发送带附件的邮件

Mailgun send mail with attachment

我正在尝试使用 mailgun 发送带有附件的邮件。 邮件本身没有问题,但缺少附件。 同样在 mailgun 日志中它显示正常,但附件数组是空的。

我用 example.com 替换了我的凭据。 文件放在子目录下,可读

$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);

我没有收到错误,这是 $response:

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

在 mailgun 日志中没有列出任何附件:

"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
},

根据所有文档,我发现这将是正确的解决方案,但它不起作用。

提前感谢所有回复。

将您的第一个代码更改为:

$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);

我把'@'.$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("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>";  


?>

或使用 api https://documentation.mailgun.com/en/latest/api-sending.html#examples

设置域和参数后,您就可以使用 $result = $mgClient->messages()->send( $domain, $params );

$dest = "filepath/test.pdf";
$result = $mgClient->messages()->send($domain,
            array('from'    => 'Tester <test@test.com>',
              'to'      => 'test@test02.com',
              'subject' => 'Testing',
              'html'    => '<h1>Normal Testing</h1>',
              'attachment' => array(
                array(
                    'filePath' => $dest,
                    'filename' => 'test.pdf'
                    )
                )
              ));

这段代码对我来说工作正常。

这对我有用。我已经在附件数组中传递了附件文件 URL。现在我可以在电子邮件中发送多个附件了。

$attachment = [  0 => 
'https://www.crm.truerater.com/public/assets/client_upload_images/1634327873.png',
         1 => 'https://www.crm.truerater.com/public/assets/client_upload_images/1634327873.png'  
];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/truerater.com/messages');
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    $post = array(
        'from' =>  $mailfromname .'<'.$mailfrom.'>',
        'to' => $toname.'<'.$to.'>',
        'cc' => '',
        'bcc' => '',
        'subject' => $subject,
        'html'=>$html,
        'text'=>$text,
        'o:tracking'=>'yes',
        'o:tracking-clicks'=>'yes',
        'o:tracking-opens'=>'yes',
        'o:tag'=>$tag,
        'h:Reply-To'=>$replyto,
    );
    if(sizeOf($attachment) > 0){
        $i=0;
        foreach ($attachment as $attach){
            $attachPath = substr($attach, strrpos($attach, '/public/') + 1);
            $post['attachment['.$i.']'] = curl_file_create($attach, '', substr($attachPath, strrpos($attachPath, '/') + 1));
            $i=$i+1;
        }
    }
    $headers_arr = array("Content-Type:multipart/form-data");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    curl_setopt($ch, CURLOPT_USERPWD, 'api' . ':' . $key);
    curl_setopt($ch, CURLOPT_HEADER, $headers_arr);
    curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    
    $result = curl_exec($ch);
    
    
    if (curl_errno($ch)) {
        $result = curl_error($ch);
        \Log::info($result);
    }
    else{
        $result = json_decode($result,true);
    }
    curl_close($ch);
   
    \Log::info($result);
    return $result;