2017-05-14 107 views
1

我試圖用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 
}, 

根據所有文件我發現這將是正確的解決方案,但它不起作用。

在此先感謝所有答覆。

回答

2

你的第一個代碼更改爲:

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

我改變'@'.$filenamecurl_file_create($filename, 'application/pdf', 'example.pdf')

查看文檔curl_file_create並檢查注意事項PHP < 5.5。

+0

謝謝,這有很大的幫助。 – Vestalis

0

這對我有效。

<?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>"; 


?>