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
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;
	}
?>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.