如何从准备好的语句中将电子邮件发送到数组的地址

时间:2014-10-23 19:56:51

标签: php arrays wordpress email

我是一个相当新的php和预备语句。我被要求开发一个电子邮件脚本,向未注册的用户发送提醒电子邮件。

基本上代码是在wordpress中,因此需要使用$ wpdb类。但是,我无法从数组中提取电子邮件地址,然后使用邮件功能发送它们。它只发送到1个电子邮件地址。

$query =  $wpdb->get_results("select contact_name, email_address, password from user where activation_token is NULL");

foreach ($query as $result ) {      
$recipients = $result->email_address;   
        }   


if(!empty($query)) {
    $boundary = uniqid('np');
    $from = 'mydomain';

    //headers - specify your from email address and name here
    //and specify the boundary for the email
    $headers = "MIME-Version: 1.0\r\n";
    $headers .= "From: ". $from ." \r\n";
    $headers .= "To: ".$email."\r\n";
            if (isset($bcc)) {
                $headers .= "Bcc: ".$bcc. "\r\n";
            }
    $headers .= "To: ".$recipient."\r\n";
    $headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";

    //here is the content body
    $message = "This is a MIME encoded message.";
    $message .= "\r\n\r\n--" . $boundary . "\r\n";
    $message .= "Content-type: text/plain;charset=utf-8\r\n\r\n";

    //Plain text body
    $message .= $text_msg;
    $message .= "\r\n\r\n--" . $boundary . "\r\n";
    $message .= "Content-type: text/html;charset=utf-8\r\n\r\n";

    //Html body
    $message .= $html_msg;
    $message .= "\r\n\r\n--" . $boundary . "--";

    $to = $name; 
    $subject = "E-mail subject";
    $headers .= 'BCC: ' .$recipients. "\r\n";

    //error_log($message);
    //error_log($headers);

mail('', $subject, $message, $headers);
echo 'emails have been sent';


}else{

    echo 'failed';
}

1 个答案:

答案 0 :(得分:0)

我不是WordPress专家,但这看起来像是将大量信息转换为一点信息的好方法。

foreach ($query as $result ) {      
    $recipients = $result->email_address;   
}   

这只会记住$recipients

中的最后一个电子邮件地址

mail()函数的第一个参数是收件人地址,您输入的是'',即mail('', $subject, $message, $headers);

我猜你真的想做这样的事情

$query =  $wpdb->get_results("select contact_name, email_address, password from user where activation_token is NULL");

foreach ($query as $result ) {      

    $boundary = uniqid('np');
    $from = 'mydomain';

    //headers - specify your from email address and name here
    //and specify the boundary for the email
    $headers = "MIME-Version: 1.0\r\n";
    $headers .= "From: ". $from ." \r\n";
    if (isset($bcc)) {
       $headers .= "Bcc: ".$bcc. "\r\n";
    }
    $headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";

    //here is the content body
    $message = "This is a MIME encoded message.";
    $message .= "\r\n\r\n--" . $boundary . "\r\n";
    $message .= "Content-type: text/plain;charset=utf-8\r\n\r\n";

    //Plain text body
    $message .= $text_msg;
    $message .= "\r\n\r\n--" . $boundary . "\r\n";
    $message .= "Content-type: text/html;charset=utf-8\r\n\r\n";

    //Html body
    $message .= $html_msg;
    $message .= "\r\n\r\n--" . $boundary . "--";

    $to = $name; 
    $subject = "E-mail subject";
    $headers .= 'BCC: ' .$recipients. "\r\n";

    //error_log($message);
    //error_log($headers);

    if ( mail($result->email_address, $subject, $message, $headers) ) {
        echo 'emails have been sent';
    } else {
        echo 'emailing failed';
    }
}