Mail function and its limits

7

I'm 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 what is the limit of 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 localhost and local SMTP boundaries?

asked by anonymous 02.05.2014 / 01:26

2 answers

7

Well, responding in parts ...

  

I need to know what is the limit of 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 script timeout , etc ... Also, it is often a bad idea not to use any mail server as an intermediary. Email servers control waiting lists, retries, bounces, etc ... that your application can not. As for the server, I can not respond 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. You need to keep in mind 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?

Depends on the purpose and context of the program. One idea is to make a queue and defer, that is, the main script calls a secondary script in charge of sending the emails and does not wait for the response. So emails are sent in the background. Another idea is to use a service for this purpose. Mailchimp, for example, has an API that lets you create and send campaigns .

  

Advantages of installing SMTP on localhost and local SMTP boundaries?

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. Following 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:

  • It will most likely be marked as SPAM
  • Certain ISPs block emails sent from home IPs
  • You must install, configure and maintain the mail server

All said, in its place, or use the Mailchimp API or the Gmail API to send the emails. With zend-mail , for example, it's trivial to use Gmail . And there are lots of tutorials on the net explaining the process.

    
02.05.2014 / 04:04
2
  

I need to know what is the limit of 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 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 runtime. There is usually a limit of 30 seconds, but you can eliminate 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. In general email sending does not cause overhead in terms of CPU usage because it only requires process or network communication with servers, and since this is only I / O operations do not consume CPU.

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

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

As of PHP 5.3, the quoted_printable_encode function is integrated into PHP. It is much faster and simpler than trying to encode text with PHP routines as needed in previous versions.

Otherwise I do not recommend reinventing the wheel and try to do it manually as you did in your example composing e-mail messages. There are many ready-to-do classes that know how to compose messages according to Internet e-mail patterns. You do not need to learn all of the standards to use these classes.

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

This class has caching capabilities to reuse connections to identical composite messages sent to different recipients.

On the iMasters website you have a more detailed article explaining how Sending bulk email in PHP using smart optimizations with this class.

  

Advantages of installing SMTP on localhost and local SMTP boundaries?

In this question you assume that you need an SMTP server to send messages. This is not true. It is a common misconception. I'll try to explain without going into too many technical details.

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

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

The message is queued on the mail server and later the mail server sends the message communicating with the destination SMTP server of the recipient's email domain.

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

Some sites use external e-mail services such as Amazon or MailChimp because the hosting provider does not install a local mail server or offer an SMTP server to delegate message delivery.

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

In your case, I recommend installing a local mail server, such as postfix or qmail, to avoid paying high email accounts.

Note that when I say local mail 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 do not have an email server installed, you may need a virtual server (VPS) plan or dedicated server to install if your hosting provider does not provide one.

However you need to be careful to configure your domain's SPF TXT record so 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.

    
03.05.2014 / 23:01