I am using SendGrid v3 api with the php library to try and send to multiple recipients as part of a WordPress function. I can successfully send emails using the SendGrid sample code and hard coding the multiple recipients. However, when I query the database to try build the list of to: email addresses it always fails with a 400 error. Here is the SendGrid code I am working with. It works fine with live data and hard coded. However, I can't seem to properly build the $tos array from my database query. I have read the documentation and every tutorial I can find. I also contacted Sendgrid support.
$email = new \SendGrid\Mail\Mail();
$email->setFrom("test@example.com", "Example User");
$tos = [
"test+test1@example.com" => "Example User1",
"test+test2@example.com" => "Example User2",
"test+test3@example.com" => "Example User3"
];
$email->addTos($tos);
$email->setSubject("Sending with SendGrid is Fun");
$email->addContent("text/plain", "and easy to do anywhere, even with PHP");
$email->addContent(
"text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
);
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
try {
$response = $sendgrid->send($email);
print $response->statusCode() . "
";
print_r($response->headers());
print $response->body() . "
";
} catch (Exception $e) {
echo 'Caught exception: '. $e->getMessage(). "
";
}
Here is my WordPress query: $sends = $wpdb->get_results( "SELECT * FROM test
" );
How do I properly code the $tos variable from my database query so that $email->addTos($tos) does not error out?
Thanks.
According to this page, the addTos
function is defunct and no longer supported in SendGrid's API: "The addTos()
method was moved to addTo()
and does not currently support arrays being passed in".
Therefore:
Using your test
database:
$email = new \SendGrid\Mail\Mail();
$email->setFrom("test@example.com", "Example User");
$userResults = $wpdb->get_results( "SELECT `email` FROM `test`", ARRAY_A ); //Select as Associative Array
foreach($userResults as $userKey => $userValue){
$userEmail = $userValue['email']);
if ( is_email( $userEmail ) ) { //Validate properly formatted email structure
$email->addTo($userValue['email']); // If you hade a name column you could use: $email->addTo($userValue['email'], $userValue['name']);
}
}
$email->setSubject("Sending with SendGrid is Fun");
$email->addContent("text/plain", "and easy to do anywhere, even with PHP");
$email->addContent(
"text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
);
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
try {
$response = $sendgrid->send($email);
print $response->statusCode() . "
";
print_r($response->headers());
print $response->body() . "
";
} catch (Exception $e) {
echo 'Caught exception: '. $e->getMessage(). "
";
}
Using wp_users
database:
$userResults = $wpdb->get_results( "SELECT `user_email`, `user_nicename` FROM `wp_users`", ARRAY_A );
foreach($userResults as $userKey => $userValue){
$email->addTo($userValue['user_email'], $userValue['user_nicename']);
}
I think @Jamie_D have some misunderstanding about addTos
method because as per now in 19 June 2019 i am using addTos
method and it is working perfectly ok,
Edit: and now sendgrid's employee has also confirmed it see this link:https://github.com/sendgrid/sendgrid-php/issues/127#issuecomment-508330993
when First time i have used it i got errors also so i have debugged and printed some variable in sendgrid library and i have got the point, may be you are also missing that point so you are getting error.
in $tos
array you have to provide associative array in which email should be the keys and user's name should be the values, see below example:
Syntax:
$tos = [
//user emails => user names
"user1@example.com" => "Example User1",
"user2@example.com" => "Example User2",
"user3@example.com" => "Example User3"
];
$email->addTos($tos);
Example using wp_users table:
$tos = array();
$userResults = $wpdb->get_results( "SELECT `user_email`, `user_nicename` FROM `wp_users`", ARRAY_A );
foreach($userResults as $user){
$tos[$user['user_email']]= $user['user_nicename'];
}
$email->addTos($tos);
try it, it will definitely works and if doesn't then tell me in comments, thanks.