使用PHP发送电子邮件收件人

时间:2012-01-12 18:37:54

标签: php phpmailer

我不擅长PHP所以需要一些帮助。我有一个带有复选框的php页面,我试图向多个收件人发送邮件。我可以发送邮件,但没有什么问题。当我选择例如。 3复选框发送电子邮件然后第一个电子邮件收件人是好的,但第二个电子邮件与第一和第二个收件人和第三个电子邮件第1,第2,第三个收件人我猜有'foreach'的问题。有人请帮我用MySQL查询发送个人电子邮件给个人收件人。

这是我的mail.php页面的代码

<?php
require_once('auth.php');


<html>
<head>
<title>PHPMailer - SMTP basic test with authentication</title>
</head>
<body>


include("Connections/connection.php");
//error_reporting(E_ALL);
error_reporting(E_STRICT);

date_default_timezone_set('Europe/Dublin');

require_once('php_mailer/class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from withinclass.phpmailer.php   if not already loaded

$mail = new PHPMailer();


//$body = file_get_contents('contents.php');
//$body = eregi_replace("[\]",'',$body);

$sender_name        =   $_SESSION['sender_name'];
$sender_email       =   $_SESSION['sender_email'];
$sender_password    =   $_SESSION['sender_password'];


$id_user    =   $_POST["id_user"];

foreach ($id_tariff as $idt)
{
$query = sprintf("SELECT From_Date, To_Date, first, last, city, country, Email_1, Email_2, account_name FROM user_info where id_user = $id_user");
$result = mysql_query($query) or die(mysql_error());


$body = "
<table width='100%' border='1' cellspacing='0' cellpadding='3' bordercolor='#ffcccc'>
<tr>
<th bgcolor='#cc3333'>From</th>
<th bgcolor='#cc3333'>To</th>
<th bgcolor='#cc3333'>First Name</th>
<th bgcolor='#cc3333'>Last Name</th>
<th bgcolor='#cc3333'>City</th>

<th bgcolor='#cc3333'>country</th>

</tr>


";


while($row = mysql_fetch_array($result)){
$body .="<tr>";
$body .="<td>".$row['From_Date']."</td>";
$body .="<td bgcolor='#FFE8E8'>".$row['To_Date']."</td>";
$body .="<td>".$row['first']."</td>";
$body .="<td bgcolor='#FFE8E8'>".$row['last']."</td>";
$body .="<td>".$row['city']."</td>";
$body .="<td bgcolor='#FFE8E8'>".$row['country']."</td>";
$body .="</tr>";
$to1 = $row['Email_1'];
$to2 = $row['Email_2'];
$account_name = $row['account_name'];
}
$body .="</table>";


$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com"; // sets the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "$sender_email"; // SMTP account username
$mail->Password = "$sender_password"; // SMTP account password


$mail->SetFrom($sender_email,$sender_name);

$mail->AddReplyTo("$sender_email","$sender_name");


$mail->Subject = "Hello Dear $account_name";


$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";

$mail->MsgHTML($body);



$mail->AddAddress($to1,$account_name);

$mail->AddAddress($to2,$account_name);




if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "YOUR E-MAIL HAS SENT";

}


}
?>

</body>
</html>

1 个答案:

答案 0 :(得分:2)

每封邮件发出后,您必须致电$mail->ClearAddresses()。您不是在每封邮件后重置PHPMailer对象,而AddAddress()完全按照它所说的那样 - 在“收件人:”列表中添加新地址。

相关问题