SendGrid - Curl php external file attachment broken -
i'm using sendgrid customer project curl method.
all works fine the(ir) file(s) attached on email sending sendgrid broken.
here code :
$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 $filename=>$documentpath){ $params['files['.$filename.']'] = $documentpath; } } $request = $url.'api/mail.send.json'; // generate curl request $session = curl_init($request); // tell curl use http post curl_setopt ($session, curlopt_post, true); // tell curl body of post curl_setopt ($session, curlopt_postfields, $params); // tell curl not return headers, return response curl_setopt($session, curlopt_header, false); curl_setopt($session, curlopt_returntransfer, true); // obtain response $response = curl_exec($session); curl_close($session);
when don't have extension file on key of array, i've text file containing related value.
i think i'm not alone have problem, well, if you've idea solve problem, !
the issue you're experiencing because giving sendgrid url file, rather file itself, , sendgrid's api needs file.
to code work, change $documentlist
variable to:
$documentlist = array( "doc1.pdf" => "@" . realpath("/path/where/my/attachment/file/is/myfile.pdf") );
instructions on kind of file upload can found in this stackoverflow question, might otherwise want use curl_file_create, this.
however, perhaps best/easiest way use sendgrid's php library makes sending attachments, trivially simple.:
require("path/to/sendgrid-php/sendgrid-php.php"); $sendgrid = new sendgrid('username', 'password'); $email = new sendgrid\email(); $email->addto('foo@bar.com')-> setfrom('me@bar.com')-> setsubject('subject goes here')-> settext('hello world!')-> sethtml('<strong>hello world!</strong>') addattachment("../path/to/file.txt"); $sendgrid->send($email);
Comments
Post a Comment