SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict TegSkywalker's Avatar
    Join Date
    Nov 2001
    Location
    USA
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multiple emails with PHPMailer but individual TO: in email

    I'm using PHPMailer to send out multiple emails but if I use AddAddress, it will clump all of the emails into multiple TO: values, and if I use AddBCC the email addresses are hidden but there is no personalization.

    So in a nutshell I want to send out multiple emails from emails in a database and each email to have a personal TO: field including their email address and name. Is this even possible or would I have to do multiple mail() calls? I substituted my own domain with domain.com but otherwise the same as what I have.

    Here is a sample of the script:

    PHP Code:
    // Get campaign email body
    $csubject $_REQUEST['csubject'];
    $cbody $_REQUEST['cbody'];
    $campid $_REQUEST['campaign'];

    // Query the database for email addresses
    $result mysql_query("SELECT * FROM ecampaigns_test WHERE campaign=$campid");

    // Include PHPMailer script
    include('inc/phpmailer/class.phpmailer.php');

    // Start PHPMailer code
    $mail = new PHPMailer(); // defaults to using php "mail()"
    $mail->SetFrom('info@domain.com''Domain.com');
    $mail->Subject "$csubject";

    // Start Loop of sending emails
    while ($row mysql_fetch_array ($result)) {
        
    $mail->AltBody "To view the message, please use an HTML compatible email viewer"// optional, comment out and test
        
    $mail->MsgHTML("Dear ".$row["fname"].",<br>".$cbody);
        
    $mail->AddAddress($row["email"], $row["fname"]);
    }

    if(!
    $mail->Send()) {
      echo 
    "Mailer Error: " $mail->ErrorInfo;
    } else {
      echo 
    "<span style=\"font-weight:bold; font-size:110%\">Email campaign has been sent.</span><br><br><a href=\"/admin\">Return to Admin</a>";


  2. #2
    SitePoint Evangelist AlienDev's Avatar
    Join Date
    Feb 2007
    Location
    UK
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code php:
    $mail = new PHPMailer();
    $mail->SetFrom('info@domain.com', 'Domain.com');
    $mail->Subject = "$csubject";
    $mail->AltBody = "To view the message, please use an HTML compatible email viewer"; // optional, comment out and test
     
    while ($row = mysql_fetch_array ($result)) {
        $mail2 = clone $mail;
        $mail2->MsgHTML("Dear ".$row["fname"].",<br>".$cbody);
        $mail2->AddAddress($row["email"], $row["fname"]);
        $mail2->send();
    }
    Me on StackOverflow | Blog & personal website.

    I mostly use: PHP, Java, JavaScript, Android.

  3. #3
    SitePoint Addict TegSkywalker's Avatar
    Join Date
    Nov 2001
    Location
    USA
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your help, it works great.

  4. #4
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what is clone doing there?
    it seems that it creates a copy of the $mail object which is $mail2.
    Why cant we use the $mail object in the loop and keep changing couple of its properties during each iteration?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •