MarcusAnthonius Posted April 22, 2010 Share Posted April 22, 2010 Hello PHP/SQL programmers, I would like to send a personalized email with phpmailer class. see my script in this topic. the problem is: when i use it this way: all people get same name in body. and... last person get all emails double with names of others. but this script does send mail to everyone in mailinglist (data from MySQL-database) i am a basic programmer, so please keep the answer simple and just edit my mistakes in my own script, or rewrite it for me so i can learn, haha. thanks in advance! ini_set('display_errors','on'); error_reporting(E_ALL | E_STRICT); date_default_timezone_set("Europe/Paris"); $verzendbutton = isset($_POST['verzendbutton']) ? $_POST['verzendbutton'] : null; $trigger = isset($_POST['trigger']) ? $_POST['trigger'] : null; $submit = isset($_POST['submit']) ? $_POST['submit'] : null; set_time_limit(2500); /* not to high ? */ if ( $trigger != 1) { echo "<form action=".$_SERVER['PHP_SELF']."?pass=example method=\"post\">"; ?> <input type="hidden" value="1" name="trigger"> <input type="submit" value="verzenden" name="verzendbutton"> <?php } else { require_once('../class.phpmailer.php'); $mail = new PHPMailer(); $mail->IsHTML(true); // $mail->AddAttachment('../../mailing_images/logo.gif','logo.gif'); // attachment $mail->SetFrom('[email protected]', 'Afzender'); $mail->AddReplyTo('[email protected]', 'Mark de Winter'); $mail->Subject = "newsletter"; @MYSQL_CONNECT("localhost","user","pass"); @mysql_select_db("database-klanten"); $query = "SELECT id, mailadres, sexe, name, prefix, surname from mailinglist WHERE wished = 'yess' "; $result = @mysql_query($query); $c = mysql_num_rows($result); echo "We have selected $c people<br/><br/>"; $body = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"><html><head><title>Nieuwsbrief</title> <style type=\"text/css\"> body {background:#FFC; color:black;} a:link {color: #006600;text-decoration: underline;} a:visited {color: #006600;text-decoration: underline;} a:hover {color: #FF0000;font-size: 100%; text-decoration: none;} a:active {color: #006600;text-decoration: underline;} </style> </head> \n"; $x = 1; while ($row = mysql_fetch_array ($result)) { $id = $row['id']; $sexe = $row['sexe']; $name = $row['name']; $prefix = $row['prefix']; $surname = $row['surname']; $mailadres = $row['mailadres']; if ($geslacht == "v"){$geslacht = "mevrouw";} elseif($geslacht == "m"){$geslacht = "heer";} else{$geslacht == "";} // include_once('../../nieuwsbrieven/nieuwsbrief_36.php'); /*does help a bit, but repeats the email when i use 'include' instead !? $body .= '<body> <div '.$css_body.' > <div '.$css_all.' > <div '.$css_header.' > <img src="logo.gif" width="198" height="52" '.$css_logo.' > <div '.$css_kop.' > <span '.$css_kop_span.' >newsletter</span> <br> location, 21 april 2010<br/> </div> <!-- einde div "kop" --> <div '.$css_intro.'> <p> Dear '.$sexe.' '.$name.' '.$prefix.' '.$surname.', </p> <p> This is my newsletter blah, blah, blah </p> <p> with regards,<br/> Mark de Winter </p> </div> <!-- einde div "intro" --> </div> <!-- einde div "header" --> <div '.$css_footer.' > <br> <br> </div> </div> <!-- einde div "all" --> </div> <!-- einde div "body" --> '."\n"; $body .= "</body></html>\n"; $mail->AltBody = "you need HTML compatable to see newsletter!"; // optional, comment out and test $mail->AddAddress($row['mailadres'], $row['voorletters'].' '.$row['tussenvoegsel'].' '.$row['achternaam']); $mail->MsgHTML($body); if(!$mail->Send()) { echo "Mailer Error (" . str_replace("@", "@", $row["mailadres"]) . ') ' . $mail->ErrorInfo . '<br />'; } else { echo "Message sent to :" . $row["surname"] . ' (' . str_replace("@", "@", $row["mailadres"]) . ')<br />'; $x++; } // Clear all addresses and attachments for next loop $mail->ClearAddresses(); $mail->ClearAttachments(); /* warning, last person gets all emails when i use include instead of include once !!! */ } } ?> Link to comment https://forums.phpfreaks.com/topic/199438-phpmailer-class-script-mysql-database-for-personal-newsletters/ Share on other sites More sharing options...
andrewgauger Posted April 23, 2010 Share Posted April 23, 2010 while ($row = mysql_fetch_array ($result)) { $id = $row['id']; $sexe = $row['sexe']; $name = $row['name']; $prefix = $row['prefix']; $surname = $row['surname']; $mailadres = $row['mailadres']; if ($geslacht == "v"){$geslacht = "mevrouw";} elseif($geslacht == "m"){$geslacht = "heer";} else{$geslacht == "";} // include_once('../../nieuwsbrieven/nieuwsbrief_36.php'); /*does help a bit, but repeats the email when i use 'include' instead !? $body .= '<body> <div '.$css_body.' > <div '.$css_all.' > <div '.$css_header.' > <img src="logo.gif" width="198" height="52" '.$css_logo.' > <div '.$css_kop.' > <span '.$css_kop_span.' >newsletter</span> <br> location, 21 april 2010<br/> </div> <!-- einde div "kop" --> <div '.$css_intro.'> <p> Dear '.$sexe.' '.$name.' '.$prefix.' '.$surname.', </p> <p> This is my newsletter blah, blah, blah </p> <p> with regards,<br/> Mark de Winter </p> </div> <!-- einde div "intro" --> </div> <!-- einde div "header" --> <div '.$css_footer.' > <br> <br> </div> </div> <!-- einde div "all" --> </div> <!-- einde div "body" --> '."\n"; $body .= "</body></html>\n"; $mail->AltBody = "you need HTML compatable to see newsletter!"; // optional, comment out and test $mail->AddAddress($row['mailadres'], $row['voorletters'].' '.$row['tussenvoegsel'].' '.$row['achternaam']); $mail->MsgHTML($body); if(!$mail->Send()) { echo "Mailer Error (" . str_replace("@", "&#64;", $row["mailadres"]) . ') ' . $mail->ErrorInfo . '<br />'; } else { echo "Message sent to :" . $row["surname"] . ' (' . str_replace("@", "&#64;", $row["mailadres"]) . ')<br />'; $x++; } // Clear all addresses and attachments for next loop $mail->ClearAddresses(); $mail->ClearAttachments(); /* warning, last person gets all emails when i use include instead of include once !!! */ } So inside of a while loop that parses each email address to append the same content to a $body variable. $body .= will add the following to the body variable. So, if you take the "." out of that line, your body will not get progressively larger. After you fix this, post if there are additional errors. Link to comment https://forums.phpfreaks.com/topic/199438-phpmailer-class-script-mysql-database-for-personal-newsletters/#findComment-1046757 Share on other sites More sharing options...
MarcusAnthonius Posted April 23, 2010 Author Share Posted April 23, 2010 Thanks Andrew. This newsletter has just been send; it worked the way i wanted it to be. I have send a few 1000 emails (by browser) with this script. Probably dangerous, so i installed phpmailerml and will look at it later. or would you (someone else) recommend another method to send with phpmailer? greetings, Mark Link to comment https://forums.phpfreaks.com/topic/199438-phpmailer-class-script-mysql-database-for-personal-newsletters/#findComment-1046778 Share on other sites More sharing options...
andrewgauger Posted April 23, 2010 Share Posted April 23, 2010 Are the emails solicited? (did the recipients want the email?) Link to comment https://forums.phpfreaks.com/topic/199438-phpmailer-class-script-mysql-database-for-personal-newsletters/#findComment-1046785 Share on other sites More sharing options...
MarcusAnthonius Posted April 23, 2010 Author Share Posted April 23, 2010 Yess, the recipients added themselves on a mailinglist. But I haven't made a double opt-in yet, which is of course more adviceable. why? Link to comment https://forums.phpfreaks.com/topic/199438-phpmailer-class-script-mysql-database-for-personal-newsletters/#findComment-1046877 Share on other sites More sharing options...
andrewgauger Posted April 23, 2010 Share Posted April 23, 2010 I would have steered you the wrong direction if you were trying to spam. No, I like Worx's products, I would definitely advise using phpmailerml Link to comment https://forums.phpfreaks.com/topic/199438-phpmailer-class-script-mysql-database-for-personal-newsletters/#findComment-1047151 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.