SendGrid sending to multiple recipients using api v3 & PHP library in Wordpress function












0















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 SendGridMailMail(); 
$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() . "n";
print_r($response->headers());
print $response->body() . "n";
} catch (Exception $e) {
echo 'Caught exception: '. $e->getMessage(). "n";
}


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.










share|improve this question























  • You should remove the plus(+) symbols in the test email address to check if SendGrid isn't choking on them.

    – Jamie_D
    Nov 20 '18 at 23:48













  • Please post the structure of your test database.

    – Jamie_D
    Nov 20 '18 at 23:56











  • test database has 2 columns - id and email.

    – apadley
    Nov 21 '18 at 3:33











  • The + is not the issue. I used real email addresses and the sample code works fine. The issue is when I try to dynamically populate the email addresses from the database.

    – apadley
    Nov 21 '18 at 3:34
















0















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 SendGridMailMail(); 
$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() . "n";
print_r($response->headers());
print $response->body() . "n";
} catch (Exception $e) {
echo 'Caught exception: '. $e->getMessage(). "n";
}


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.










share|improve this question























  • You should remove the plus(+) symbols in the test email address to check if SendGrid isn't choking on them.

    – Jamie_D
    Nov 20 '18 at 23:48













  • Please post the structure of your test database.

    – Jamie_D
    Nov 20 '18 at 23:56











  • test database has 2 columns - id and email.

    – apadley
    Nov 21 '18 at 3:33











  • The + is not the issue. I used real email addresses and the sample code works fine. The issue is when I try to dynamically populate the email addresses from the database.

    – apadley
    Nov 21 '18 at 3:34














0












0








0








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 SendGridMailMail(); 
$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() . "n";
print_r($response->headers());
print $response->body() . "n";
} catch (Exception $e) {
echo 'Caught exception: '. $e->getMessage(). "n";
}


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.










share|improve this question














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 SendGridMailMail(); 
$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() . "n";
print_r($response->headers());
print $response->body() . "n";
} catch (Exception $e) {
echo 'Caught exception: '. $e->getMessage(). "n";
}


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.







php wordpress sendgrid-api-v3






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 21:22









apadleyapadley

33




33













  • You should remove the plus(+) symbols in the test email address to check if SendGrid isn't choking on them.

    – Jamie_D
    Nov 20 '18 at 23:48













  • Please post the structure of your test database.

    – Jamie_D
    Nov 20 '18 at 23:56











  • test database has 2 columns - id and email.

    – apadley
    Nov 21 '18 at 3:33











  • The + is not the issue. I used real email addresses and the sample code works fine. The issue is when I try to dynamically populate the email addresses from the database.

    – apadley
    Nov 21 '18 at 3:34



















  • You should remove the plus(+) symbols in the test email address to check if SendGrid isn't choking on them.

    – Jamie_D
    Nov 20 '18 at 23:48













  • Please post the structure of your test database.

    – Jamie_D
    Nov 20 '18 at 23:56











  • test database has 2 columns - id and email.

    – apadley
    Nov 21 '18 at 3:33











  • The + is not the issue. I used real email addresses and the sample code works fine. The issue is when I try to dynamically populate the email addresses from the database.

    – apadley
    Nov 21 '18 at 3:34

















You should remove the plus(+) symbols in the test email address to check if SendGrid isn't choking on them.

– Jamie_D
Nov 20 '18 at 23:48







You should remove the plus(+) symbols in the test email address to check if SendGrid isn't choking on them.

– Jamie_D
Nov 20 '18 at 23:48















Please post the structure of your test database.

– Jamie_D
Nov 20 '18 at 23:56





Please post the structure of your test database.

– Jamie_D
Nov 20 '18 at 23:56













test database has 2 columns - id and email.

– apadley
Nov 21 '18 at 3:33





test database has 2 columns - id and email.

– apadley
Nov 21 '18 at 3:33













The + is not the issue. I used real email addresses and the sample code works fine. The issue is when I try to dynamically populate the email addresses from the database.

– apadley
Nov 21 '18 at 3:34





The + is not the issue. I used real email addresses and the sample code works fine. The issue is when I try to dynamically populate the email addresses from the database.

– apadley
Nov 21 '18 at 3:34












1 Answer
1






active

oldest

votes


















0














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 SendGridMailMail(); 
$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() . "n";
print_r($response->headers());
print $response->body() . "n";
} catch (Exception $e) {
echo 'Caught exception: '. $e->getMessage(). "n";
}


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']);
}





share|improve this answer


























  • Thanks. Your solution will work in the limited situation of adding emails to "To" field. However, the article you cited about addTos being deprecated is over 3 years old and was written about an older api. The current api v3 does use addTos and uses the same format for addCcs and addBccs so I'm still looking for a solution that will address all three.

    – apadley
    Nov 21 '18 at 20:51











  • When your solution is applied to the addBccs it generates the following warnings and no bccs are sent: Warning: current() expects parameter 1 to be array, string given in /sendgrid-php/lib/mail/Mail.php on line 281 Warning: Invalid argument supplied for foreach() in /sendgrid-php/lib/mail/Mail.php on line 291

    – apadley
    Nov 21 '18 at 20:51











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401735%2fsendgrid-sending-to-multiple-recipients-using-api-v3-php-library-in-wordpress%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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 SendGridMailMail(); 
$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() . "n";
print_r($response->headers());
print $response->body() . "n";
} catch (Exception $e) {
echo 'Caught exception: '. $e->getMessage(). "n";
}


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']);
}





