2014-09-10 2 views
0

curl 메서드를 사용하여 고객 프로젝트에 SendGrid를 사용하고 있습니다.SendGrid - Curl PHP 외부 파일 첨부가 깨졌습니다.

모두 잘되지만 SendGrid로 보내는 이메일에 첨부 된 (ir) 파일이 손상되었습니다. 내가 배열의 내 키 확장 파일이없는 때, 나는 관련 값을 포함하는 텍스트 파일을했습니다

$documentList = array(
    "DOC1.php" => "http://www.customerdomain.com/my/path/where/my/attachment/file/is/myfile.pdf" 
); 


$params = array(
         'api_user' => $user; 
         'api_key' => $pass, 
         'x-smtpapi' => json_encode($json_string), 
         'from'  => $from, 
         'to'  => $to, 
         'subject' => $subject, 
         'html'  => $mailHtml, 
         'text'  => $mailText 
); 


if(count($documentList)>0){ 
    foreach($documentList as $fileName=>$documentPath){ 
     $params['files['.$fileName.']'] = $documentPath; 
    } 
} 

$request = $url.'api/mail.send.json'; 

// Generate curl request 
$session = curl_init($request); 
// Tell curl to use HTTP POST 
curl_setopt ($session, CURLOPT_POST, true); 
// Tell curl that this is the body of the POST 
curl_setopt ($session, CURLOPT_POSTFIELDS, $params); 
// Tell curl not to return headers, but do return the response 
curl_setopt($session, CURLOPT_HEADER, false); 
curl_setopt($session, CURLOPT_RETURNTRANSFER, true); 


// obtain response 
$response = curl_exec($session); 
curl_close($session); 

:

여기 내 코드입니다.

이 문제를 해결할 생각이 있다면 혼자만의 문제는 아닙니다. 도움을 주셔서 감사합니다.

답변

3

발생하는 문제는 SendGrid에 파일 자체가 아닌 파일의 URL을 제공하고 SendGrid의 API에 파일이 필요하기 때문입니다. this StackOverflow Question에서 찾을 수 있습니다 파일 업로드의 종류에

$documentList = array(
    "DOC1.pdf" => "@" . realpath("/path/where/my/attachment/file/is/myfile.pdf") 
); 

지침을하지만 그렇지 않으면이 작업을 수행하기 위해, curl_file_create을 사용할 수 있습니다 :

작동하도록 코드를 얻으려면, 간단하게 할 수있는 $documentList 변수를 변경 . 아마 그러나


,이 작업을 수행하는 가장 좋은/가장 쉬운 방법은 있습니다 SendGrid's PHP Library를 사용하는 것입니다 sending attachments, trivially simple. :

require("path/to/sendgrid-php/sendgrid-php.php"); 
$sendgrid = new SendGrid('username', 'password'); 
$email = new SendGrid\Email(); 
$email->addTo('[email protected]')-> 
     setFrom('[email protected]')-> 
     setSubject('Subject goes here')-> 
     setText('Hello World!')-> 
     setHtml('<strong>Hello World!</strong>') 
     addAttachment("../path/to/file.txt"); 
$sendgrid->send($email); 
+0

'curl_file_create'이 효과적입니다. – Shahbaz

0

경로가 자동 생성 된 링크이고 그게 내가 왜 때문에 내 초기 문제였다 realpath보다 url을 사용하지 않았습니다.

어쨌든 코드를 변경했고 파일 realpath (및 @ 앞에)에 언급 된 mimetype을 사용하여 realpath를 사용합니다.

지금은 문제가없는 것 같습니다.

귀하의 도움에 감사드립니다.

감사합니다.