IMAP interface access to existing user messaging system in Python - python

I am running a site where users can private message each other. As with any other such website, to read and mark their messages, users must log on to the site.
I wish to expose an IMAP interface so that users may read their site messages using their standard email client. There would be few complications in such approach as what be userid to email-address mapping and what would happen if the user replies a mail but for the time being I'm little concerned about these issues.
Is there any lightweight raw IMAP server in Python to which I could just add few rules or logic to expose an IMAP interface to user's messages?

Twisted Mail project:
Twisted Mail contains high-level, efficient protocol implementations for both clients and servers of SMTP, POP3, and IMAP4. Additionally, it contains an "out of the box" combination SMTP/POP3 virtual-hosting mail server. Also included is a read/write Maildir implementation and a basic Mail Exchange calculator (depends on Twisted Names).
The examples for IMAP4 contain only a client. Look into the source for more information.

Related

determine smtp server for a given email address python smtplib

Given an email address is it possible to determine what the smtp address is using python. I am building an email application using python smtplib.
Thanks
The Python smtplib module does not perform mail routing, so it leaves that to a smart host.
You could use dnspython to look up MX records for the destination domain (and A and AAAA records if there is no MX record). The resulting address you can then pass to smtplib.
However, this is only a small fraction of the work that is needed to implement proper mail routing: If the target mail server is unreachable or times out, you are supposed to try the next server. Similarly if it responds with a 4xx error code. If all servers are unavailable, you need to temporarily store the message locally, so you need to implement your own mail queue. Furthermore, your external IP address may be blacklisted for mail delivery.
Therefore, it is usually easier to install a local mail server such as Exim or Postfix and use that to inject mail, possibly routing it via a smart host with good email reputation to avoid blacklisting.

What's an email server I could route a simple, non-encrypted SMTP request through manually?

I have a pretty basic networking assignment where I have to make a manual SMTP request (with the HELO, MAIL FROM:, and the like) as the client to the SMTP server in order to send an email.
It doesn't seem like you can do this anymore with Gmail, as it requires an encrypted SMTP request for obvious reasons.
I know I can do this with OpenSSL, but this is to be build with Python and it seems like an external library would have to be used for this, which isn't allowed on the assignment.
So if possible I'd like to be able to submit this unencrypted for simplicity's sake. Are there any test servers to send non-encrypted SMTP over now a days?
Inbox.py is here to save you (made by the author of python-requests: Kenneth Reitz), it's a very simple SMTP server "made for Humans".
Useful for your test purpose, I think.

Send AND receive email in an python application (no IMAP or POP3)

I'm building a application that needs to send and receive emails.
However I do not want to have a separate email server (or use IMAP and POP3), since I need to create/delete/manage inboxes on the fly, with no email inbox passwords, etc.
I have an email storage database in place, and I can receive emails by using a custom smtpd server, replacing postfix. However, that way I'm not able to send emails via postfix (using smtplib, connecting through port 25 to postfix and sending emails)
Any solution to this problem? How to send emails with a custom smtp server? Can I configure postfix to relay all incoming emails to a custom smtp server running in another port, and still use postfix on port 25 to send emails?
Thanks for your time
By using a custom SMTP server, you run the risk of inadvertently creating security holes or violating the SMTP protocol in some way. With so many great SMTP servers out there (Postfix, exim, sendmail...), that doesn't sound like a good option to me.
The easiest way I can think of to solve that issue is to use Postfix to relay inbound and outbound e-mail.
Inbound e-mail can be configured to be piped to an application, and outbound e-mail can be configured to be delivered by Postfix, either directly or relayed through a different server.
This way, you can use, instead of a custom SMTP server, an application that is able to parse RFC822-compliant message files. This is better than doing exactly the same thing, but with the overhead of having to implement the SMTP protocol.
This approach probably won't scale well should you need to receive a high volume of messages - every message will fork+exec a new process. If that is a requirement, a good approach would be to keep a custom SMTP server to do that job, but let Postfix relay it the messages - you will then benefit from Postfix's architecture in front of your parser.
Assuming you follow the approach of piping the messages to an application, all you need to do in Postfix is to
Configure Postfix's alias_maps' parameter to look for such a map:
alias_maps = hash:/etc/aliases, hash:/etc/postfix/app-aliases
Then, configure the map to pipe messages sent to each address into an application:
test: "|/usr/local/bin/your-app"
As usual, don't forget to $ postalias app-aliases.
This will make a message sent to test#yourdomain be piped into /usr/local/bin/your-app, which acts as an e-mail gateway to your application.

How would one build an smtp client in python?

