📜 ⬆️ ⬇️

Own temporary mail: telegram bot

Often, with new tools and capabilities, there is a desire to experiment and realize something not quite ordinary, which I have never done before. The idea to create your temporary mail service in the form of telegrams bot seemed to me quite interesting.

A little background
Not so long ago, I moved from a regular hosting to a VPS and it so happened that a month later or a little more I had to move to another VPS again. In both cases, I had the cheapest pricing plan and Ubuntu 16.04. Since the last time at that time I came across a terminal at the university, which was tantamount to a complete lack of experience, I used excellent step-by-step instructions from DigitalOcean to configure my VPS (some of them were translated into Russian for those who, like me, are not enough knows english). And yes, my first VPS was at DO, and I had to move again, mainly because some of its IP addresses fell under the distribution of the RKN. Having repeated the LAMP setup procedure a couple of times, I was a little used to the VPS terminal and, as part of its further development, I decided to switch to unusual experiments - to create my own temporary mail service, for example.

I already had experience in the backend, in particular in creating telegram bots in PHP MySQL, but receiving email “by myself” seemed distant and incomprehensible. Having opened several tabs with various articles on the topic, I realized that I did not understand anything. Everywhere it was proposed to use a ton of various tools, which in my opinion was more suitable for a full-fledged mail service than for the task of receiving incoming email messages on a VPS.

Receive incoming mail


For the first step, the sandbox article helped me a lot: habr.com/ru/en/post/260429 . I drew attention to her negative rating, but it described exactly what interested me. I wanted to get a result as soon as possible that can be “felt”, and with the thoughts “I will do it in the future”, I went to set up sendmail.
')
Then I set up the domain. DNS records:

example.com IN MX 5 mail.example.com
mail.example.com IN A XXX.XX.XXX.XXX (ip address of the VPS)

On the server, in the /etc/mail/virtusertable added the line @example.com vasya , thereby determining that all mail destined for any addresses on ****@example.com is addressed to user Vasya.

To process incoming mail with a php script, I added the line vasya: "|php -q /home/vasya/mail.php" /etc/aliases file vasya: "|php -q /home/vasya/mail.php" .

After conducting several tests and making sure that incoming mail is sent to the php script, I could do it by processing.

Receiving a raw incoming mail sent in php using the method described above is implemented in the code very simply:

 $msg = file_get_contents("php://stdin"); 

It is quite another thing to parse the mail format and present the data in a clear and accessible way. Google offered me several options on how to parse the mail format using PHP. All the libraries I found were installing additional components, but one of them seemed to me less cumbersome: github.com/zbateson/mail-mime-parser . The only thing I needed to install additionally is the popular PHP package manager, Composer. Of course, I didn’t come across it on a regular hosting, but its installation and further connection of the library for parsing mail was not any difficult.

The beginning of the php script for processing incoming mail using the zbateson / mail-mime-parser library looks like this:

 <?php require("vendor/autoload.php"); use ZBateson\MailMimeParser\MailMimeParser; use ZBateson\MailMimeParser\Message; $msg = file_get_contents("php://stdin"); $parser = new MailMimeParser(); $message = Message::from($msg); 

Since the temporary mail in my opinion does not involve several recipients, it is enough to take only the first of the possible:

 $to = $message->getHeader('To'); $email = $to->getAddresses()[0]->getEmail(); 

In the variable $ email we have the address of the recipient of the form vasyaorpetya@example.com.

For receiving the content of incoming letters in the library there are corresponding methods:

 $from = $message->getHeader('From')->getEmail(); $subject = $message->getHeaderValue('Subject'); $msg_text = $message->getTextContent(); $msg_html = $message->getHtmlContent(); 

Telegram bot


What should be able to telegram bot temporary mail in the first place?

  1. Issue a new temporary email address upon request
  2. Send in a chat incoming email for this email, while the mailing address is valid
  3. Renew email addresses

Quite suitable in this and many other cases, the method of receiving updates from the Telegram is the use of Webhook. All you need is the address of the script with https. Using Certbot to configure an ssl domain certificate is described in detail in the DO instructions.

To interact with the Telegram Bot API, I use my own work. Someone prefers to use popular libraries. Sending messages with buttons in telegrams has long become a familiar affair, as many articles have been written about.

The generation of temporary email addresses is essentially the issue of the next address in order. I created a table for email addresses in the database, where the int type id with auto increment uniquely identifies the recipient. The transformation of a numeric id into a string address is carried out as a translation of a number into another number system, where the entire Latin alphabet is available as a “number”. 26 letters in comparison with numbers give a good reduction in the length of the identifier. Probably, I could also use large letters, numbers and some characters without problems to further reduce the length of the addresses given, but I left only small Latin letters.

Functions for converting a numeric id to a string and back:

 // $alphabet = explode(",", "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"); //   @grayfolk: $alphabet = range('a', 'z'); function num2str($n, $a) { // $a -  $b = count($a); $r = 0; $x = ""; while ($n) { $r = $n%$b; $n = ($n-$r)/$b; $x .= $a[$r]; } return strrev($x); } function str2num($s, $a) { $n = 0; $b = count($a); $s = strrev($s); for ($i = 0; $i < strlen($s); $i++) { $n += array_search($s[$i], $a) * pow($b, $i); } return $n; } 

One of the key benefits of using a temporary email service is the lack of spam. But if the addresses are in order, you can make a list of the nearest addresses that will be issued and successfully do the mailing. To solve this problem, I added some random string to the recipient ID. To distinguish between id and random component in the address, I decided to always start the random component with a digit.

We write a random string of the e-mail address to the database along with the recipient's id, the user id in the telegraph and the time of the mailbox issuance.

It would seem, you can not even store incoming mail - sent to telegrams and that's it. But what about html letters? They can not be displayed in the message in the chat. It remains to record the incoming html messages in the database and show them on the site, and the user to send a link that includes the message id and the next generated password. To clean the database with crown on schedule, a php script is launched that deletes incoming html messages that were received more than an hour ago.

Later in the bot telegram, I added buttons that prolong the mailbox for 10 or 60 minutes, as well as a button that lets you know how much, nevertheless, it remains for him to live before receiving incoming messages is stopped.

Since we are dealing with registered users in the telegraph, you can provide an opportunity to activate your old mailboxes, for example, in order to recover a forgotten password on a website or for any other operations that require confirmation using email. A given mailbox "accepts" incoming messages only when it is necessary for the user; all the rest of the time, possible spam is ignored.



Wish for the future:


Links


Telegram bot: @tmpmailbot

An article describing the sendmail configuration

PHP library for parsing email

Source: https://habr.com/ru/post/445988/


All Articles