今天早上,我开始研究一个邮件系统,该邮件系统向多个地址发送邮件,并且只向最终用户显示一个地址,即时事通讯电子邮件。可以找到工作代码here。现在,我试图通过SMTP服务器传递循环。这是捆绑邮件以允许更安全的传递。我的问题是传递这些标题并获取消息以进入SMTP服务器。我使用PEAR获取对SendGrids SMTP的访问权限。我收到一条错误消息,指出" $ header必须是数组 "。
我首先打破了Rackspace提供的这种模式。
<?php
require_once "Mail.php";
$from = "Bill Gates Jr. <billy@example.com>";
$to = "Steve Ballmer Sr. <Stevie@example.com>";
$subject = "Secrete Microsoft Project\r\n\r\n";
$body = "Yo this is a test message";
$host = "smtp.sendgrid.net";
$port = "587";
$username = "Account_Username";
$password = "Account_Password";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$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:标题中看到他们的地址。我没有使用SMTP here。
这是我试图做的事情。允许SMTP服务器安全地发送邮件。
<?php
$sub = $_POST['subject'];
$ttl = $_POST['title'];
$img = $_POST['image'];
$bdy = $_POST['body'];
$lk = $_POST['link'];
require_once "Mail.php";
/*
Gathers the number of rows within the database. Used for the loop that displays the entire list in the BCC.
*/
$host = "smtp.sendgrid.net";
$port = "587";
$username = "Account_Username";
$password = "Account_Password";
mysql_connect("MYSQL_HOST", "Username", "Password") or die(mysql_error()) ;
mysql_select_db("Database") or die(mysql_error()) ;
// Select only what you really need from the table. This saves you memory
// and it speeds up the query.
$result = mysql_query("SELECT `email` FROM news");
// You are not using these numbers in the script you showed us. I am just
// leaving them in here to show you, how you can reuse the "$result"
// variable without querying the database again.
$num_rows = mysql_num_rows($result);
// We are reusing the "$result" here without re-querying the database, which
// speeds the whole process up and takes load away from the database. We are
// storing all receivers in a dedicated variable, to reuse them later on.
$receivers = array();
while ($row = mysql_fetch_array($result)) {
$receivers[] = $row['email'];
}
// Now, instead of querying the database again, we are using our stored mail
// addresses in "$receivers" to send the emails one by one. We could have
// done this part in the "while" loop before, but I wanted to stay close to
// your code, so you would recognize it ;)
foreach ($receivers as $receiver) {
// I have removed the "for" loop here, because it runs only once. If a loop
// only runs once and you control all its input, you really do not need a
// loop at all (except some exceptions, just in case someone knows one^^).
// You can actually just put the value of $receiver in $to. PHP is pretty
// good at typecasting of scalar types (integers, strings etc.), so you do
// not need to worry about that.
$to = $receiver;
$subject = $sub;
// I am putting the headers into an array and implode them later on. This
// way we can make sure that we are not forgetting the new line characters
// somewhere.
$headers = array(
"From: test@example.com",
"MIME-Version: 1.0",
"Content-Type: text/html; charset=ISO-8859-1",
// I have removed the "BCC" header here, because this loops send out an
// email to each user seperately. It is basically me sending an email to
// you. Afterwards I copy&paste all the content to another new mail and
// send it to another fella. You would never know that you both got the
// same mail ;)
);
$message = '
<html><body>
<p>Test Message</p>
</body></html>
';
// Imploding the headers.
$imploded_headers = implode("\r\n", $headers);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $subject, $message, $imploded_headers);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
}
?>
从运行脚本开始,我发现必须创建头字符串而不是数组。我尝试过并仍然收到相同的错误消息。
如何通过SMTP传递邮件,我该怎么做?
更新
我设法让它发送消息,虽然它是空的。出于某种原因,文件会附加到邮件中。该文件显示为&#34; noname&#34;大小为1.4KB。
<?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>");
}
?>
我排除了$imploded_headers = implode("\r\n", $headers);
,因为这最初给了我错误&#34; $ header需要是数组&#34;。有什么想法吗?