Mail with loop or single function?

Discussion in 'PHP' started by Tony Brar, Nov 10, 2012.

  1. #1
    Hi guys,

    Is it faster to use a while loop to send email:
    ($addresses is an array with email addresses in it)
    foreach($addresses as $to)
    {
    mail($to,'test','this is a test msg','From:someone@example.com');
    }
    Or to run mail() once, with $to being a list of addresses written like ?

    Thanks,
    -Tony
     
    Solved! View solution.
    Tony Brar, Nov 10, 2012 IP
  2. #2
    IMO using mail() once and CCing all the emails would be faster.
     
    arsalankhan, Nov 10, 2012 IP
  3. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #3
    Okay, I'll take your word for it...
    But why?
    I'd just like to know for future reference.

    Thanks,
    -Tony
     
    Tony Brar, Nov 10, 2012 IP
  4. arsalankhan

    arsalankhan Peon

    Messages:
    66
    Likes Received:
    0
    Best Answers:
    6
    Trophy Points:
    0
    #4
    Just because CCing all the emails will make one request to the outside network and using mail() will make a request for each email address, but if you have a large number of emails then I wouldn't recommend CCing them.
     
    arsalankhan, Nov 10, 2012 IP
  5. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #5
    What do you mean by a large number?
    And if I did have a large number, what would you recommend?

    Thanks,
    -Tony
     
    Tony Brar, Nov 10, 2012 IP
  6. arsalankhan

    arsalankhan Peon

    Messages:
    66
    Likes Received:
    0
    Best Answers:
    6
    Trophy Points:
    0
    #6
    For me anything over 100 is large for a single email, and if you are sending over 1500 mails in one day then I would recommend you to check mail sending services like sendgrid.com and use their API to send emails.
     
    arsalankhan, Nov 10, 2012 IP
  7. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #7
    Okay. Because my site is not popular now, and is down as well, I will just use my server's sending capabilities.

    Thanks.
     
    Tony Brar, Nov 10, 2012 IP
  8. arsalankhan

    arsalankhan Peon

    Messages:
    66
    Likes Received:
    0
    Best Answers:
    6
    Trophy Points:
    0
    #8
    Yep as long as you are below 1500-2500 emails/day it is better to use your server's sending capabilities :).
     
    arsalankhan, Nov 10, 2012 IP
  9. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #9
    Will do.

    Thanks,
    -Tony
     
    Tony Brar, Nov 11, 2012 IP
  10. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #10
    An addition of what Arsalan said, it is faster to use bulk CC (I would use BCC instead so users do not see the other user emails) if you are using php built in mail(). If you are using PEAR Mail (or any other famous email systems that share SMTP connections), then this is not an issue and then it is favored to send it individually if you want to know the results of the email (was it actually sent to the recipient or if it failed).

    Using PHP built in mail() makes an SMTP request each time it is called, which can slow things down. PEAR mail on the other hand is faster:

    http://pear.php.net/package/Mail

    You can also use other libraries.

    In regards to the limit of BCC/CC you can send, it does not matter. Whichever email system you use will send the message to each account independently. It all depends on your host's maxrecipients email config. If it is more, it will fail.
     
    ThePHPMaster, Nov 11, 2012 IP
  11. arsalankhan

    arsalankhan Peon

    Messages:
    66
    Likes Received:
    0
    Best Answers:
    6
    Trophy Points:
    0
    #11
    Master, I beg to differ with you on one point!!!!!!!!!! Even though there might not be any limit from your host or PHP, but if you are frequently sending emails with a large number of addresses as BCC or CC then it is very likely that you will be marked as spam by most of the email service providers, so to be on the safe side I would recommend to limit the number of CCs and BCCs.
     
    arsalankhan, Nov 12, 2012 IP
  12. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #12
    NO CC PLEASE! BCC is 100% better!

    And for sending emails, just use the mail function in PHP and send each email seperate! this way you 100% know that others don't see email addresses from other users.
     
    EricBruggema, Nov 12, 2012 IP
  13. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #13
    Hey Arsalan,

    Emails send via mail will not be marked as spam no matter how many you send (I always used it on high traffic sites with no issues), unless you incorrectly set the headers or do not have your reverse DNS setup properly. You can also make sure the user you are sending the mail() from is part of Sendmail allowed users.
     
    ThePHPMaster, Nov 12, 2012 IP