【发布时间】: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