Mail function and its limits

Asked

Viewed 1,386 times

7

I am developing a tool to send bulk emails to a list of 20,000 emails, and the script is this:

    <?php 
            $path = "listas/lista1.txt";
            $ponteiro = fopen ("$path", "r");
            $conteudo = fread ($ponteiro, filesize ($path));
            $linha = explode(" ", $conteudo);
            echo count($linha);
            echo " emails no arquivo.<br><br>";
            for ($f = 0; $f <= count($linha); $f++) {
                echo "Email:  ".$linha[$f]."<br>";
                $to  = $linha[$f];
                // subject
                $subject = 'Email 1!';
                // message
                $message = '<html><body>Teste</body></html>';
                // To send HTML mail, the Content-type header must be set
                $headers  = 'MIME-Version: 1.0' . "\r\n";
                $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
                // Additional headers
                $headers .= 'From: Teste <[email protected]>' . "\r\n";
                // Mail it
                if (mail($to, $subject, $message, $headers))
                {
                echo "MENSAGEM ENVIADA - ";
                }
                else
                {
                echo "NAO ENVIOU - ";
                }
            }
            fclose ($ponteiro);
     ?>
  • I need to know the limit of the mail function and current servers ?

  • How many emails at a time can I send ?

  • What to do to not overload the server ?

  • Advantages of installing SMTP on the localhost and local SMTP limits ?

  • Check your hosting to see how many emails/hours can be sent.

2 answers

7


Well, answering for parts...

I need to know the limit of the mail function and current servers?

The mail function has no limits, per se, but is limited by the maximum memory that can be allocated to the PHP process, by the time of the script ("script timeout"), etc... Besides, it’s usually a bad idea not to use any mail server as an intermediary. Email servers control the waiting lists, retries, bounces, etc... that your application cannot. As for the server, I cannot answer, because it depends on your server...

How many emails at a time can I send ?

In theory as many as you want, in practice see the answer above. Please note that the memory required to send each email depends on factors such as the size of the text and the recipients of the message.

What to do to not overload the server ?

It depends on the purpose and context of the program. One idea is to make a waiting list ("Queue and Defer"), that is, the main script calls a secondary script in charge of sending the emails and does not wait for the answer. So emails are sent in the background. Another idea is to use your own service for this purpose. Mailchimp, for example, has an API that allows you to create and send campaigns.

Advantages of installing SMTP on the localhost and local SMTP limits?

Well, to send emails in "Bulk", you will almost certainly need an SMTP server, for the reasons given above. However, I do not advise using a local SMTP but a dedicated remote such as Gmail or Mailchimp. Follow the advantages/disadvantages

Advantages of the local server:

  • Full control over the server
  • No limitations on the number of emails, uptime, Bandwidth, etc...

Disadvantages of the local server:

  • Most likely it will be marked as SPAM
  • Certain Isps block emails sent from residential Ips
  • You have to install, configure and maintain the mail server

That said, if I were you, I would either use the Mailchimp API or the Gmail API to send the emails. With zend-mail, for example, it is trivial to use Gmail. And there are lots of tutorials on the net explaining the process.

2

I need to know the limit of the mail function and current servers ?

The mail function itself has no limit, but some hosting providers limit the number of messages you can send in the case of shared hosting, i.e. multiple clients on the same server.

How many emails at a time can I send ?

The main limit is the running time. There is usually a 30-second limit, but you can remove the limit with the set_time_limit(0) function. However, some hosting providers are not allowed to use this function.

What to do to not overload the server ?

Depends on how you want to send it. In general the sending of email does not cause overhead in terms of CPU usage because it only requires process or network communication with servers, and as this is only I/O (input and output) operations do not consume CPU.

What consumes the most CPU is the composition of the messages themselves. What happens is that email patterns require you to properly encode messages in MIME format.

This means that for example 8-bit characters (letters with accents and cedillas) should be encoded in the body of the message using quoted-printable encoding, which you are not doing in the above example.

From PHP 5.3 there is integrated in PHP the function quoted_printable_encode. It’s much faster and simpler than trying to encode text with PHP routines as needed in previous versions.

Besides, I don’t recommend that you re-invent the wheel and try to do it manually as you did in your email composition example. There are many classes ready to do this that know how to compose messages according to Internet email standards. You don’t need to learn all patterns to use these classes.

I use the class MIME message which is even optimized for bulk email sending. I have a website that sends around 5 million newsletters a month. Some newsletters go by email to over 400,000 subscribers.

This class has caching capabilities to reuse connections equal composed messages sent to different recipients.

On the iMasters website there is a more detailed article explaining how the bulk PHP email sending using smart optimizations with this class.

Advantages of installing SMTP on the localhost and local SMTP limits ?

In this question you assume that you need an SMTP server to send messages. That’s not true. It’s a common misconception. I’ll try to explain without going into too much technical detail.

SMTP is a protocol for receiving messages. Who receives is the SMTP server. You only need an SMTP server if you need to receive messages.

In PHP to send email on a Linux system, the mail function uses a local program called sendmail that injects the message into the local email server.

The message is queued at the email server and later the email server sends the message communicating with the target SMTP server of the recipient’s email domain.

Therefore PHP itself does not use SMTP on Linux. PHP uses SMTP on Windows because normally Windows does not come with the mail server and so PHP needs to communicate with an SMTP server of its provider that is usually on another machine.

Some websites use external email services such as Amazon or Mailchimp because the hosting provider does not install local email server or offer SMTP server to delegate message delivery.

However these services get very expensive when you exceed the limits of free accounts, which seems to be your case.

In your case, I recommend installing a local email server, postfix or qmail type, not to have to pay high email accounts.

Note that when I say local email server, I am referring to an email server on your hosting machine, not on your personal computer that you have at home or at your company.

If you don’t have an email server installed, you may need a virtual server plan (VPS) or dedicated server to install if your hosting provider does not provide a.

However, you need to be careful to set up your domain’s TXT SPF record so that messages are not considered spam. You also need to be careful if you use an external SMTP server.

If you have questions about this, you can comment below, or create a new question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.