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

Asked

Viewed 1,350 times

0

Good afternoon to all,

I’m not a professional developer, I know that in php there are ways to send email through phpmailer(), I wonder 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-servermail itself?

from now on I thank you all.

Sincerely yours truly, Guilherme Vargas

  • Or you use the API of these utilities to access the email. Or you use the imap of php http://php.net/manual/en/book.imap.php

2 answers

3

Yes, you can write a program to "receive an email". And no, you don’t want to do it that way, I explain a little bit why. And definitely not its application can’t receive an email from an existing domain, and owned by another entity. The domains you listed are not "public" - you may have confused them with "public knowledge" - but google, owner of gmail.com, microsoft owner of outlool.com and yahoo, owner of yahoo.com are private companies, owners of these respective domains, in the same way that you can register a domain ". with";

Good - detailing a little 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 implement the SMTP server protocol on top of it. The specification is here: https://tools.ietf.org/html/rfc5321

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

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

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

what should suit you

It’s actually implementing an IMAP client - you set up the email account on the private server of your choice, among those you listed, and take the data for an "IMAP" connection to it - in this case, who has to be "listening to the internet all the time" to see if an e arrivesmail, remains the third party server - you write a program that works with an IMAP client, which does not need to have a server behavior - at the desired moment it connects to the server by IMAP protocol, and checks if any messages arrived, query its headers, download, delete, archive, how to suit existing messages. IMAP is a public protocol (yes, public) to do with emails the things that today each company does with its private API.

The Python language at least has in the standard library an IMAP client implementation - just do import imaplib and consult the documentation: https://docs.python.org/3/library/imaplib.html - 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 the messages are written in general containing attachments, with text formatted as HTML, etc... Then your code will still have to interpret the message headers correctly - and if they have accents in Subject for example, it’s an extra loop, with the RFC 5335, and then extract the attachments, which can be html text, or simply plain text, but accentuated with utf-8 characters - for this you have to implement the decoding of what is described in RFC 2045

  • Wow. It’s been a while since I accessed SO.pt, but it’s been a day and a laborious response like yours, not even a single PU received.

  • It’s part of the deal! :-)

1

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

Grab folders ("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 criteria, this is only partially documented in php.net, as it is part of the IMAP protocol so it is more likely to find all "commands" of sorts filter in:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.