我不擅長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>
歡迎來到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
你沒有說什麼是你的問題與foreach。你是否試圖迴應foreach結果 – zod 2012-01-12 18:43:36
是的,這需要更多更好的信息。 – 2012-01-12 18:45:55