【问题标题】:how to send emails to an array off address from prepared statement如何从准备好的语句中将电子邮件发送到地址数组
【发布时间】:2014-12-19 14:35:16
【问题描述】:

我是 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';
}

【问题讨论】:

  • 这与mail()无关。它不关心您从哪里获得电子邮件地址。因此,要么您调用 mail() 函数错误,要么您没有正确检索电子邮件地址。对于这类事情,你真的应该使用 PHPMailer 或 Swiftmailer。它们使发送“复杂”的电子邮件变得容易得多。
  • 我认为这行与它有关if (isset($bcc)) { $headers .= "Bcc: ".$bcc. "\r\n"; }

标签: php arrays wordpress email


【解决方案1】:

我不是 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';
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    • 2015-04-18
    • 1970-01-01
    • 2014-02-14
    相关资源
    最近更新 更多