Nicolas Aguenot
Nicolas Aguenot

Reputation: 3

SendGrid - Curl php external file attachment broken

I'm using SendGrid for a customer project with curl method.

All works fine but the(ir) file(s) attached on my email sending with SendGrid are broken.

Here is my 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 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);

When I don't have extension file on my key of array, I've a text file containing the related value.

I think I'm not alone to have this problem, well, if you've any idea to solve this problem, thanks for your help !

Upvotes: 0

Views: 3813

Answers (2)

Nicolas Aguenot
Nicolas Aguenot

Reputation: 3

My initial issue was because the path is an autogenerated link and that's why I didn't use an url than realpath.

Anyway, I changed my code and I use now the realpath with mimetype mentioned after the file realpath (and @ before).

It seems work fine now.

I'll like to thank you for your help.

Regards

Upvotes: 0

Nick Q.
Nick Q.

Reputation: 3986

The issue you're experiencing is because you are giving SendGrid a URL for file, rather than the file itself, and SendGrid's API needs the file.

To get your code to work, simply change the $documentList variable to:

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

Instructions on this kind of file upload can be found in this StackOverflow Question, but you might otherwise want to use curl_file_create, to do this.


However, perhaps the best/easiest way to do this is to use SendGrid's PHP Library which makes 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);

Upvotes: 3

Related Questions