使用Wordpress函数中的api v3和PHP库将SendGrid发送到多个收件人

时间:2018-11-20 21:22:35

标签: php wordpress sendgrid-api-v3

我正在将SendGrid v3 api与php库一起使用,以尝试作为WordPress功能的一部分发送给多个收件人。我可以使用SendGrid示例代码成功发送电子邮件,并对多个收件人进行硬编码。但是,当我查询数据库以尝试建立收件人列表:电子邮件地址时,它总是失败并显示400错误。这是我正在使用的SendGrid代码。它适用于实时数据和硬编码。但是,我似乎无法从数据库查询中正确构建$ tos数组。我已经阅读了文档以及所有可以找到的教程。我还联系了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";
}

这是我的WordPress查询:$ sends = $ wpdb-> get_results(“ SELECT * FROM test”);

如何从数据库查询中正确编码$ tos变量,以使$ email-> addTos($ tos)不会出错?

谢谢。

2 个答案:

答案 0 :(得分:0)

根据this pageaddTos函数已失效,并且在SendGrid的API中不再受支持:“ addTos()方法已移至addTo(),目前不支持数组被传递”。

因此:

使用您的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";
}

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

答案 1 :(得分:0)

我认为@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);

尝试一下,它肯定会起作用,如果不能,请在评论中告诉我,谢谢。

相关问题