Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Errors when using personalization #869

Closed
majortom84 opened this issue Aug 1, 2019 · 2 comments
Closed

Errors when using personalization #869

majortom84 opened this issue Aug 1, 2019 · 2 comments
Labels
status: duplicate duplicate issue

Comments

@majortom84
Copy link

majortom84 commented Aug 1, 2019

I am getting a 400 Bad Request with personalization, without email is sent just fine.

Issue details

400
Array
(
[0] => HTTP/1.1 100 Continue
[1] =>
[2] => HTTP/1.1 400 Bad Request
[3] => Server: nginx
[4] => Date: Thu, 01 Aug 2019 13:55:13 GMT
[5] => Content-Type: application/json
[6] => Content-Length: 510
[7] => Connection: keep-alive
[8] => Access-Control-Allow-Origin: https://sendgrid.api-docs.io
[9] => Access-Control-Allow-Methods: POST
[10] => Access-Control-Allow-Headers: Authorization, Content-Type, On-behalf-of, x-sg-elas-acl
[11] => Access-Control-Max-Age: 600
[12] => X-No-CORS-Reason: https://sendgrid.com/docs/Classroom/Basics/API/cors.html
[13] =>
[14] =>
)
{"errors":[{"message":"Invalid type. Expected: object, given: null.","field":"personalizations.0","help":null},{"message":"Invalid type. Expected: object, given: string.","field":"personalizations.1.to.0","help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.personalizations.to"},{"message":"Invalid type. Expected: object, given: string.","field":"personalizations.1.to.1","help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.personalizations.to"}]}

Code:

           `private function daynamic_email( $array ){

	require '/var/www/html/mydir/vendor/autoload.php';// If you're using Composer (recommended)
	$this->config->load('sendgrid'); //loading of config file
	//print_r($this->config->item('php_key' , 'api_key')); die(); // testing
	// Comment out the above line if not using Composer
	// require("./sendgrid-php.php"); 
	// If not using Composer, uncomment the above line
	
	$email = new \SendGrid\Mail\Mail();
	$personalization = new \SendGrid\Mail\Personalization();
	
	$personalization->addTo("Test1", $toEmails[0]);
	
	$personalization->addTo("Test2", $toEmails[1]);
	$email->setFrom("myemail", "me");
	$email->setSubject($subject);
	
	$email->addPersonalization($personalization);
	
	$email->addContent("text/plain", strip_tags($this->message));
	$email->addContent("text/html", $this->message);

             echo json_encode($email, JSON_PRETTY_PRINT); die();
             //print_r($email); die();
	
	$sendgrid = new SendGrid($this->config->item('php_key' , 'api_key'));
	//$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
	try {
	    $response = $sendgrid->send($email);
	    print $response->statusCode() . "\n";
	    print_r($response->headers());
	   // echo "<pre>"; 
	    print $response->body() . "\n";
	} catch (Exception $e) {
		//echo "<pre>";
	    echo 'Caught exception: ',  $e->getMessage(), "\n";
	}
} // end fuction test_email`

JSON output:

{
    "personalizations": [
        null,
        {
            "to": [
                "test1",
                "test2"
            ]
        }
    ],
    "from": {
        "name": "me",
        "email": "myemail"
    },
    "subject": "SpringStone School Closed for Staff: In-Service",
    "content": [
        {
            "type": "text\/plain",
            "value": "Staff: In-Service\n\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\tSchool is CLOSED\n\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\tDate: 08-09-2019\n\t\t\t\t\t\t\t\t\tLocation: SpringStone\n\t\t\t\t\t\t\t\t\tNotified: \n\t\t\t\t\t\t\t\t"
        },
        {
            "type": "text\/html",
            "value": "Staff: In-Service<\/h1>\n\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t
\n\t\t\t\t\t\t\t\t\t
School is CLOSED<\/span><\/p>\n\t\t\t\t\t\t\t\t<\/div>

\n\t\t\t\t\t\t\t\t\tDate: 08-09-2019<\/span>\n\t\t\t\t\t\t\t\t\t

Location: SpringStone<\/span>\n\t\t\t\t\t\t\t\t\t

Notified: Testing<\/span>\n\t\t\t\t\t\t\t\t<\/div><\/div>" } ] }

Technical details:

  • sendgrid-php Version:
    sendgrid/php-http-client 3.9.6 HTTP REST client, simplified for PHP
    sendgrid/sendgrid 7.0.0 This library allows you to quickly and easily send emails through S...
  • PHP Version: 7.2.19
@ghost
Copy link

ghost commented Aug 29, 2019

I has a similar issue when I tried to use Dynamic Templates.
The code in the repository works well with them, but if you use composer then the library dosen't work, returning a 400 error code.

@thinkingserious
Copy link
Contributor

Hello @majortom84,

I believe this issue should be resolved by #705, but it has yet to be released.

I don't have a specific timeline on the next release for this library, but I can say it is a priority and we are working on implementing a two week consistent release cycle.

With best regards,

Elmer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate duplicate issue
Projects
None yet
Development

No branches or pull requests

2 participants