I’m researching imap and smtp and found interesting things …
Messages are received and sent using an endpoint / socket TCP / IP over SSL or TLS, interestingly php is able to create a socket and receive and send data through it, well, I could modify the domain’s dns and point to the endpoint . The main IMAP commands are LOGIN and SELECT, of course, there are others, but these are more interesting, this tutorial shows how an IMAP server works:
https://www.toptal.com/php/building-an-imap-email-client-with-php
based on this it is possible to imitate the server and easily replace it, a database and some authentication techniques are essential, it is not difficult to find "login" in a header, it is also possible to search for words in texts, therefore the need for a command line terminal is no longer essential, I just need to know what the customer does, validate and who knows how to deliver.
There are probably theses contrary to mine, suggestions on how to use a Devecot or not reinvent the wheel, I already have a server, but it is useless, I need something based on the web for greater integration, I could use one of these APIS from cpanel, but no , is not what I want.
What I’m trying to understand here is how this transaction is made, see a shipping code:
// The message
$message = "Line 1rnLine 2rnLine 3";
// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "rn");
// Send
mail('[email protected]', 'My Subject', $message);
It is important to note that just because the email is accepted for
delivery, it does not mean that the email will reach the expected
destination. https://www.php.net/manual/en/function.mail.php
It doesn’t make sense, but ok, let’s consider that he went through dozens of servers and one of them was in a bad mood, by the way, which is nothing new, so he decided to disappear with the email and that’s it. The most confusing thing here is that emails go through one or more transfer agents and these agents can appeal the message, they even read to see if it is in compliance, privacy has been eliminated in this process … so in practice there is a supervisor who receives and if it is in accordance with your criteria, search and delivery to the recipient, plus the documentation talks a lot about "commands", "injection of commands", after all
1 – How do these emails reach the recipient server?
2 – Everything here is a command or does it have text too?
3 – How can I capture these requests? For example: Server A sends to Server B (‘Agent’) a message addressed to Server C, so Server C needs to process it and reply that it accepts the message, at the same time the documentation insists on "commands" talking about injection command, which means that Server B will inject commands into server C, so from now on it’s no longer http, now it should be something like ssh or maybe telnet (hopefully not), where did the http header end up?
$headers = array(
'From' => '[email protected]',
'Reply-To' => '[email protected]',
'X-Mailer' => 'PHP/' . phpversion()
);
2
Answers
I found a less painful way to do this, on github there are 6 packages that intend to act as an email receiver, one of them follows the same logic as mine, creates a socket and tries to understand what the customer is trying to do, validate and deliver if applicable, making a filter based on a blacklist, here is using a very simple json configuration file, this is very good for those working with a javascript framework, in my case in particular it will not be useful, I will have to cut it it in pieces to make it the way I’m idealizing, but it will help those who need it in the future, it’s very small, it has 3 php files and 1 json file, it also has another more documented json file.
It specifies that you need to download the project, provide permission 777 and run this line in the terminal:
screen php run.php
The reason is very simple, you need to start the socket before making the request, once this is done, it will be available at http://localhost port 25
Although there are controversies, you can specify the port you want, it is now standard to use 585 to send and port 25 to receive, minimally modern email clients should provide the option of advanced configuration, but if this is not possible, there are hundreds of php libraries to access the mailbox via imap and pop3, in addition many of the email clients use port discovery, one of which is gmail.
run.php
Correction: port 25 was closed in some countries due to the number of spam, the most used ports in SMTP are 585, 587 and 465
It looks like you are missing a basic understanding of how mail delivery works.
The php mail() function invokes a sendmail program, usually configured by the system administrator, that allows you to send emails. The sendmail program is responsible to do the delivery in this case. There are several libs out there that doesn’t rely on sendmail and sends emails directly using sockets. The link you provided is one of such libs. Note that those libs are clients, not servers. They don’t receive mail, only send.
To be able to receive emails using pure php you would need to create a socket that listens in the default email ports. This server must be able to understand and reply to the commands described in the SMTP protocol.
HTTP headers have nothing to do with email delivery. The commands you mentioned are keywords following a set of rules and procedures described in a protocol for transmitting data.