sendgrid-php icon indicating copy to clipboard operation
sendgrid-php copied to clipboard

The to email parameter is required.","field":"personalizations

Open kamaldua65 opened this issue 6 years ago • 7 comments

I am trying to send email to many recipients with template & persotionalization. I am getting Error: SendGrid\Response Object ( [statusCode:protected] => 400 [body:protected] => {"errors":[{"message":"The to email parameter is required.","field":"personalizations.0.to.0.email","help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.personalizations.to"}]} [headers:protected] => Array ( [0] => HTTP/1.1 400 Bad Request [1] => Server: nginx [2] => Date: Sat, 30 May 2020 20:50:40 GMT [3] => Content-Type: application/json [4] => Content-Length: 206 [5] => Connection: keep-alive [6] => Access-Control-Allow-Origin: https://sendgrid.api-docs.io [7] => Access-Control-Allow-Methods: POST [8] => Access-Control-Allow-Headers: Authorization, Content-Type, On-behalf-of, x-sg-elas-acl [9] => Access-Control-Max-Age: 600 [10] => X-No-CORS-Reason: https://sendgrid.com/docs/Classroom/Basics/API/cors.html [11] => [12] => ) )

outpout of print_r($email) is: SendGrid\Mail\Mail Object ( [from:SendGrid\Mail\Mail:private] => SendGrid\Mail\From Object ( [name:SendGrid\Mail\EmailAddress:private] => ABC [email:SendGrid\Mail\EmailAddress:private] => [email protected] [substitutions:SendGrid\Mail\EmailAddress:private] => [subject:SendGrid\Mail\EmailAddress:private] => ) [subject:SendGrid\Mail\Mail:private] => [contents:SendGrid\Mail\Mail:private] => [attachments:SendGrid\Mail\Mail:private] => [template_id:SendGrid\Mail\Mail:private] => SendGrid\Mail\TemplateId Object ( [template_id:SendGrid\Mail\TemplateId:private] => xxxxxxxxxxxxxxxxxxxxxxxxxx3 ) [sections:SendGrid\Mail\Mail:private] => [headers:SendGrid\Mail\Mail:private] => [categories:SendGrid\Mail\Mail:private] => [custom_args:SendGrid\Mail\Mail:private] => [substitutions:SendGrid\Mail\Mail:private] => [send_at:SendGrid\Mail\Mail:private] => [batch_id:SendGrid\Mail\Mail:private] => [asm:SendGrid\Mail\Mail:private] => [ip_pool_name:SendGrid\Mail\Mail:private] => [mail_settings:SendGrid\Mail\Mail:private] => [tracking_settings:SendGrid\Mail\Mail:private] => [reply_to:SendGrid\Mail\Mail:private] => [personalization:SendGrid\Mail\Mail:private] => Array ( [0] => SendGrid\Mail\Personalization Object ( [tos:SendGrid\Mail\Personalization:private] => [ccs:SendGrid\Mail\Personalization:private] => [bccs:SendGrid\Mail\Personalization:private] => [subject:SendGrid\Mail\Personalization:private] => [headers:SendGrid\Mail\Personalization:private] => [substitutions:SendGrid\Mail\Personalization:private] => [dynamic_template_data:SendGrid\Mail\Personalization:private] => [has_dynamic_template:SendGrid\Mail\Personalization:private] => [custom_args:SendGrid\Mail\Personalization:private] => [send_at:SendGrid\Mail\Personalization:private] => ) [1] => SendGrid\Mail\Personalization Object ( [tos:SendGrid\Mail\Personalization:private] => Array ( [0] => ArrayObject Object ( [storage:ArrayObject:private] => Array ( [0] => Array ( [email] => [email protected] ) [1] => Array ( [email] => [email protected] ) [2] => Array ( [email] => [email protected] ) [3] => Array ( [email] => [email protected] ) [4] => Array ( [email] => [email protected] ) ) ) ) [ccs:SendGrid\Mail\Personalization:private] => [bccs:SendGrid\Mail\Personalization:private] => [subject:SendGrid\Mail\Personalization:private] => [headers:SendGrid\Mail\Personalization:private] => [substitutions:SendGrid\Mail\Personalization:private] => Array ( [Title] => Array ( [0] => น.ส. [1] => นาย [2] => น.ส. [3] => นาง [4] => นาย ) [FirstName] => Array ( [0] => First_ 11117554 [1] => First_ 11117555 [2] => First_ 11117556 [3] => First_ 11117557 [4] => First_ 11117558 ) [LastName] => Array ( [0] => Last_ 11117554 [1] => Last_ 11117555 [2] => Last_ 11117556 [3] => Last_ 11117557 [4] => Last_ 11117558 ) [UserName] => 904345343 [Password] => xxxxxxxxxxxx ) [dynamic_template_data:SendGrid\Mail\Personalization:private] => [has_dynamic_template:SendGrid\Mail\Personalization:private] => [custom_args:SendGrid\Mail\Personalization:private] => [send_at:SendGrid\Mail\Personalization:private] => ) ) )

I can see email parameters are present. Code is as below:

$email = new SendGrid\Mail\Mail(); $email->setFrom("[email protected]", "ABC"); $personalization = new SendGrid\Mail\Personalization(); $personalization->addTo($recipients,null, 0); $email->addPersonalization($personalization);

$email->setTemplateId( new \SendGrid\Mail\TemplateId( $TEMPLATE_ID ) );

$email->addDynamicTemplateDatas( [ 'Title' => $title, 'FirstName' => $fname, 'LastName' => $lname, 'UserName' => $username, 'Password' => $password
] );

$sendgrid = new \SendGrid('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');

print_r($email);

$response = $sendgrid->send($email);

print_r($response);

kamaldua65 avatar May 30 '20 21:05 kamaldua65

Did you use this code in previous version of this library, or did you find this code somewhere else?

This sample has a few issues (if using current version): $personalization->addTo($recipients,null, 0); The addTo method of Personalization accepts just a single argument. You provided 3, which suggest you intent to use addTo method of Mail (?). According to tos member of Personalization, the array contains an ArrayObject instead of To instances. Because the ArrayObject is added, contents are extra wrapped and that isn't recognized by the API.

If your purpose is to 'send a single message having the same contents, to multiple recipients which see each other' (which your code sample does), remove the 3 Personalization lines and add every recipient in a loop by $email->addTo($recipient); $recipient contains the email address (string or created To instance).

kampalex avatar May 31 '20 13:05 kampalex

Hi kampalex Thank you for quick response. Actually I have tried initially Personalization with single argument. My requirement is :

  • I need to send single email with username & password to each recipient.
  • recipient email , username, passwords are picked from database
  • Each recipient will see only his email in To field.
  • I already have template at sendgrid ( username & password ) will be replaced in template.

I will try again. If you kenow any sample which fulfills above requirements then please share, Thanks in advance

kamaldua65 avatar Jun 01 '20 05:06 kamaldua65

I had the same idea while I was reading your code sample.

You have to personalize every single message which has their own substitutions. If you convert this sentence to code, you have to create a single Personalization instance for every recipient, apply addTo and addSubstitution methods and append onto Mail instance using addPersonalization.

Please take a look at the Kitchen Sink example, which contains sample code to add multiple Personalization instances having different settings.

(Note to myself: Maybe it's an idea to implement addSubstitutions in Personalization which accepts an array containing multiple substitutions.)

kampalex avatar Jun 01 '20 08:06 kampalex

I am not sure what I am doing wrong, everything looks good. Now only issue remaining is all emails are sent to last email in percipient array:

$email = new SendGrid\Mail\Mail();

$email->setFrom("[email protected]", "ABC");

$i=0;

$Emailobject = new stdClass(); $personalization=array(); foreach ($recipients as &$recipient) {

$Emailobject->email = $recipient;


$personalization[$i] = new SendGrid\Mail\Personalization();
	
$personalization[$i]->addTo($Emailobject);

$email->addPersonalization($personalization[$i]);

$email->setTemplateId(
new \SendGrid\Mail\TemplateId( $TEMPLATE_ID )
);

$email->addDynamicTemplateDatas( [
'Title'     => $title[$i],
'FirstName' => $fname[$i],
'LastName' => $lname[$i],
'UserName' => $username[$i],
'Password' => $password[$i]

] ); $i++; }

echo json_encode($email, JSON_PRETTY_PRINT), "\n";

See $email { "personalizations": [ { "to": [ { "email": " [email protected]" } ], "dynamic_template_data": { "Title": "\u0e19.\u0e2a.", "FirstName": "First_ 11117554", "LastName": "Last_ 11117554", "UserName": "kamal", "Password": "5lQ8685F" } }, { "to": [ { "email": " [email protected]" } ], "dynamic_template_data": { "Title": "\u0e19\u0e32\u0e22", "FirstName": "First_ 11117555", "LastName": "Last_ 11117555", "UserName": "4043017468", "Password": "8bA7B708" } }, { "to": [ { "email": " [email protected]" } ], "dynamic_template_data": { "Title": "\u0e19.\u0e2a.", "FirstName": "First_ 11117556", "LastName": "Last_ 11117556", "UserName": "4043018163", "Password": "7yC5CDF8" } }, { "to": [ { "email": " [email protected]" } ], "dynamic_template_data": { "Title": "\u0e19\u0e32\u0e07", "FirstName": "First_ 11117557", "LastName": "Last_ 11117557", "UserName": "4043027780", "Password": "1mWB23B8" } }, { "to": [ { "email": " [email protected]" } ], "dynamic_template_data": { "Title": "\u0e19\u0e32\u0e22", "FirstName": "First_ 11117558", "LastName": "Last_ 11117558", "UserName": "4043030343", "Password": "7bO61444" } } ], "from": { "name": "ABC", "email": "[email protected]" }, "template_id": "d-e7f2a5aace61444bb486e2ebd6e65823" }

kamaldua65 avatar Jun 01 '20 13:06 kamaldua65

I suggest to clean up some output in your post. It may contain sensitive data.

The cause of the 'last address is used everywhere' is because you're using the same object (reference) in every Personalization instance you've created. In the foreach loop, property email of this object is overwritten every time, so its value equals latest value of $recipients.

Much better would be using like this:

// $recipient = "[email protected]";
$personalization[$i]->addTo(new SendGrid\Mail\To($recipient));

I'm still not confident how you assign your addDynamicTemplateDatas onto $email, but it works for now because it's assigned to the last added Personalization. Leave out the setTemplateId() from the foreach loop. You only have to assign it a single time (before/after this loop).

kampalex avatar Jun 01 '20 19:06 kampalex

Thank you for suggestion, all data is demo data, not real data. problem is not with Personalization, it works perfectly good. I found the issue, i moved $Emailobject = new stdClass(); inside the loop and problem is solved. Final code which works is below, in case someone else needs it.

However I have a suggestion, which may be good idea to implement for future. $personalization[$i]->addTo($Emailobject); should be able to take parameter as string/array element, instead of object, which can save lot of trouble converting array element to object.

`$email = new \SendGrid\Mail\Mail(); $email->setFrom("[email protected]", "ABC");

$i=0;

$personalization=array();

foreach ($recipients as &$recipient) { $Emailobject = new stdClass(); $Emailobject->email = $recipient; $personalization[$i] = new SendGrid\Mail\Personalization(); $personalization[$i]->addTo($Emailobject); $email->addPersonalization($personalization[$i]);

$email->addDynamicTemplateDatas( [
'Title'     => $title[$i],
'FirstName' => $fname[$i],
'LastName' => $lname[$i],
'UserName' => $username[$i],
'Password' => $password[$i]

] ); $i++; } $email->setTemplateId( new \SendGrid\Mail\TemplateId( $TEMPLATE_ID ) );

echo json_encode($email, JSON_PRETTY_PRINT), "\n";`

kamaldua65 avatar Jun 02 '20 06:06 kamaldua65

However I have a suggestion, which may be good idea to implement for future. $personalization[$i]->addTo($Emailobject); should be able to take parameter as string/array element, instead of object, which can save lot of trouble converting array element to object.

I agree with that, but limited to only allow string (email address) or To instance. Just like the addTo method in Mail. For now, I strongly recommend to use a To instance because pattern validation of the provided email address can be performed.

kampalex avatar Jun 02 '20 07:06 kampalex