Script for mail receiving

Started by keiron, Sep 05, 2022, 02:01 AM

Previous topic - Next topic

keironTopic starter

People who are reading, please answer the following question -- is it really possible to write a script to receive mail -- a standard e-mail -- preferably do it in php if possible, specify more specifically what tools you need to use for this (what functions, libraries, what to turn to).
Or, if there is a ready-made script (which can be accessed via a url) with an e-mail registered on it, the login to which is carried out exclusively through the procedure of transferring the login and password to a certain form, is it possible to enter it from a web page generated by another script, forwarding the login and password? If possible, PLEASE specify HOW IT CAN BE DONE. If it depends on a script with a registered e-mail, tell me how you can download that script.
Or If somewhere in the Net there is a ready-made script for these purposes, or someone has a script of some ready-made mailer or something in this spirit, I would be very grateful if you inform me. If anyone knows resources on this subject - a huge request to indicate.


There are a large number of programs that implement webmail using PHP, for instance, on source forge there is a NOCC - webmail client written in PHP that supports IMAP and POP3. This is if you want to host it at home.
There are also a large number of ready-made services that allow you to remotely read your POP3 mail via the web, for example, Mail2Web.

Now, please clarify what is meant by "is it possible to enter it from a page generated by another script"? Do you want to read that web page and display it to your user yourself? If so, then you can use CURL, something like this:
$ch curl_init();
curl_setopt ($chCURLOPT_POST1);
curl_setopt ($chCURLOPT_POSTFIELDS"mailaddress=youremail&password=yourpassword&");
$store curl_exec ($ch);
$content curl_exec ($ch);
curl_close ($ch); 

and after that, you have a read page in $content. Just look at all the field values on mail2web if you will actually use it.


Shell, this is in the sense of bash, sh, ash, etc. There are such tasks - to get into the mailbox, pull out web mail and do something with it. Of course, there is a bundle of software that, if there is a file of the right size, can be adapted for this case, but in fact the task is solved with the help of what is sure to be found in any system - echo, sleep and netcat.
The only disadvantage is that in addition to the actual body of the letter, there are also pieces of protocol messages at the output. But we need it for the script - you can just ignore it, or you can cut out a few extra lines.

Actually, a function for bash. We connect to the specified server via POP3, log in under the specified user and password, try to pick up one email and delete it, disconnect.

(echo "USER $2";
sleep 1;
echo "PASS $3";
sleep 1;
echo "RETR 1";
sleep 5
echo "DELE 1";
sleep 1;
echo "QUIT") | netcat $1 110 > /tmp/mailout.txt

Calling as getmail username password

If desired, iconv / enca can be inserted into the conveyor, so that the letter in the file turns out to be in the desired encoding. One of the disadvantages is a fixed delay in receiving a letter (sleep 5). If we do not fit in this time, we will have to increase it.

That's it, then. But that's not all. This works with most servers, but not all. Some basic servers (we won't point fingers, but it's Google) categorically do not work through simple POP3, only through encrypted. Netcat is of course somewhat lost in this case.
But then the stunnel program will come to the rescue. We put it, and go to edit the settings in /etc/stunnel/stunnel.conf. The default settings should be quite acceptable. We need a client mode (client=yes) and a service for POP3S (at the same time we will do SMTPS at once):

accept =
connect =

accept =
connect =

and run stunnel (something like /etc/init.d/stunnel start). Now when connecting to (POP3 service on the localhost) we will be transferred through an encrypted tunnel to the Google server.