2017-05-14 4 views
1

mailgun을 사용하여 첨부 파일이있는 메일을 보내려고합니다. 메일 자체는 문제가 없지만 첨부 파일이 누락되었습니다. 또한 mailgun 로그에서 정상적으로 표시되지만 첨부 파일 배열이 비어 있습니다.Mailgun이 첨부 파일과 함께 메일을 보냅니다.

나는 내 자격 증명을 example.com으로 바꿨습니다. 파일은 하위 디렉토리에 있으며 읽을 수 있습니다. 나는이 발견 모든 문서에 따르면

"message": { 
    "headers": { 
     "to": "[email protected]", 
     "message-id": "[email protected]", 
     "from": "Example <[email protected]>", 
     "subject": "Subject" 
    }, 
    "attachments": [], 
    "size": 349 
}, 

: 첨부 파일이 나열되어 mailgun 로그 내에서

string(103) "{ 
    "id": "<[email protected]>", 
    "message": "Queued. Thank you." 
}" 

:

$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 내가 오류가 발생하지 않는 것입니다

올바른 해결책이지만 작동하지 않습니다.

미리 답변 해 주셔서 감사합니다.

답변

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

내가 curl_file_create($filename, 'application/pdf', 'example.pdf')'@'.$filename을 변경했습니다.

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


?>