2012-01-12 157 views
0

我不擅长PHP,所以需要一些帮助。我有一个php页面与复选框从哪里我试图发送邮件给多个收件人。我可以发送邮件,但没有问题。当我选择例如。 3复选框发送电子邮件,然后第一个电子邮件收件人是好的,但第二封电子邮件与第一和第二收件人和第三封邮件去第一,第二,第三收件人。我想有'foreach'的问题。有人会帮助我用我的MySQL查询发送单个邮件给个人收件人。使用PHP发送电子邮件收件人

这里是我的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

欢迎来到Stack Overflow!你显示的代码容易受到[SQL注入](http://php.net/manual/en/security.database.sql-injection.php)的影响。使用适当的卫生方法(例如'mysql_real_escape_string()'用于传统的mysql库),或切换到PDO并准备好语句。 – 2012-01-12 18:43:31

+1

你没有说什么是你的问题与foreach。你是否试图回应foreach结果 – zod 2012-01-12 18:43:36

+0

是的,这需要更多更好的信息。 – 2012-01-12 18:45:55

回答

2

你必须调用$mail->ClearAddresses()每个邮件熄灭后的代码。您不会在每封邮件后重置PHPMailer对象,并且AddAddress()完全按照它的说法 - 向“To:”列表中添加新地址。

+0

Hello Marc B,非常感谢您的回复。 我只是把这个放在这里。它不适合我。但是我有另一种解决方案。我只是叫$ mail = new PHPMailer();在while循环之后。和它的工作。 (!$ mail-> Send()){ $ mail-> ClearAddresses(); 回声“邮件错误:”。 $ MAIL-> ERRORINFO; }其他{ 回声“您的电子邮件已发送”; } – Rezbin 2012-01-13 06:17:43

+0

这是没有意义的。 “如果邮件发送失败,清除地址”,这意味着如果邮件没有问题,你仍然会发送重复邮件。在重新开始循环之前,您应该无条件地清除地址。 – 2012-01-13 14:05:58