2014-02-28 3 views
0

phpmailer를 사용하여 데이터베이스에있는 각 전자 메일 주소로 전자 메일을 보내려고하지만 고유 한 전자 메일로 사용하려고합니다. 웬일인지, 그것은 중복되는 전자 메일을 보내고, 내 쿼리가 행을 반환 할 때만큼 많은 복사본을 전송합니다. 따라서 내 쿼리가 5 개의 행을 반환하면 각받는 사람에게 5 개의 전자 메일이 전송됩니다 (총 전자 메일은 25 개입니다). 이메일 내용이 개인화되어 있기 때문에 여러 수신자에게 동일한 이메일을 사용할 수 없습니다.mysql 결과가있는 phpmailer

코드에 어떤 오류가 있습니까? 도와주세요 ...

여기 내 코드입니다 :

$customers_query = "SELECT customer_name, customer_email, customer_id FROM customers"; 

    $customers = mysql_query($customers_query); 

    if (!$customers) { 
     $message = 'Error notice: ' . mysql_error() . "\n"; 
     $message .= 'Whole query: ' . $query; 
    die($message); 
    } 

    require_once('class.phpmailer.php'); 
    // create an instance 

    while ($row = mysql_fetch_array($customers)) { 
     $email = $row['customer_email']; 
     $name = $row['customers_name']; 
     $id = $row['customer_id']; 
     $mail = new PHPMailer(); 
     // use sendmail for the mailer 
     $mail->IsSendmail(); 
     $mail->SingleTo = true; 
     $mail->IsHTML(true); 
     $mail->From = "[email protected]"; 
     $mail->FromName = "MyWebsite"; 
     $mail->Subject = "Welcome to MyWebsite"; 
     $mail->AddAddress($email); 

     $mail->Body = "Dear ".$name.", welcome to MyWebsite. Your ID is: ".$id.". Enjoy your stay."; 
     $mail->Send(); 
    } 

그래서, 여기에 무엇을 놓친 거지가? 왜 많은 이메일을 보냅니 까?

+0

모든 이메일이 똑같은 사본을 보내고 있습니까? 아니면 각기 다른 이름과 ID를 가진 여러 개의 이메일을 보내고 있습니까? – mathius1

+0

사이드 노트와 마찬가지로 : mysql 함수는 더 이상 사용되지 않습니다. mysqli 나 PDO를 더 안정적이고 안전하게 사용하십시오. –

답변

0

다음과 같은 방법을 시도해보십시오. 행을 어떻게 든 계산하여 다시 처리하지 않아야합니다. 또한, AddAddress(); 함수는 TO : 필드에 전자 메일 주소를 계속 추가하는 데 사용되므로 ClearAddresses(); 새로운 수신자 세트를 사용하여 다시 시작하십시오.

$customers_query = "SELECT customer_name, customer_email, customer_id FROM customers"; 

    $customers = mysql_query($customers_query); 
    $count = mysql_num_rows($customers); 
    $result = mysql_fetch_array($customers); 
    $i = 0; 

    if (!$customers) { 
     $message = 'Error notice: ' . mysql_error() . "\n"; 
     $message .= 'Whole query: ' . $query; 
    die($message); 
    } 

    require_once('class.phpmailer.php'); 
    // create an instance 

    while ($i < $count) { 
     $email = mysql_result($result,$i,"customer_email"); 
     $name = mysql_result($result,$i,"customers_name"); 
     $id = mysql_result($result,$i,"customer_id"); 
     $mail = new PHPMailer(); 
     // use sendmail for the mailer 
     $mail->IsSendmail(); 
     $mail->SingleTo = true; 
     $mail->IsHTML(true); 
     $mail->From = "[email protected]"; 
     $mail->FromName = "MyWebsite"; 
     $mail->Subject = "Welcome to MyWebsite"; 
     $mail->AddAddress($email); 

     $mail->Body = "Dear ".$name.", welcome to MyWebsite. Your ID is: ".$id.". Enjoy your stay."; 
     $mail->Send(); 
     $mail->ClearAddresses(); 
     $i++; 
    } 
+0

도움이되지 않습니다, 나는 여전히 중복 이메일을받습니다. 모든 phpmailer 코드를 제거하고 간단한'echo mysql_result ($ customers, $ i, "customer_email")을 추가했습니다. '-'. $ i;'루프 안에서 올바른 결과 ('[email protected] - 1, [email protected] - 2')를 얻지 만, 전자 메일을 보내면 두 전자 메일을 모두받습니다. 정확히 동일합니다. 결과가 3면 동일한 이메일 3 부를 보냅니다. 어떤 아이디어? – user1078494

+0

조사해 봤어. 내 대답을 편집 했으므로 문제가 해결 될 것입니다. – RugerSR9

+0

중복을 제거해야하며 데이터베이스에서 찾은 각 주소로 전자 메일을 보내야합니다. – RugerSR9

1

이 시도 :

$mail->ClearAddresses(); after $mail->Send(); 
+0

그 일찍 시도한 첫 번째 것들 중 하나 였지만, 만약 내가 그렇게한다면, 첫 번째 수신자는 이메일을받습니다 , 다른 사람들은 아무것도 얻지 못합니다 ... – user1078494

0

당신은 한 가지 더 할 수있는, 예 또는 아니오, is_email_sent처럼, 고객 테이블에 하나 개 더 추가 필드를 추가합니다. 이메일을 보내면 기본 키를 사용하여 특정 행을 업데이트 할 수 있습니다. 그래서 여러 번 같은 이메일을 보내지는 않을 것입니다.