[英]SendGrid sending to multiple recipients using api v3 & PHP library in Wordpress function
I am using SendGrid v3 api with the php library to try and send to multiple recipients as part of a WordPress function. 我正在将SendGrid v3 api与php库一起使用,以尝试作为WordPress函数的一部分发送给多个收件人。 I can successfully send emails using the SendGrid sample code and hard coding the multiple recipients.
我可以使用SendGrid示例代码成功发送电子邮件,并对多个收件人进行硬编码。 However, when I query the database to try build the list of to: email addresses it always fails with a 400 error.
但是,当我查询数据库以尝试建立收件人列表:电子邮件地址时,它总是失败并显示400错误。 Here is the SendGrid code I am working with.
这是我正在使用的SendGrid代码。 It works fine with live data and hard coded.
它适用于实时数据和硬编码。 However, I can't seem to properly build the $tos array from my database query.
但是,我似乎无法从数据库查询中正确构建$ tos数组。 I have read the documentation and every tutorial I can find.
我已经阅读了文档以及所有可以找到的教程。 I also contacted Sendgrid support.
我还联系了Sendgrid支持。
$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() . "\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
" ); 这是我的WordPress查询:$ 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? 我如何从我的数据库查询中正确编码$ tos变量,以使$ email-> addTos($ tos)不会出错?
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". 根据此页面的说明 ,
addTos
函数已失效,并且在SendGrid的API中不再受支持:“将addTos()
方法移至addTo()
,当前不支持传入数组”。
Therefore: 因此:
Using your test
database: 使用
test
数据库:
$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() . "\n";
print_r($response->headers());
print $response->body() . "\n";
} catch (Exception $e) {
echo 'Caught exception: '. $e->getMessage(). "\n";
}
Using wp_users
database: 使用
wp_users
数据库:
$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, 我认为@Jamie_D对
addTos
方法有一些误解,因为按照现在的方法,到2019年6月19日,我正在使用addTos
方法,并且运行得很好,
Edit: and now sendgrid's employee has also confirmed it see this link: https://github.com/sendgrid/sendgrid-php/issues/127#issuecomment-508330993 编辑:现在sendgrid的员工也已确认它看到此链接: 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. 当我第一次使用它时,我也遇到了错误,所以我已经调试并在sendgrid库中打印了一些变量,并且我明白了,可能是您也缺少该点,所以您出错了。
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: 在
$tos
数组中,您必须提供一个关联数组,其中email应该是键,用户名应该是values ,请参见以下示例:
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: 使用wp_users表的示例 :
$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. 尝试一下,它肯定会起作用,如果不能,请在评论中告诉我,谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.