share|improve this answer


























  • Thanks. Your solution will work in the limited situation of adding emails to "To" field. However, the article you cited about addTos being deprecated is over 3 years old and was written about an older api. The current api v3 does use addTos and uses the same format for addCcs and addBccs so I'm still looking for a solution that will address all three.

    – apadley
    Nov 21 '18 at 20:51











  • When your solution is applied to the addBccs it generates the following warnings and no bccs are sent: Warning: current() expects parameter 1 to be array, string given in /sendgrid-php/lib/mail/Mail.php on line 281 Warning: Invalid argument supplied for foreach() in /sendgrid-php/lib/mail/Mail.php on line 291

    – apadley
    Nov 21 '18 at 20:51
















0














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 SendGridMailMail(); 
$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() . "n";
print_r($response->headers());
print $response->body() . "n";
} catch (Exception $e) {
echo 'Caught exception: '. $e->getMessage(). "n";
}


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']);
}





share|improve this answer


























  • Thanks. Your solution will work in the limited situation of adding emails to "To" field. However, the article you cited about addTos being deprecated is over 3 years old and was written about an older api. The current api v3 does use addTos and uses the same format for addCcs and addBccs so I'm still looking for a solution that will address all three.

    – apadley
    Nov 21 '18 at 20:51











  • When your solution is applied to the addBccs it generates the following warnings and no bccs are sent: Warning: current() expects parameter 1 to be array, string given in /sendgrid-php/lib/mail/Mail.php on line 281 Warning: Invalid argument supplied for foreach() in /sendgrid-php/lib/mail/Mail.php on line 291

    – apadley
    Nov 21 '18 at 20:51














0












0








0







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 SendGridMailMail(); 
$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() . "n";
print_r($response->headers());
print $response->body() . "n";
} catch (Exception $e) {
echo 'Caught exception: '. $e->getMessage(). "n";
}


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']);
}





share|improve this answer















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 SendGridMailMail(); 
$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() . "n";
print_r($response->headers());
print $response->body() . "n";
} catch (Exception $e) {
echo 'Caught exception: '. $e->getMessage(). "n";
}


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']);
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 10:12

























answered Nov 21 '18 at 9:33









Jamie_DJamie_D

51938




51938













  • Thanks. Your solution will work in the limited situation of adding emails to "To" field. However, the article you cited about addTos being deprecated is over 3 years old and was written about an older api. The current api v3 does use addTos and uses the same format for addCcs and addBccs so I'm still looking for a solution that will address all three.

    – apadley
    Nov 21 '18 at 20:51











  • When your solution is applied to the addBccs it generates the following warnings and no bccs are sent: Warning: current() expects parameter 1 to be array, string given in /sendgrid-php/lib/mail/Mail.php on line 281 Warning: Invalid argument supplied for foreach() in /sendgrid-php/lib/mail/Mail.php on line 291

    – apadley
    Nov 21 '18 at 20:51



















  • Thanks. Your solution will work in the limited situation of adding emails to "To" field. However, the article you cited about addTos being deprecated is over 3 years old and was written about an older api. The current api v3 does use addTos and uses the same format for addCcs and addBccs so I'm still looking for a solution that will address all three.

    – apadley
    Nov 21 '18 at 20:51











  • When your solution is applied to the addBccs it generates the following warnings and no bccs are sent: Warning: current() expects parameter 1 to be array, string given in /sendgrid-php/lib/mail/Mail.php on line 281 Warning: Invalid argument supplied for foreach() in /sendgrid-php/lib/mail/Mail.php on line 291

    – apadley
    Nov 21 '18 at 20:51

















Thanks. Your solution will work in the limited situation of adding emails to "To" field. However, the article you cited about addTos being deprecated is over 3 years old and was written about an older api. The current api v3 does use addTos and uses the same format for addCcs and addBccs so I'm still looking for a solution that will address all three.

– apadley
Nov 21 '18 at 20:51





Thanks. Your solution will work in the limited situation of adding emails to "To" field. However, the article you cited about addTos being deprecated is over 3 years old and was written about an older api. The current api v3 does use addTos and uses the same format for addCcs and addBccs so I'm still looking for a solution that will address all three.

– apadley
Nov 21 '18 at 20:51













When your solution is applied to the addBccs it generates the following warnings and no bccs are sent: Warning: current() expects parameter 1 to be array, string given in /sendgrid-php/lib/mail/Mail.php on line 281 Warning: Invalid argument supplied for foreach() in /sendgrid-php/lib/mail/Mail.php on line 291

– apadley
Nov 21 '18 at 20:51





When your solution is applied to the addBccs it generates the following warnings and no bccs are sent: Warning: current() expects parameter 1 to be array, string given in /sendgrid-php/lib/mail/Mail.php on line 281 Warning: Invalid argument supplied for foreach() in /sendgrid-php/lib/mail/Mail.php on line 291

– apadley
Nov 21 '18 at 20:51




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401735%2fsendgrid-sending-to-multiple-recipients-using-api-v3-php-library-in-wordpress%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents