Integrate postfix mail into my (python) webapp

I have a postfix server that listens and receives all emails received on mywebsite.com. Now I want to show these postfix messages in a custom interface, and this is also for each user.

To be clear, all mywebsite.com users will be provided with email addresses, such as someguy@mywebsite.com , that receive email on my production machine, but he sees them in his console, which is built into his toolbar on mywebsite.com.

So for the user to see the mail he received, I need to create a postfix email replica so that mywebsite (which works on django-python) will easily reflect them. How do I achieve this. To be precise, this is my question, how do I convert postfix mail to a python mail object (so that my system / website) understands this?

To be clear, I wrote psuedo code to achieve what I want:

email_as_python_object = postfix_email_convertor(postfix_email) attachments_list = email_as_python_object.attachments body = email_as_python_object.body # be it html or whatever 

And by the way, I tried the default email module that comes with python, but this is not convenient for all cases. And even I need to deal with mail attachments manually (which I hate). I just need an easy way to deal with such cases (I was wondering how postfix understands the received email, i.e. how it automatically detects different headers, attachments, etc.). Please help me.

+4
source share
3 answers

First of all, Postfix mail routing rules can be very complex, and the preferred solution seems to include a lot of hype in the wrong places. You don’t want to accidentally show someone else’s emails to someone, do you? Secondly, although Postfix can do almost everything, it should not, because it is only an MDA (mail delivery agent).

Your solution is best solved using a POP3 or IMAP server (Cyrus IMAPd, Courier, etc.). IMAP servers can have "superuser accounts" that can read messages from all users. Then your web application can connect to the users mailbox and return headers and bodys.

If you want to show only the subject line, you can get them with a special IMAP command and very low overhead. However, Python’s IMAP library is not the easiest way to understand the API. I will take a snapshot (not verified!) With an example taken from the standard library:

 import imaplib sess = imaplib.IMAP4() sess.login('superuser', 'password') # Honor the mailbox syntax of your server! sess.select('INBOX/Luke') # Or something similar. typ, data = sess.search(None, 'ALL') # All Messages. subjectlines = [] for num in data[0].split(): typ, msgdata = sess.fetch(num, '(RFC822.SIZE BODY[HEADER.FIELDS (SUBJECT)])') subject = msgdata[0][1].lstrip('Subject: ').strip() subjectlines.append(subject) 

This log is registered on the IMAP server, selects the users mailbox, retrieves all message identifiers, and then extracts (I hope) only storylines and adds the data to the list of thematic sections.

To get other parts of mail, change the line using sess.fetch. For specific fetch syntax, see RFC 2060 (Section 6.4.5).

Good luck

+7
source

You want postfix to deliver to the local mailbox and then use the email system to access this saved mail.

Do not get hung up on postfix - it's just a transfer agent - it receives messages from one place and puts them in another place, it does not save messages. Therefore, postfix will accept messages through SMTP and put them in local mail files.

Then IMAP or some webmail system will display these messages to your users.

If you want mail to be integrated into your webapp, you probably should start the IMAP server and use the python IMAP libraries to receive messages.

+9
source

I'm not sure I understand this question.

If you want your remote web application to be able to view the users mailbox, you can install a pop server or imap server and use the mail client (you can find it from the shelf) to read emails. Alternatively, you can write something to query the pop / imap server using the appropriate libraries that come with Python itself.

If you want to replicate mail to another machine, you can use procmail and configure the steps for this. Postfix can be configured to invoke procmail this way.

0
source

All Articles