我已经被困在这几天了。 Sendgrid需要双引号和逗号分隔的值。当我打印$emaildata
时,我得到所需格式的值。我在运行脚本时也收到了成功消息,但没有发送电子邮件。我和curl
尝试过相同但结果相同。我感觉这就是我提取数据的方式。任何帮助将不胜感激。
// Get List Data from form, several lists can be selected
$lists = $_POST['lists_array'];
$values = array_map('array_pop', $lists);
$imploded = implode(',', $values);
// Query the database for the selected lists
$stmt = $db->prepare("SELECT * FROM subscribers WHERE list_name = '$imploded'");
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Select all email ids from the given lists
$edata= array_column($rows, 'subscriber_email');
// Put email ids into quoted, coma seperated format.
$emaildata = '"'.implode('", "', $edata).'"';
// SendGrid PHP Library
require("sendgrid-php.php");
/* USER CREDENTIALS
====================================================*/
$sg_username = "XXXX";
$sg_password = "XXXX";
/* CREATE THE SENDGRID MAIL OBJECT
====================================================*/
$sendgrid = new SendGrid( $sg_username, $sg_password );
$mail = new SendGrid\Email();
/* SMTP API
====================================================*/
// ADD THE RECIPIENTS
$emails = array (
$emaildata
);
$mail->setSmtpapiTos($emails);
/* SEND MAIL
====================================================*/
try {
$mail->
setFrom( "$fromemail" )->
setSubject( "$subjectline" )->
setText( "$previewmessage" )->
setHtml( "$campaign_html" );
$sendgrid->send( $mail );
echo "Success";
} catch ( Exception $e ) {
echo "Unable to send mail: ", $e->getMessage();
}
答案 0 :(得分:1)
好的我已经将它排序了。而不是像他们的例子那样提供昏迷分离值的头痛,只是将数据存储在一个数组中。然后我们将其传递给sendgrid。我们最多可以传递10k封电子邮件。
$storeArray[] = $row['subscriber_email'];
然后将此数组传递给sendgrid,就像这样
$emails = $storeArray;
$mail->setSmtpapiTos($emails);
所以最终的代码看起来像这样。我确信还有其他方法可以做到这一点但是如果有人需要一个有效的例子,你可以使用下面的内容。我希望有人觉得这很有用:)
// Get List Data from form, several lists can be selected
$lists = $_POST['filter_array'];
$values = array_map('array_pop', $lists);
$imploded = implode(',', $values);
// Query the database for the selected lists
$stmt = $db->prepare("SELECT * FROM subscribers WHERE subscriberlist_id IN ($imploded) AND subscriber_status='enabled'");
$stmt->execute();
while ($row = $stmt->fetch()) {
$storeArray[] = $row['subscriber_email']; // Put email ids into an array.
}
// SendGrid PHP Library
require("sendgrid-php.php");
/* USER CREDENTIALS
/ Fill in the variables below with your SendGrid
/ username and password.
====================================================*/
$sg_username = "XXXX";
$sg_password = "XXXX";
/* CREATE THE SENDGRID MAIL OBJECT
====================================================*/
$sendgrid = new SendGrid( $sg_username, $sg_password );
$mail = new SendGrid\Email();
/* SMTP API
====================================================*/
// ADD THE RECIPIENTS
$emails = $storeArray; // Passing the array of email ids.
$mail->setSmtpapiTos($emails);
/* SEND MAIL
====================================================*/
try {
$mail->
setFrom( "$fromemail" )->
setSubject( "$subjectline" )->
setText( "$previewmessage" )->
setHtml( "$campaign_html" );
$sendgrid->send( $mail );
echo "Success";
} catch ( Exception $e ) {
echo "Unable to send mail: ", $e->getMessage();
}