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

Asked

Viewed 273 times

5

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

  • How is this communication made if the function mail is not used?
  • How is the communication with server in this case?
  • What PHP functions can be used to implement this?
  • Is communication done via socket? How it works?

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

2 answers

0


As an introduction, what I have already said in this reply /a/215080/3635, are "under the TCP protocol", internally the function itself mail() connects to the default hosting SMTP server (this can be set in the php.ini, but the focus of the question is not this).

So understanding that almost everything is written on the basis of TCP, the SMTP and IMAP protocol are no exceptions, so let’s separate the email protocols first:

  • SMTP is an email messaging protocol
  • POP3 is a protocol for receiving/reading email messages
  • IMAP is also a receiving/reading 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 won’t go into detail one by one because it will get away from it. Basically Phpmailer and other similar libraries do basically this:

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

So when you’re connected, right now you start a while that keeps checking the "state", like this:

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

Then inside the while you write and read the answers, the basic command when you connect to an SMTP is HELO or EHLO (the command EHLO returns more details, which are sometimes needed depending on the server), this command is basically to pick up a simple answer and know if this is actually 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 everything being ok, you will have several commands, to connect to an email account you can use commands like:

  • AUTH PLAIN
  • AUTH LOGIN
  • AUTH CRAM-MD5

Not all server supports/allows all commands, each server uses a form of authentication, usually the command EHLO gives this detail in the return of the reply, 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, messages with prefix number are replies from SMTP server.

That’s because it’s what I said, it’s all via protocols, even HTTP goes through something similar and it’s also TCP.

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

  • Some of the more specific questions I asked were marked as duplicates, however there are still others open, if you can help me with that, I appreciate.

  • Dear @rcs really I have very tight time, I answered yours because it is a subject that interests me very much. Protocols, history of certain technologies and their nuclei. But I promise to look calmly at all your questions.

-7

Just like it’s done, just you downloading the library and reading source code. In the documentations they will only really tell you how to make it work and the functionalities. The mail function is a native function of PHP, it was created directly from the PHP source code made in C. If Phpmailer does not use mail it is because they have implemented the mail function itself possibly because of some functionality that mail did not provide. All you need to do is understand how smtp email sending works (architecture, requests, etc) and rework the logic using any programming language.

  • 5

    I voted against because the question is basically "how it works" and the answer is basically "just research how it works". In my view it did not add much.

  • 1

    If Phpmailer does not use mail it is because they have implemented the mail function itself possibly because of some functionality that mail did not provide. At first, what functionality is this that has been implemented? How does it work? That is the purpose of the question.

Browser other questions tagged

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