sendgrid php 库将电子邮件转换成引号逗号分隔值

sendgrid php library get emails into quoted coma separated values

我已经坚持了几天了。 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();
}

好的,我已经解决了。而不是像他们的例子那样令人头疼的提供逗号分隔值,我们只是将数据存储在一个数组中。然后我们将其传递给 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();
}