First I connect to my own mailbox using such code:
import email
import imaplib
imap_server = imaplib.IMAP4_SSL('imap.gmail.com', 993)
imap_server.login("myemail", "mypassword")
imap_server.select('inbox')
Then I get the last email on it in the way like:
_, data = imap_server.search(None, 'ALL')
id_list = data[0].split()
_, data = imap_server.fetch(id_list[-1], '(RFC822)')
rawmail = data[0][1]
prev_mail = email.message_from_bytes(rawmail)
And after that, I need to get sender email from prev_mail variable.
I tried it myself a little bit, and figured out that prev_mail['From'] is usually sth like this: "John Snow" <johnsnow#gmail.com>, so that means I can just grab the <*> part and use it.
But the question is: will it always be like this so I could use my method? And are there any better ways to do this rather than mine?
EDIT:
There are multiple variants of From: headers. Python 3.8 has a good parser for this in the email.header module. But perhaps the Return-Path: header would actually be closer to what you really want? - tripleee
Thank you very much, tripleee, that's exactly what I needed!
Related
I've tried with no conclusions to resend emails with Python.
Once I've logged in SMTP and IMAP with TLS, this is what I have written:
status, data = self._imapserver.fetch(id, "(RFC822)")
email_data = data[0][1]
# create a Message instance from the email data
message = email.message_from_string(email_data)
# replace headers (could do other processing here)
message.replace_header("From", 'blablabla#bliblibli.com')
message.replace_header("To", 'blobloblo#blublublu.com')
self._smtpserver.sendmail('blablabla#bliblibli.com', 'blobloblo#blublublu.com', message.as_string())
But the problem is that the variable data doesn't catch the information from the email, even if the ID is the one I need.
It tells me:
b'The specified message set is invalid.'
How can I transfer an email with Python?
Like the error message says, whatever you have in id is invalid. We don't know what you put there, so all we can tell you is what's already in the error message.
(Also, probably don't use id as a variable name, as you will shadow the built-in function with the same name.)
There are additional bugs further on in your code; you need to use message_from_bytes if you want to parse it, though there is really no need to replace the headers just to resend it.
status, data = self._imapserver.fetch(correct_id, "(RFC822)")
self._smtpserver.sendmail('blablabla#bliblibli.com', 'blobloblo#blublublu.com', data[0][1])
If you want to parse the message, you should perhaps add a policy argument; this selects the modern EmailMessage API which was introduced in Python 3.6.
from email.policy import default
...
message = email.message_from_bytes(data[0][1], policy=default)
message["From"] = "blablabla#bliblibli.com"
message["To"] = "blobloblo#blublublu.com"
self._smtpserver.send_message(message)
The send_message method is an addition to the new API. If the message could contain other recipient headers like Cc:, Bcc: etc, perhaps using the good old sendmail method would be better, as it ignores the message's headers entirely.
I'm trying to work with email messages in Python 3.7 and struggling with what looks like compatibility issues. The docs mention email.message.Message having an iter_parts method that should allow me to do a non-recursive walk of message parts.
This doesn't exist on messages returned from mailbox messages and it's taken me a while to get it behaving. For example, I can generate a dummy message with:
from email.message import EmailMessage
msg = EmailMessage()
msg['Subject'] = 'msg 1'
msg.add_alternative("Plain text body", subtype='plain')
msg.add_alternative("<html><body><p>HTML body</p></body></html>", subtype='html')
msg.add_attachment(b"Nothing to see here!", maintype='data', subtype='raw')
and then dump out the parts with:
def iter_parts(msg):
ret = msg.get_content_type()
if msg.is_multipart():
parts = ', '.join(iter_parts(m) for m in msg.iter_parts())
ret = f'{ret} [{parts}]'
return ret
iter_parts(msg)
which gives me: multipart/mixed [multipart/alternative [text/plain, text/plain], data/raw]
but if I save this to a mbox file and reload it:
import mailbox
mbox = mailbox.mbox('/tmp/test.eml')
mbox.add(msg)
iter_parts(mbox[0])
it tells me AttributeError: 'mboxMessage' object has no attribute 'iter_parts'
Initially I thought it might be related to https://stackoverflow.com/a/45804980/1358308 but setting factory=None doesn't seem to do much in Python 3.7.
Am posting my solution, but would like to know if there are better options!
After much poking and reading of source I found that I can instead do:
from email import policy
from email.parser import BytesParser
mbox = mailbox.mbox('/tmp/test.eml', factory=BytesParser(policy=policy.default).parse)
and then I get objects with an iter_parts method.
I'm writing a script to receive emails from my gmail email in python. I'm managing to download the raw email however I am then unable to access certain types of it, E.G BODY, TO, FROM etc.
import imaplib, email
msrvr = imaplib.IMAP4_SSL('imap.gmail.com', 993)
unm = 'stackoverflow#gmail.com'
pwd = 'lovetocode'
msrvr.login(unm,pwd)
stat,cnt = msrvr.select('Inbox')
stat, dta = msrvr.fetch(cnt[0], '(RFC822)')
b = email.message_from_string(str(dta))
print(b)
print(b['[To]'])
msrvr.close()
msrvr.logout()
Where am I going wrong?
You might find it easier to use native Python Google SDK's for working with their email:
https://developers.google.com/appengine/docs/python/mail/
The imaplib module you are using is will only give you a subset of all gmail features..
Here's some code that parses an email and prints some header fields:
msg = email.message_from_string(raw_email)
for field in ('From', 'Subject', 'Received', 'Message-ID'):
print '{0}: {1}'.format(field, msg[field])
For debugging, also print the raw parts of the Message object:
print msg.__dict__
(Note: I'm using Python2.7, but I believe there's not much difference.)
Basically, I am try to write a program where I need to login into my email account and check the header of recently delivered emails.
I am a complete novice in terms of this type of stuff and would prefer to do the programming in Ruby. I also have some skill in Python if this would make the task easier. Thanks in advance!
The documentation for Python's imaplib library includes an example that does pretty much that:
import getpass, imaplib
M = imaplib.IMAP4()
M.login(getpass.getuser(), getpass.getpass())
M.select()
typ, data = M.search(None, 'ALL')
for num in data[0].split():
typ, data = M.fetch(num, '(RFC822)')
print 'Message %s\n%s\n' % (num, data[0][1])
M.close()
M.logout()
You can modify the search string from 'ALL' to e.g. (since "24-May-2012"). I don't think IMAP supports searching by times; you'll probably have to do that filtering yourself.
I want to parse some emails from a user 's inbox but when I do:
typ, msg_data = imap_conn.fetch(uid, '(RFC822)')
It marks the email as SEEN or read. This is not the desired functionality. Do you know how can I keep the email at its previous stare either SEEN or NOT SEEN?
You might also set read_only to true when selecting the folder:
imap_conn.select('Inbox', readonly=True)
The following should work:
typ, msg_data = imap_conn.fetch(uid, '(BODY.PEEK[HEADER])')
or BODY.PEEK[TEXT], etc.
You can use (RFC822.PEEK) as the "message-parts" argument, according to RFC 1730 (I have not verified which servers actually implement that correctly, but it doesn't seem hard for them to).
You may use imap_tools package:
https://pypi.org/project/imap-tools/
from imap_tools import MailBox, Q
# get list of email subjects from INBOX folder
with MailBox('imap.mail.com').login('test#mail.com', 'password') as mailbox:
# mark_seen=False - not mark emails as seen on fetch
subjects = [msg.subject for msg in mailbox.fetch(mark_seen=False)]