Jump to content

Phpmailer class +script +mysql database for personal newsletters


MarcusAnthonius

Recommended Posts

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("@", "&#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 !!! */ 
    } 
} 
?> 

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("@", "&#38;#64;", $row["mailadres"]) . ') ' . $mail->ErrorInfo . '<br />'; 
      } else { 
          
          echo "Message sent to :" . $row["surname"] . ' (' . str_replace("@", "&#38;#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.

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

 

 

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.