PHPMailer help

Discussion in 'PHP' started by xyar, Feb 14, 2008.

  1. #1
    Hi all,

    I'm desperate to find an answer, if anyone can help. I built a referral page so existing customers of a small motel chain can send coupons to their friends and attract new business. I built it using PHPMailer to email the PDF of the coupon to the recipient. The problem is there appears to (still) be a bug in PHPMailer wherein when I add multiple addresses separated by commas using the $mail->AddAddress function, it won't work, it only works if I use the $mail->AddAddress individually for each email address. Their documentation claims it should work but it doesn't.

    So all I'm trying to do is take a string that might have multiple email addresses separated by commas, and have it spit out individual $mail->AddAddress commands for each one so this will work for more than one address at a time. I know this is simple but I can't figure it out to save my life. I came across some ASP code that will do it but cannot figure out how to convert it to PHP. I posted to the PHPMailer forum on Sourceforge back in December but never got any replies.

    Any ideas?

    Thanks in advance!!
     
    xyar, Feb 14, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Try:

    
    <?php
    
    $emails = "one@one.com, two@two.com, three@three.com";
    
    $emails = explode(',', $emails);
    $emails = array_map('trim', $emails);
    
    foreach ($emails as $email){
        $mail->AddAddress($email);
    }
    ?>
    
    PHP:
     
    jayshah, Feb 14, 2008 IP
    Colbyt likes this.
  3. xyar

    xyar Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You're a lifesaver!!! THANK YOU!! That's exactly what I needed!!!!!
    YAY!!!
    :)
     
    xyar, Feb 14, 2008 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    PhpMailer takes care of the trim()'ing. And you can skip the foreach too:
    
    $emails = "one@one.com, two@two.com, three@three.com";
    
    array_map(array($mail, 'AddAddress'), explode(',', $emails));
    
    PHP:
     
    nico_swd, Feb 14, 2008 IP
  5. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #5
    But my code looks better :D

    Nice one liner though, but to someone reading it, it won't make too much sense. That's the only reason I break my code down :)
     
    jayshah, Feb 16, 2008 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Lol, yes yours is clearly more readable. :D

    I was mainly trying to point out that you can use array_map() with class methods too. (I don't like unnecessary loops. :p)
     
    nico_swd, Feb 16, 2008 IP