作为WordPress函数的一部分,我正在将SendGrid v3 api与php库一起使用,尝试发送给多个收件人。我可以使用SendGrid示例代码成功地发送电子邮件,并对多个收件人进行硬编码。然而,当我查询数据库以尝试构建收件人:电子邮件地址的列表时,它总是失败,并出现400错误。这是我正在使用的SendGrid代码。它适用于实时数据和硬编码。然而,我似乎无法从数据库查询中正确构建$tos数组。我已经阅读了文档和我能找到的每一个教程。我还联系了Sendgrid的支持人员。
$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";
}
这是我的WordPress查询:$sends=$wpdb->get_results("SELECT*FROMtest
"(;
如何正确编码数据库查询中的$tos变量,使$email->addTos($tos(不会出错?
谢谢。
根据此页面,addTos
函数已不存在,并且在SendGrid的API中不再受支持:"addTos()
方法已移动到addTo()
,并且当前不支持传入数组"。
因此:
使用test
数据库:
$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";
}
使用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']);
}
我认为@Jamie_D对addTos
方法有一些误解,因为截至2019年6月19日,我正在使用addTos
方法,它运行得很好,
编辑:现在sendgrid的员工也确认了,请查看此链接:https://github.com/sendgrid/sendgrid-php/issues/127#issuecomment-508330993
当我第一次使用它时,我也遇到了错误,所以我在sendgrid库中调试并打印了一些变量,我得到了要点,可能你也错过了那个要点,所以你得到了错误。
在$tos
数组中,您必须提供关联数组,其中电子邮件应为关键字,用户名应为值,请参见以下示例:
语法:
$tos = [
//user emails => user names
"user1@example.com" => "Example User1",
"user2@example.com" => "Example User2",
"user3@example.com" => "Example User3"
];
$email->addTos($tos);
使用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);
试试看,它肯定会起作用,如果不起作用,请在评论中告诉我,谢谢。