Sending newsletter using phpmailer

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kkonline
Forum Contributor
Posts: 251
Joined: Thu Aug 16, 2007 12:54 am

Sending newsletter using phpmailer

Post by kkonline »

Hi,
I am using the following code to send mails (the emails are stored in the database).

The problem is If there are 5 mails in the database then the following code sends 5 emails to each of the 5 emails... i mean mail1@test.com then the second mail has to recipients as mail1@test.com and mail2@tset.com then the third recipient gets cc as mail1@test.com, mail2@tset.com and mail3@test.com and so on... and the loop continues ... what to do

Code: Select all

$subject="Your friend sends you an interesting link";
$message="Greetings,

Your friend wants you to check the following link:
http://test.com";

$mail->FromName = "Test";
$mail->From = "test@abc.com";

$mail->AddReplyTo("test@abc.com", "Test");

$mail->Subject = $subject;
$mail->Body = $message;
$mail->WordWrap = 65;

$sql = "SELECT * FROM contacts";
$result = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_array($result)){
$value=$row['email'];

$mail->AddAddress($value);

if(!$mail->Send())
{
   echo 'Message was not sent.';
   echo 'Mailer error: ' . $mail->ErrorInfo;
}
else
{
  echo 'Message has been sent.';

}
}
I want to send the newsletter something like blind carbon copy .
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

I forget the specifics of phpmailer but you have to clear the mailing list. There is a reset or clear method. Be sure to call it after the send mail call.
Also be very careful with user submitted data when using PHPMailer. It does absolutely no sanitization. Take a look at SwiftMail if you want something more secure and faster.
Post Reply