2012-05-11 4 views
2

私は問題があります。私が送信ボタンをクリックすると、ちょうど[email protected]がメッセージを受け取ります。phpmailerでPHPとMySQLで複数のメールを送信するには

変更方法私はGmailのSMTP送信を使用しています。

、データベース内の2つのレコードがあります

がここには私のコードです:

include "phpmailer/class.phpmailer.php"; 
$host = "localhost"; 
$user = "root"; 
$pass = "root"; 
$db = "mailTest"; 

mysql_connect($host, $user, $pass); 
mysql_select_db($db); 

$query = "SELECT email FROM list"; 

$recordset = mysql_query($query); 
$row_recordset = mysql_fetch_assoc($recordset); 
$tota_row_recordset = mysql_num_rows($recordset); 
$msg = strip_tags($_POST['msg']); 
$mail= new PHPMailer(); //建立新物件 
while($row = mysql_fetch_array($recordset)) 
{ 
    $to = $row['email']; 
    $mail->AddAddress($to, "Test Message"); 
}  
$mail->IsSMTP();  
$mail->SMTPAuth = true; 
$mail->SMTPSecure = "ssl"; 
$mail->Host = "smtp.gmail.com";  
$mail->Port = 465;  
$mail->CharSet = "big5"; 
$mail->Subject = "Test Message";  
$mail->Username = "xxxxxx";  
$mail->Password = "xxxxxx"; 
$mail->Body = "$msg"; 
$mail->IsHTML(true);   
if(!$mail->Send()) {  
echo "Mailer Error: " . $mail->ErrorInfo;  
} else {  
header('Location: index.php'); 
} 

答えて

3

を、最後の方のみメールを受け取ります。

while($row = mysql_fetch_array($recordset)) 
{ 
    $to = $row['email']; 
} 
$mail = new PHPMailer(); 

$mail = new PHPMailer(); // create object FIRST 
while($row = mysql_fetch_array($recordset)) 
{ 
    $to = $row['email']; 
    $mail->AddAddress($to, "Test Message"); // add each DB entry to list of recipients 
} 
にコードを変更し

+0

私が$ mail-> AddAddress($ to、 "Test Message")を置くと、 Webページにエラーが表示されます。 – tommychoo

+0

どのようなエラーが表示されますか? –

+0

@tommychooこれを修正するための例が変更されました。もう一度やり直してください – Kaii

2

は、以下の本試してください:あなたは、各反復をレシピエントに上書きこのループによって

while($row = mysql_fetch_array($recordset)) 
{ 
    $to = $row['email']; 
    $mail= new PHPMailer(); 
    $mail->IsSMTP(); 

    $mail->SMTPAuth = true; 

    $mail->SMTPSecure = "ssl"; 

    $mail->Host = "smtp.gmail.com"; 

    $mail->Port = 465; 

    $mail->CharSet = "big5"; 

    $mail->Subject = "Test Message"; 

    $mail->Username = "xxxxxx"; 

    $mail->Password = "xxxxxx"; 

    $mail->Body = "$msg"; 

    $mail->IsHTML(true); 

    $mail->AddAddress($to, "Test Message"); 

    if(!$mail->Send()) { 

    echo "Mailer Error: " . $mail->ErrorInfo; 
    } 
}  
+1

本当にすべてのサイクルで新しいphpmailerのをインスタンス化すべきではありません! – dmp

+0

このコードを試してみますが、問題はまだ存在します。 – tommychoo

0
if(isset($_POST['sbmit'])) 
{ 
    for($i=0;$i<count($_POST['email_address']);$i++) 
    { 

      $to = $_POST['email_address'][$i]; 
      echo $to."<br />"; 
      $subject = 'Thank you for Subscribing'; 
      $message = $_POST['news']; 
      $headers = 'From: [email protected]' . "\r\n" . 
      'Reply-To: [email protected]' . "\r\n" . 
      'X-Mailer: PHP/' . phpversion(); 


     echo $_POST['email_address'][$i]."<br />"; 
     ini_set("SMTP","smtp.ntc.net.np"); 
     ini_set('sendmail_from', '[email protected]'); 
     if(mail($to,$subject,$message,$headers)) 
     { 
       echo "Mail has been sent to admin<br />"; 
     } 
     else 
     { 
       echo "Mail could not been sent"; 
     } 

    } 
} 


?> 
関連する問題