Is there a limit when using php mail function?

21,253

Solution 1

Please be aware of this note from the mail documentation:

Note: It is worth noting that the mail() function is not suitable for larger volumes of email in a loop. This function opens and closes an SMTP socket for each email, which is not very efficient.
For the sending of large amounts of email, see the » PEAR::Mail, and » PEAR::Mail_Queue packages.

Solution 2

No limit to the emails number, but there is the time limit of the PHP script. See the max_execution_time set in your php.ini, typically it's 20 or 30 seconds. If you don't know that, use phpinfo() to find it out.

Moreover, you should take some steps to prevent users from getting too much emails. You should mark them as sent, so they don't receive double posts if you accidentally start the script twice.

Other than that, you should note that php's mail function is inherently not optimized at all. You could try some libraries, like phpmimemessage or any other, which will allow you do to some caching, for example, among many other features.

Solution 3

You should build a queue of emails sent/failed, so you can try to resend failed attempts and avoid re-sending emails if something should go wrong.

Do not create a loop that tries to send 10k emails via mail()

Also, the most likely limit you'll hit will be that of the mail server of your ISP or host.

Share:
21,253
bbtang
Author by

bbtang

Updated on July 09, 2022

Comments

  • bbtang
    bbtang almost 2 years

    I am using php and mysql.

    I am going to send 10k++ (ten thousands plus) emails to update my subscribers, and this is the first time I am going to send them. I will use php mail function, basically here is what I will do:

    First get the data from database:

    Select name, email FROM data
    

    After that, using while loop to send the data:

    while($r = mysql_fetch_assoc($exe)){
        ...
        if($mail){
            echo "success<br>";
        } else {
            echo "failed<br>";
        }
    }
    echo "Sent all";
    

    I include the if.. else statement, to ensure that each email is sent successfully. Is there anything I need to take care of? Will I have any problems when SENDING TO 10K++ users?

    Is there a limit of numbers of emails that you are going to sent?

  • bbtang
    bbtang over 14 years
    Omg, I didnt know that. So, you normally use "phpmimemessage" to send out 10k++ emails? Which library you are using?
  • bbtang
    bbtang over 14 years
    What library do you use for sending blast email? Pear:: mail?? I am going to study that stuff. Thanks
  • Peter Parker
    Peter Parker over 14 years
    use pear::Mail_Queue, not mail
  • Palantir
    Palantir over 14 years
    I have done something much more complex, but yes, I was using this library to do mass mailing (newsletters). Mimemessage is quite old now, there are better libraries now, see other answers, or google for them. My script was running in the background from CLI however, without time limitations.
  • Enrique
    Enrique over 4 years
    what about PHPmailer with gmail? could it work fine or has the same problem? is the problem the time_execution limit or something else?