Gustavo
Gustavo

Reputation: 914

Sendgrid error 400 Bad Request

I'm getting this error message when trying to send a email through SendGrid:

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) "" }

Here is my code:

<?php

// If you are using Composer
require 'vendor/autoload.php';

use SendGrid\Mail;

$apiKey = 'mykey';
$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("favicon.ico") );
$att1->setType("text/plain");
$att1->setFilename("1.txt");
$att1->setDisposition("attachment");
$mail->addAttachment( $att1 );

$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());
?>

Not sure if it's related to the attachment. I managed to send an email using a different code, but I'm trying to implement attachments to it so this is the new code. I don't know much php so I'm kinda lost on what does this error means.

Upvotes: 2

Views: 6813

Answers (1)

The error that you're getting is due to the attachment.

The contents of the file must be base64 encoded. Otherwise it will malform the JSON since you're introducing random bytes into the request body.

You can see that in the complete example of the mail helper for sendgrid-php, a base64 string is set as content to the attachment. https://github.com/sendgrid/sendgrid-php/blob/master/examples/helpers/mail/example.php#L83

As you can see in the Attachment class, the content is simply just set and will serialize as a string in the JSON object when the class is serialized: https://github.com/sendgrid/sendgrid-php/blob/master/lib/helpers/mail/Mail.php#L670

You just need to use this function: http://php.net/manual/en/function.base64-encode.php

To change the line:

$att1->setContent( file_get_contents("favicon.ico") );

To:

$att1->setContent( base64_encode( file_get_contents("favicon.ico") ) );

The v3 Mail Send documentation is here: https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html

If you scroll down to the "attachments" object and expand the "content" field, it'll show the requirements for it, maily: The Base64 encoded content of the attachment.

Upvotes: 2

Related Questions