Is it possible to have two mailboxes(folders) with same name? - python

I am working on a product where I have to interact with email servers (like zimbra, dovecot etc) through IMAP to fetch emails etc.
I am using Python's imaplib library. I connect to email server account with credentials and then select a mailbox (folder) through its name.
Now I am wondering whether is it possible to have two mailboxes with same name in an account? If yes then how to go about it ?

Question: I am probably unclear on this.
Using select(mailbox=... you are dealing with Real Folders.
Therefore no duplicate possible.
IMAP4.select(mailbox=’INBOX’, readonly=False)
Select a mailbox.
Returned data is the count of messages in mailbox (EXISTS response).
The default mailbox is 'INBOX'.
If the readonly flag is set, modifications to the mailbox are not allowed.

Related

Python email create

i can create with python outlook emails and send them. Im Using
The SenderEmailAddress property is read-only and set on received or sent emails, not composed ones.
The Outlook object model provides two main ways for sending emails from other accounts:
The MailItem.SendUsingAccount property which returns or sets an Account object that represents the account under which the MailItem is to be sent. In that case the other account should be configured in an Outlook profile as well.
The MailItem.SentOnBehalfOfName property which returns or sets a string indicating the display name for the intended sender of the mail message. In that case you need to have all the required permissions to do that on behalf of another person. Valid for Exchange only.
On a general note, no mail server (certainly not Exchange Server) will let you spoof the sender name and address (unless you were given an explicit permisison) for the obvious security reasons.
You can send on behalf of another Exchange user (if you have the permission) by setting the MailItem.SentOnBehalfOfName property. You can also create secondary Exchange / POP3/SMTP / IMAP4/SMTP accounts in Outlook and send through these accounts by setting the MailItem.SendUsingAccount property to an Account object from the Application.Session.Accounts collection.

Python: Verify if e-mail does not exist safely

We have a database of over 200.000 e-mail addresses and associated contacts. I had an idea that if I could find out which e-mail addresses don't exist anymore I could inactivate those contacts, thus keeping a more up to date database. My main goal is not to validate if an e-mail exists. My main goal is to find as many non-exsitent e-mail addresses as possible.
I have based a lot of my research on these answers: How to check if an email address exists without sending an email?
I have tried the python validate_email library, but it was very unreliable. It's also unsafe because you could get banned if you try to validate multiple contacts at the same company any time. It returned False to my active company e-mail, and None to my active gmail as well... so definitely unreliable.
I have tried both DNS with py3dns and MX records. Also the VRFY command. Unfortunately none of these seemed to be reliable since any e-mail server could send a fake response.
Greylisting is also a problem:
There is also an antispam technique called greylisting, which will
cause the server to reject the address initially, expecting a real
SMTP server would attempt a re-delivery some time later. This will
mess up attempts to validate the address.
The idea also occured to my that I could send a dummy e-mail, or two because of greylisting with a bit of delay in between them. I am afraid that this could get me blacklisted after a while, especially if multiple of these contacts work at the same company. Another idea is to do this from randomly generated e-mail addresses and hosts but that is probably not possible. Is there any way I could determine if an e-mail does not exist, preferably in a way that the chance of getting banned is minimal?

Checking folders different than inbox using poplib in Python

Some time ago I ve written a Python script, using poplib library, which retrieves messages from my pop3 email account. Now I would like to use it to retrieve emails from different mail server which works with IMAP. It works well, but only to retrieve messages from Inbox. Is there any way to also get emails from other folders like Spam, Sent etc? I know I could use imaplib and rewrite the script, but my questions is if it's possible to obtain that with poplib.
No.
POP is a single folder protocol. It is very simple and was not designed for multiple folders.
You will need to use IMAP or other advanced protocols to access additional folders.

gmail IMAP clone mailbox: issue duplicating emails with same UID

I am trying to replicate a Google Apps mailbox into another Google account. I am using the IMAP library in Python.
I am able to get a unique UID for each mailbox. I could, download each message from each mailbox using thie UID.
The problem is that the unique UID is per mailbox, not per account. For example, one email may reside in two mailboxes, or have two labels. It will show up as two separate UIDs. If I download the email twice, they are no longer the same email. When one is deleted, the other will remain, etc.
Gmail has a X-GM-MSGID which gives a unique ID per the account.
At the moment, the only way I know of getting this X-GM-MSGID is by first getting the UID and requesting it's X-GM-MSGID. If the inbox has 10,000 emails, this will become a lot of requests. Is there a another way to get the X-GM-MSGID of all emails in an inbox, or even better, of all mailboxes?
I believe once I have the unique ID of each email, I can then FETCH the flags, time and content of each email and APPEND it onto the new server.
You can get multiple X-GM-MSGIDs using a bulk fetch:
FETCH 1:* X-GM-MSGID
However, there is a better approach.
Operate entirely within the All Mail folder (\AllMail from XLIST), and FETCH and STORE the X-GM-LABELS for each message.

Integrate postfix mail into my (python)webapp

I have a postfix server listening and receiving all emails received at mywebsite.com Now I want to show these postfix emails in a customized interface and that too for each user
To be clear, all the users of mywebsite.com will be given mail addresses like someguy#mywebsite.com who receives email on my production machine but he sees them in his own console built into his dashboard at mywebsite.com.
So to make the user see the mail he received, I need to create an email replica of the postfix mail so that mywebsite(which runs on django-python) will be reflecting them readily. How do I achieve this. To be precise this is my question, how do I convert a postfix mail to a python mail object(so that my system/website)understands it?
Just to be clear I have written 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 have tried default email module which comes with python but thats not handy for all the cases. And even I need to deal with mail attachments manually(which I hate). I just need a simple way to deal with cases like these(I was wondering how postfix understands a email received. ie.. how it automatically figures out different headers,attachments etc..). Please help me.
You want to have postfix deliver to a local mailbox, and then use a webmail system for people to access that stored mail.
Don't get hung up on postfix - it just a transfer agent - it takes messages from one place, and puts them somewhere else, it doesn't store messages.
So postfix will take the messages over SMTP, and put them in local mail files.
Then IMAP or some webmail system will display those messages to your users.
If you want the mail integrated in your webapp, then you should probably run an IMAP server, and use python IMAP libraries to get the messages.
First of all, Postfix mail routing rules can be very complex and your presumably preferred solution involves a lot of trickery in the wrong places. You do not want to accidentally show some user anothers mails, do you? Second, although Postfix can do almost anything, it shouldn't as it only is a MDA (mail delivery agent).
Your solution is best solved by using a POP3 or IMAP server (Cyrus IMAPd, Courier, etc). IMAP servers can have "superuser accounts" who can read mails of all users. Your web application can then connect to the users mailbox and retreive the headers and bodys.
If you only want to show the subject-line you can fetch those with a special IMAP command and very low overhead. The Python IMAP library has not the easiest to understand API though. I'll give it a shot (not checked!) 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 logs into the IMAP server, selects the users mailbox, fetches all the message-ids then fetches (hopefully) only the subjectlines and appends the resulting data onto the subjectlines list.
To fetch other parts of the mail vary the line with sess.fetch. For the specific syntax of fetch have a look at RFC 2060 (Section 6.4.5).
Good luck!
I'm not sure that I understand the question.
If you want your remote web application to be able to view users' mailbox, you could install a pop or imap server and use a mail client (you should be able to find one off the shelf) to read the emails. Alternatively, you could write something to interrogate the pop/imap server using the relevant libraries that come with Python itself.
If you want to replicate the mail to another machine, you could use procmail and set up actions to do this. Postfix can be set up to invoke procmail in this wayy.

Categories