buildin an smtp client in python . which can send mail , and also show that mail has been received through any mail service for example gmail !!
Create mail messages (possibly with multipart attachments) with email.
The email package is a library for managing email messages, including MIME and other RFC 2822-based message documents.
Send mail using smtplib
The smtplib module defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener daemon.
If you are interested in browsing a remote mailbox (for example, to see if the message you sent have arrived), you need a mail service accessible via a known protocol. An popular example is the imaplib module, implementing the IMAP4 protocol. IMAP is supported by gmail.
This (imaplib) module defines three classes, IMAP4, IMAP4_SSL and IMAP4_stream, which encapsulate a connection to an IMAP4 server and implement a large subset of the IMAP4rev1 client protocol as defined in RFC 2060. It is backward compatible with IMAP4 (RFC 1730) servers, but note that the STATUS command is not supported in IMAP4.
If you want the Python standard library to do the work for you (recommended!), use smtplib. To see whether sending the mail worked, just open your inbox ;)
If you want to implement the protocol yourself (is this homework?), then read up on the SMTP protocol and use e.g. the socket module.
Depends what you mean by "received". It's possible to verify "delivery" of a message to a server but there is no 100% reliable guarantee it actually ended up in a mailbox. smtplib will throw an exception on certain conditions (like the remote end reporting user not found) but just as often the remote end will accept the mail and then either filter it or send a bounce notice at a later time.

How do I get started processing email related to website activity?

I am writing a web application that requires user interaction via email. I'm curious if there is a best practice or recommended source for learning about processing email. I am writing my application in Python, but I'm not sure what mail server to use or how to format the message or subject line to account for automated processing. I'm also looking for guidance on processing bouncebacks.
There are some pretty serious concerns here for how to send email automatically, and here are a few:
Use an email library. Python includes one called 'email'. This is your friend, it will stop you from doing anything tragically wrong. Read an example from the Python Manual.
Some points that will stop you from getting blocked by spam filters:
Always send from a valid email address. You must be able to send email to this address and have it received (it can go into /dev/null after it's received, but it must be possible to /deliver/ there). This will stop spam filters that do Sender Address Verification from blocking your mail.
The email address you send from on the server.sendmail(fromaddr, [toaddr]) line will be where bounces go. The From: line in the email is a totally different address, and that's where mail will go when the user hits 'Reply:'. Use this to your advantage, bounces can go to one place, while reply goes to another.
Send email to a local mail server, I recommend postfix. This local server will receive your mail and be responsible for sending it to your upstream server. Once it has been delivered to the local server, treat it as 'sent' from a programmatic point of view.
If you have a site that is on a static ip in a datacenter of good reputation, don't be afraid to simply relay the mail directly to the internet. If you're in a datacenter full of script kiddies and spammers, you will need to relay this mail via a public MTA of good reputation, hopefully you will be able to work this out without a hassle.
Don't send an email in only HTML. Always send it in Plain and HTML, or just Plain. Be nice, I use a text only email client, and you don't want to annoy me.
Verify that you're not running SPF on your email domain, or get it configured to allow your server to send the mail. Do this by doing a TXT lookup on your domain.
$ dig google.com txt
...snip...
;; ANSWER SECTION:
google.com. 300 IN TXT "v=spf1 include:_netblocks.google.com ~all"
As you can see from that result, there's an SPF record there. If you don't have SPF, there won't be a TXT record. Read more about SPF on wikipedia.
Hope that helps.
Some general information with regards to automated mail processing...
First, the mail server "brand" itself isn't that important for broadcasting or receiving emails. All of them support the standard smtp / pop3 communications protocol. Most even have IMAP support and have some level of spam filtering. That said, try to use a current generation email server.
Second, be aware that in an effort to reduce spam a lot of the receiving mail servers out there will simply throw a message away instead of responding back that a mail account doesn't exist. Which means you may not receive those.
Bear in mind that getting past spam filters is an art. A number of isp's watch for duplicate messages, messages that look like spam based on keywords or other content, etc. This is sometimes independent of the quantity of messages sent; I've seen messages with as few as 50 copies get blocked by AOL even though they were legitimate emails. So, testing is your friend and look into this article on wikipedia on anti-spam techniques. Then make sure your not doing that crap.
**
As far as processing the messages, just remember it's a queued system. Connect to the server via POP3 to retrieve messages, open it, do some action, delete the message or archive it, and move on.
With regards to bouncebacks, let the mail server do most of the work. You should be able to configure it to notify a certain email account on the server in the event that it is unable to deliver a message. You can check that account periodically and process the Non Delivery Reports as necessary.

Categories