PHP: How is sending email via SMTP without the mail function?

I would like to know what the implementation of sending email with SMTP is like.

  • How is this communion done if the function mail is not used?
  • How is server communication in this case?
  • What PHP functions can be used to implement this?
  • is communication done via socket? How does it work?

PS: the question is not about using the PHPMailer library (or other email sending libraries), but rather about the way the submission is done and all the communication that involves it within PHP independent of libraries. The doubt is more conceptual and is about the reasons for the choices of certain implementations.

Author: raphael, 2019-06-17

2 answers

As an introduction, what I already said in this answer https://pt.stackoverflow.com/a/215080/3635 , are "under the TCP protocol" , internally the function itself mail() connects to the standard SMTP server of the hosting (this can be adjusted in php.ini, but the focus of the question is not this).

So understanding that almost everything is written based on TCP, the SMTP and IMAP protocol are no exceptions, so let's first separate the protocols from e-mail:

  • SMTP is a protocol for sending email messages
  • POP3 is a protocol for receiving/reading email messages
  • IMAP is also a receive/read protocol (of course both are different)

So in php, like many languages, you can make a connection to a server + port, regardless of the service that the port delivered, you can use these functions:

  • fsockopen()
  • fsockopen()
  • pfsockopen()
  • stream_socket_client()

I'm not going to detail one by one because it's going to get away from the subject. Basically PHPMailer and other similar libraries basically do this:

$handler = fsockopen('smtp.servidor.com', 25);

Then when connected, at this point you start a while that is checking the "state", like this:

while (feof($handler) === false) {
     ... ações ...
}

So inside while you write and read the answers, the basic command when connecting on an SMTP is HELO or EHLO (the command EHLO returns more details, which are sometimes required depending on the server), this command is basically to take a simple answer and know if this is in fact an SMTP server, example:

$dominio = 'smtp.servidor.com';
$handler = fsockopen($dominio, 25);

while (feof($handler) === false) {
     fwrite($handler, "HELO $dominio"); //Envia o seu comando

     $resposta = trim(fgets($handler)); //Pega a resposta

     if (strpos($resposta, '250') === 0) {
          //Esta no servidor SMTP
     } else {
         //NÃO esta no servidor SMTP
         break; //quebra o ciclo
     }
}

flose($handler);

Then being all ok, you will have several commands, to connect in an email account you can use commands like:

  • AUTH PLAIN
  • AUTH LOGIN
  • AUTH CRAM-MD5

Not all server supports / allows all commands, each server is of uses a form of authentication, usually the command EHLO gives this detail on the return of the response, informing the supported/allowed.

All this you can do even using telnet, which is a command line tool (can be installed on Windows as well):

C:\Users\guilherme> telnet
Microsoft Telnet> OPEN smtp.servidor.com 25
250 smtp.servidor.com
HELO smtp.servidor.com
250-smtp.servidor.com

In the example above the OPEN and the HELO I typed in cmd, the messages with number with prefix are responses from the smtp server.

This is because that's what I said, it's all via protocols, even HTTP goes through something similar and is also TCP.

Some time ago I created my own email sending script, it has many commands that I don't remember, but once I find the script I will detail the answer with a basic email sending example, otherwise I will read calmly about each command and elaborate an example soon.

 0
Author: Guilherme Nascimento, 2019-10-16 04:31:10

Exactly as it is done, only you downloading the library and reading source code. In the documentations will only tell even how to do to work and the functionalities. The mail function is a native PHP function, it was created directly from the PHP source code made in C. If PHPMailer does not use mail it is because they implemented the mail function itself possibly because of some functionality that mail did not provide. You just have to understand how SMTP type email sending works (architecture, requests, etc.) and redo the logic using any programming language.

 -7
Author: Bruno Leyne, 2019-06-17 18:30:27