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.
– raphael
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.
– Guilherme Nascimento