Jump to content

PHP MYSQL Newsletter issue


simple_man_11

Recommended Posts

Even though the below query/array pulls back only one email address, it sends two emails to the same address...  Basically it sends a duplicate email and I am not sure why as the code only runs once... and stores the email in the .txt file below..  any help would be great, thanks in advance !

 

 

<?php

include ('config.php');

//if ($newsletter==1)
//{

$query = "select email from test ";
$result= mysql_query($query);

  while ($array= mysql_fetch_assoc($result)) 
{
 $emailit = $array["email"] ;



require_once ('class.phpmailer.php');

$mail = new PHPMailer();

$mail->IsSMTP();                                   // send via SMTP
$mail->Host     = "localhost"; // SMTP servers
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->Username = "*******";  // SMTP username
$mail->Password = "******"; // SMTP password


$mail->From     = "dana@****.org";
$mail->FromName = "Reveille Newsletter";
//$mail->AddAddress = "$emailit" ; 
$mail->AddAddress("$emailit"); 
// optional name 
//$mail->AddAddress("eric@***.org");               
$mail->AddReplyTo("dana@***.org");


$mail->WordWrap = 50;                              // set word wrap
//$mail->AddAttachment("/var/tmp/file.tar.gz");      // attachment
//$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); 
$mail->IsHTML(true);                               // send as HTML


$mail->Subject  ="Newsletter";
$mail->Body     =  "This is a test to see if you received this email.  If you would like to get emails from Pointman at a different email address, please let us know." ;

$mail->AltBody  =  "This is the text-only body";

if(!$mail->Send())
{
  // echo "Message was not sent, please try again. <p>";
  // echo "Mailer Error: " . $mail->ErrorInfo;
   //write bad email address to file
   $File = "email_error.txt";
       $Handle = fopen($File, "a");
       fwrite($Handle, $emailit );
	fwrite($Handle, "\n" );
       fclose($Handle); 
   //exit;
}

// message is sent!

//echo "<meta http-equiv='refresh' content='3; url=http://www.***.org'>";

//echo "<center><br>A Represenative will be contacting you shortly about your inquery.</center>";
echo "email sent to the email address in file: $emailit";
		if($mail->Send())
	{
	 //  echo "Message was not sent, please try again. <p>";
	  // echo "Mailer Error: " . $mail->ErrorInfo;
	   //write bad email address to file
	   $File = "email_good.txt";
	   $Handle = fopen($File, "a");
	   fwrite($Handle, $emailit );
	   fwrite($Handle, "\n" );
	   fclose($Handle); 
	   //exit;
	}


}

//}
//else
//{
//echo "No form was submitted !";
//}
?>

Link to comment
https://forums.phpfreaks.com/topic/98086-php-mysql-newsletter-issue/
Share on other sites

I believe your calling $mail->Send(); twice = 2 emails.

<?php
if(!$mail->Send())
{
  // echo "Message was not sent, please try again. <p>";
  // echo "Mailer Error: " . $mail->ErrorInfo;
   //write bad email address to file
   $File = "email_error.txt";
       $Handle = fopen($File, "a");
       fwrite($Handle, $emailit );
	fwrite($Handle, "\n" );
       fclose($Handle); 
   //exit;
}

// message is sent!

//echo "<meta http-equiv='refresh' content='3; url=http://www.***.org'>";

//echo "<center><br>A Represenative will be contacting you shortly about your inquery.</center>";
echo "email sent to the email address in file: $emailit";
		if($mail->Send())
	{
	 //  echo "Message was not sent, please try again. <p>";
	  // echo "Mailer Error: " . $mail->ErrorInfo;
	   //write bad email address to file
	   $File = "email_good.txt";
	   $Handle = fopen($File, "a");
	   fwrite($Handle, $emailit );
	   fwrite($Handle, "\n" );
	   fclose($Handle); 
	   //exit;
	}


}

//}
//else
//{
//echo "No form was submitted !";
//}
?>

 

I think you want:

 

<?php
if(!$mail->Send())
{
  // echo "Message was not sent, please try again. <p>";
  // echo "Mailer Error: " . $mail->ErrorInfo;
   //write bad email address to file
   $File = "email_error.txt";
       $Handle = fopen($File, "a");
       fwrite($Handle, $emailit );
	fwrite($Handle, "\n" );
       fclose($Handle); 
   //exit;
}
else
	{
	 //  echo "Message was not sent, please try again. <p>";
	  // echo "Mailer Error: " . $mail->ErrorInfo;
	   //write bad email address to file
	   $File = "email_good.txt";
	   $Handle = fopen($File, "a");
	   fwrite($Handle, $emailit );
	   fwrite($Handle, "\n" );
	   fclose($Handle); 
	   //exit;
	}
?>

 

That fixed it, thank you !  ;D

 

I figured by saying:

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

 

then

if($mail->Send())
{
           ....
        }

 

it would have taken care of if in the sections only what applied (either the mail was sent, or it was not sent, then do this.) but I guess not.  anyways thanks.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.