Skip to content

Instantly share code, notes, and snippets.

@joe-niland
Created October 13, 2016 01:10
Show Gist options
  • Save joe-niland/e69c223b85a481b30ca2548c85c7b4e2 to your computer and use it in GitHub Desktop.
Save joe-niland/e69c223b85a481b30ca2548c85c7b4e2 to your computer and use it in GitHub Desktop.
<?php
// If you are using Composer
require 'vendor/autoload.php';
use SendGrid\Mail;
$apiKey = getenv('SENDGRID_API_KEY');
$sg = new \SendGrid($apiKey);
$email = new SendGrid\Email("Me", "[email protected]");
$mail = new SendGrid\Mail();
$mail->setFrom($email);
$mail->setSubject("Attachment Test");
$p = new \SendGrid\Personalization();
$p->addTo($email);
$c = new \SendGrid\Content("text/plain", "Hi");
$mail->addPersonalization($p);
$mail->addContent($c);
$att1 = new \SendGrid\Attachment();
$att1->setContent( file_get_contents("test_files/1/1.txt") );
$att1->setType("text/plain");
$att1->setFilename("1.txt");
$att1->setDisposition("attachment");
$mail->addAttachment( $att1 );
$att2 = new \SendGrid\Attachment();
$att2->setContent( file_get_contents("test_files/2/1.txt") );
$att2->setType("text/plain");
$att2->setFilename("1.txt");
$att2->setDisposition("attachment");
$mail->addAttachment( $att2 );
$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode() . "\n";
echo json_encode( json_decode($response->body()), JSON_PRETTY_PRINT) . "\n";
var_dump($response->headers());
@gusan3
Copy link

gusan3 commented Mar 11, 2017

I'm having this error:

400 { "errors": [ { "message": "Bad Request", "field": null, "help": null } ] } array(14) { [0]=> string(25) "HTTP/1.1 400 Bad Request " [1]=> string(14) "Server: nginx " [2]=> string(36) "Date: Sat, 11 Mar 2017 19:20:44 GMT " [3]=> string(31) "Content-Type: application/json " [4]=> string(19) "Content-Length: 63 " [5]=> string(23) "Connection: keep-alive " [6]=> string(22) "X-Frame-Options: DENY " [7]=> string(58) "Access-Control-Allow-Origin: https://sendgrid.api-docs.io " [8]=> string(35) "Access-Control-Allow-Methods: POST " [9]=> string(87) "Access-Control-Allow-Headers: Authorization, Content-Type, On-behalf-of, x-sg-elas-acl " [10]=> string(28) "Access-Control-Max-Age: 600 " [11]=> string(75) "X-No-CORS-Reason: https://sendgrid.com/docs/Classroom/Basics/API/cors.html " [12]=> string(1) " " [13]=> string(0) "" }

have you faced it too?

@iankaufmann
Copy link

I am having the same issue as @gusan3

@Barendm
Copy link

Barendm commented Apr 13, 2017

I had the same error response.
I fixed this by encoding the file first to base64:

$att1 = new SendGrid\Attachment();
$att1->setContent(base64_encode(file_get_contents("photo.jpg")));
$att1->setType("image/jpeg");
$att1->setFilename("photo.jpg");
$att1->setDisposition("attachment");
$mail->addAttachment( $att1 );

@RoySegall
Copy link

@Barendm worked like a charm. Thanks!

Copy link

ghost commented Aug 28, 2018

@Barendm Thanks works very fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment