【发布时间】:2016-04-07 03:42:45
【问题描述】:
我正在尝试使用 PHP mail() 从数据库中密件抄送订阅者列表。一切正常,但我遇到了一个困扰我一上午的问题。我可以使用密件抄送发送列表,但无法将接收端电子邮件地址附加到更死的“收件人:”。
例如,我将列表发送到以下电子邮件地址(test1@example.com、test2@example.com 和 test3@example.com)。每个电子邮件地址都会收到一封电子邮件,而其他电子邮件地址由于密件抄送而被隐藏。
我的问题是,在标题中,“收件人:”在列表的所有接收端都显示为空白。我理解并知道该标头不会显示,因为我在传出消息中只有 BCC 标头。我已经尝试for 循环这个过程,但我收到了所有的电子邮件,加上一个循环中的地址。
这是我的工作代码:工作代码包括我尝试解决 To: 标头的循环。尽管我收到了所有已发送的电子邮件,但我可以发送电子邮件。
<?php
/*
Gathers the number of rows within the database. Used for the loop that displays the entire list in the BCC.
*/
session_start();
include_once '../dbconnect.php';
$result = mysql_query("SELECT * FROM news");
$num_rows = mysql_num_rows($result);
$rows = $num_rows;
/*
Requests the list from the database, please the list in a loop, displays the list only once, and places list in an operator.
*/
$result = mysql_query("SELECT * FROM `news`");
while($row = mysql_fetch_array( $result )) {
for ($x = 1; $x <= 1; $x++) {
$contacts.= "".$row['email'].",";
}
}
/*
ATTEMPT (It works... sort of): Sends mail to the list using BCC, displays the ONLY receivers email address in the To: header
*/
$result = mysql_query("SELECT * FROM `news`");
while($row = mysql_fetch_array( $result )) {
for ($x = 1; $x <= 1; $x++) {
$receiver= "".$row['email']."";
$to = "$receiver";
$subject = 'Test Email';
$headers = "From: example@example.com\r\n";
$headers .= "BCC: $contacts";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<h1 style="">Test Message</h1>';
$message .= '</body></html>';
mail($to,$subject, $message, $headers);
}
}
?>
我的一般想法是循环,但我找不到真正完全解决这个问题的解决方案,由 BBC 到列表并在 To: 标题中仅显示接收端电子邮件地址。有什么想法或想法吗?
使用 SMTP 服务器更新
我一直在尝试使用此线程中的解决方案并将其应用于 SMTP 服务器。使用 SendGrid 发送消息是理想的选择。我提出了以下选项,但脚本似乎只向数据库中的一个地址发送一条消息,而不是所有地址。
<?php
require_once "Mail.php";
$sub = $_POST['subject'];
$ttl = $_POST['title'];
$img = $_POST['image'];
$bdy = $_POST['body'];
$lk = $_POST['link'];
mysql_connect("", "", "") or die(mysql_error()) ;
mysql_select_db("") or die(mysql_error()) ;
$result = mysql_query("SELECT `email` FROM news");
$num_rows = mysql_num_rows($result);
$receivers = array();
while ($row = mysql_fetch_array($result)) {
$receivers[] = $row['email'];
}
foreach ($receivers as $receiver) { }
$from = "test@example.com";
$to = $receiver;
$subject = $sub;
$mime = "1.0";
$ctype = "text/html; charset=ISO-8859-1";
$body = '
<html><body>
<p>Test Message!</p>
</body></html>
';
$host = "";
$port = "";
$username = "";
$password = "";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject,
'MIME-Version' => $mime ,
'Content-Type:' => $ctype);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
【问题讨论】:
-
只通过“to”发送邮件怎么样?为什么 BCC 有这么多麻烦?您的服务器发出的邮件数量是否有限?
-
不,我没有限制。我从列表中隐藏了地址,因为这是用于客户邮件列表的脚本。
-
是的,我知道,但是,让我用代码快速回答一下我的实际意思;)
-
啊,我明白了。谢谢!