Is there any way to receive mail through php or some other programming language?

0

Good afternoon everyone,

I am not a professional developer, I know that in php there are ways to send email through phpmailer (), I would like to know is if there is any way to receive an email through public domain like gmail, outlook, yahoo etc ..., or if it is only on an e-mail server itself?

Thank you very much.

Sincerely, Guilherme Vargas

    
asked by anonymous 18.08.2018 / 17:37

2 answers

1

Yes, you can get the emails through the native API (since you mentioned phpmailer, I believe you are working more with php):

Get INBOX (s) ) with PHP:

$config = '{servidor.com:143}';

$mbox = imap_open($config, "username", "password");

echo "<h1>Pastas:</h1>";

$folders = imap_listmailbox($mbox, $config, "*");

if ($folders) {
    foreach ($folders as $folder) {
        echo $folder . '<br>';
    }
} else {
    echo "Falha ao listar pastas<br>";
}

echo "<h1>Lista mensagens na INBOX</h1>";

$headers = imap_headers($mbox);

if ($headers) {
    foreach ($headers as $header) {
        echo $header . '<br>';
    }
} else {
    echo "Falha ao listar<br>";
}

imap_close($mbox);

Functions you should use to read, download attachments, move messages, filter messages:

  

Note: "filter" is called critéria, this is only partially documented in php.net as it is part of the IMAP protocol so it is more likely to find all "filter" in:

     
    
18.08.2018 / 18:02
1

Yes, you can write a program to "receive an email". And no, you will not want to do it that way, I'll explain a little bit why. And No, definitely your application can not receive an email from an existing domain, and is owned by another entity. The domains that you have listed are not "public" - you may have been confused with "public knowledge" - but google, owner of gmail.com, microsoft owner of outlool.com and yahoo, owner of yahoo.com are private companies, donas domains, just as you can register a ".com" domain;

Good - detailing a bit more, the "receiving" of emails itself is done by the SMTP protocol. Just as a Web server can respond to HTTP requests, you can write a program that handles pure TCP / IP sockets and implements the SMTP server protocol on top of it. The specification is here: link

Of course, there your program will have to be on a server on the internet, responding to a domain of its own, as a web server, and emails ending in "@yourdomain.com" will be forwarded to your server. If you upload a server in an experimental network, you can even forward e-mail using the ip address of the machine where the server is located - [email protected] - for example - the part after the "@" is an internet host and works from same as the name after the "http: //" for web.

The program that sends e-mails then has implemented the client side of the SMTP protocol, just as browsers have the client side of the HTTP protocol - and it makes a connection to its server and sends the emails, which must be in very specific formats, described in other RFC documents (especially if they have attachments, remembering that rich text email is also an attachment).

Now - as I said above, this is not what you want - it would even be legal to implement a simple SMTP server, but the rules in the last 30 years of internet for a "respected" email server have become more complicated much to reduce the spam problem, and to add layers of cryptographic protection.

what should work for you

It actually implements an IMAP client - you set up the email account on your private server of your choice, and it picks up the data for an "IMAP" connection to it - in that case, who has to "listening to the internet all the time" to see if an email arrives, it's still the third-party server - you write a program that works like an IMAP client, which does not have server behavior - connects to the server via the IMAP protocol, and verifies that a message has arrived, queries its headers, downloads, deletes, archives, converts existing messages. IMAP is a public prototype (yes, public) to do with emails the things that every company does with its private API.

  • the IMAP specification is here: link

The Python language at least has an IMAP client implementation in the default library - just do import imaplib and consult the documentation: link - other languages may have equivalent libraries, embedded or written by third parties.

Remember that while the IMAP library will give you facilities to connect to the server and download the email messages there, manipulating the email messages can still be tricky - as I mentioned above, nowadays messages are written in general containing attachments, with text formatted as HTML, and so on ... Then your code will still have to account for interpreting the message headers correctly - and if they have accents in the subject for example, it's a go back, with RFC 5335 , and then extract the attachments, which can be html text, or simply pure text , but accented with utf-8 characters - for this you have to implement the decoding of what is described in RFC 2045

    
18.08.2018 / 18:08