Parse email fields - python

I want to parse email addresses from a To: email field.
Indeed, when looping on the emails in a mbox:
mbox = mailbox.mbox('test.mbox')
for m in mbox:
print m['To']
we can get things like:
info#test.org, Blahblah <blah#test.com>, <another#blah.org>, "Hey" <last#one.com>
That should be parsed into:
[{email: "info#test.org", name: ""},
{email: "blah#test.com", name: "Blahblah"},
{email: "another#blah.org", name: ""},
{email: "last#one.com", name: "Hey"}]
Is there something already built-in (in mailbox or another module) for this or nothing?
I read a few times this doc but I didn't find something relevant.

You can use email.utils.getaddresses() for this:
>>> getaddresses(['info#test.org, Blahblah <blah#test.com>, <another#blah.org>, "Hey" <last#one.com>'])
[('', 'info#test.org'), ('Blahblah', 'blah#test.com'), ('', 'another#blah.org'), ('Hey', 'last#one.com')]
(Note that the function expects a list, so you have to enclose the string in [...].)

email.parser has the modules you're looking for. email.message is still relevant, because the parser will return messages using this structure, so you'll be getting your header data from that. But to actually read the files in, email.parser is the way to go.

As pointed by #TheSpooniest, email has a parser:
import email
s = 'info#test.org, Blahblah <blah#test.com>, <another#blah.org>, "Hey" <last#one.com>'
for em in s.split(','):
print email.utils.parseaddr(em)
gives:
('', 'info#test.org')
('Blahblah', 'blah#test.com')
('', 'another#blah.org')
('Hey', 'last#one.com')

Python provides email.Header.decode_header() for decoding header. The function decode each atom and return a list of tuples ( text, encoding ) that you still have to decode and join to get the full text.
For addresses, Python provides email.utils.getaddresses() that split addresses in a list of tuple ( display-name, address ). display-name need to be decoded too and addresses must match the RFC2822 syntax. The function getmailaddresses() does all the job.
Here's a tutorial that might help http://blog.magiksys.net/parsing-email-using-python-header

Related

Insert a value before comma in a file

I'm new to Python and I'm trying to create a simple program for login which reads/writes the information to/from a text file, but I'm having an issue.
Let's say I have the following content in a text file:
mytest#gmail.com, testPass123
First the email and after the comma the password. How can I read those two separately?
I have used .split(',') but it stores the whole line.
If I run this:
email = []
for line in file:
email.append(line.split(','))
print(email[0])
I get the following output:
['mytest#gmail.com', ' testPass123\n']
I think your variable naming is confusing you here. If you name email accounts, things might become clearer:
accounts = []
for line in file:
accounts.append(line.strip().split(','))
for email, password in accounts:
print("Email:", email, "Password:", password)
You may be looking for multiple assignment
>>> a, b = "em#ail, pass".split(",")
>>> a
'em#ail'
>>> b
' pass'

Using Regex to find and replace email addresses

New to Python and would like to use it with Regex to work with a list of 5k+ email addresses. I need to change the encapsulate each address with either quotes. I am using \b[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,}\b to identify each email address. How would I replace the current entry of user#email.com to "user#email.com" adding quotes around the each of the 5k email addresses?
You can use re.sub module and using back-reference like this:
>>> a = "this is email: someone#mail.com and this one is another email foo#bar.com"
>>> re.sub('([A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,})', r'"\1"', a)
'this is email: "someone#mail.com" and this one is another email "foo#bar.com"'
UPDATE: If you have a file that want to replace emails in each line of it you can use readlines() like this:
import re
with open("email.txt", "r") as file:
lines = file.readlines()
new_lines = []
for line in lines:
new_lines.append(re.sub('([A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,})', r'"\1"', line))
with open("email-new.txt", "w") as file:
file.writelines(new_lines)
email.txt:
this is test#something.com and another email here foo#bar.com
another email abc#bcd.com
still remaining someone#something.com
email-new.txt (after running the code):
this is "test#something.com" and another email here "foo#bar.com"
another email "abc#bcd.com"
still remaining "someone#something.com"

python %s is discarding \n while sending via POST method

I am working on an email sending project in python
when i pass %s in post method with a variable its discarding all new lines and sending it to email so im getting all stuffed emails can somebody please help
post method :
r = requests.post('https://maker.ifttt.com/trigger/Custom Newsfeed/with/key/nYmoaoROeu6Mf2SrBOgUg?value1=%s' % (s))
so if s contains a news with headline and body, in email im getting it all concatenated together
If on python3, try
'value1={}'.format(s)
Or if on 3.6, try
f'value1={s}'
Use format() to construct that url. Also, learn about using named placeholders using format(), the example provided below makes use of them.
s = s.replace('\n', '%0A') # Replaces \n with urlencoded \n (%0A)
url = 'https://maker.ifttt.com/trigger/Custom Newsfeed/with/key/nYmoaoROeu6Mf2SrBOgUg?value1={value1}'.format(value1=s)
r = requests.post(url)
You could also use urlencode.
>>> import urllib
>>> f = { 'value1' : s}
>>> urllib.urlencode(f)
'value1=cool+event'

Trailing equal signs (=) in emails

I download messages from a Gmail account using POP3 and save them in a SQLite database for futher processing:
mailbox = poplib.POP3_SSL('pop.gmail.com', '995')
mailbox.user(user)
mailbox.pass_(password)
msgnum = mailbox.stat()[0]
for i in range(msgnum):
msg = '\n'.join(mailbox.retr(i+1)[1])
save_message(msg, dbmgr)
mailbox.quit()
However, looking in the database, all lines but the last one of the message body (payload) have trailing equal signs. Do you know why this happens?
Frederic's link lead me to the answer. The encoding is called "quoted printable" (wiki) and it's possible to decode it using the quopri Python module (documentation):
msg.decode('quopri').decode('utf-8')
Update for python 3.x
You now have to invoke the codecs module.
import codecs
bytes_msg = bytes(msg, 'utf-8')
decoded_msg = codecs.decode(bytes_msg, 'quopri').decode('utf-8')

Determine unique "from" email addresses in Maildir folder

I want to find out a list of "From" addresses in a Maildir folder. Using the following script, it illustrates the varying formats that are valid in From:
import mailbox
mbox = mailbox.Maildir("/home/paul/Maildir/.folder")
for message in mbox:
print message["from"]
"John Smith" <jsmith#domain.com>
Tony <tony#domain2.com>
brendang#domain.net
All I need is the email address, for any valid (or common) "From:" field format. This must have been solved a crazillion times before, so I was expecting a library. All I can find is various regexes.
Is there a standard approach?
email.utils.parseaddr is your friend:
>>> emails = """"John Smith" <jsmith#domain.com>
Tony <tony#domain2.com>
brendang#domain.net"""
>>> lines = emails.splitlines()
>>> from email.utils import parseaddr
>>> [parseaddr(email)[1] for email in lines]
['jsmith#domain.com', 'tony#domain2.com', 'brendang#domain.net']
So you should just be able to work with:
for message in mbox:
print parseaddr(message['from'])
Then, I guess if you just want unique email addresses, then you can just use a set directly over mbox, eg:
mbox = mailbox.MailDir('/some/path')
uniq_emails = set(parseaddr(email['from'])[1] for email in mbox)

Categories