2012-01-12 26 views
0

私はPHPでうまくいかないので、いくつかのhelp.Iが複数の受信者にメールを送信しようとしているところからチェックボックス付きのPHPページが必要です。私はメールを送ることができますが、ほとんど問題はありません。私は例えば選択します。 3つのチェックボックスをオンにすると1番目のメール受信者はOKですが、2番目のメールは1番目と2番目の受信者に、3番目のメールは1番目、2番目、3番目の受信者に送信されます。私は 'foreach'に問題があると思う。誰かが私のMySQLクエリの個々の受信者に個々​​の電子メールを送信するのに役立つでしょうか?ここでPHPを使用して電子メールの受信者を送信する

はあなたがそれぞれのメールが消えた後$mail->ClearAddresses()を呼び出す必要があり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

ようこそスタックオーバーフロー!あなたが表示するコードは、[SQLインジェクション](http://php.net/manual/en/security.database.sql-injection.php)に脆弱です。ライブラリ(mysql_real_escape_string()のような古典的なmysqlライブラリのような)の適切な衛生メソッドを使用するか、PDOとプリペアドステートメントに切り替えます。 –

+1

あなたはforeachであなたの問題が何なのか言っていませんでした。あなたforeachの結果をエコーし​​ようとしました – zod

+0

ええ、これはより多くのより良い情報が必要です。 –

答えて

2

ための私のコードです。あなたは各メールの後でPHPMailerオブジェクトをリセットしておらず、AddAddress()はそれを正確に行います - 新しいアドレスを 'To:'リストに追加します。

+0

こんにちはマークB、あなたの返信のために多くのおかげです。 私はちょうどここに入れます。それは私のために働いていない。しかし私は別の解決策を得た。私は$ mail = new PHPMailer()を呼び出したばかりです。 whileループの直後。とその働き。 if(!$ mail-> Send()){ $ mail-> ClearAddresses(); echo "メーラーエラー:" $ mail-> ErrorInfo; } else { echo "あなたのEメールが送信されました"; } – Rezbin

+0

これは意味をなさない。 "メールの送信に失敗した場合は、アドレスをクリアする"と表示されます。これは、メールが消えた場合でも、重複して送信されることを意味します。ループをもう一度開始する前に、アドレスを無条件にクリアする必要があります。 –

関連する問題