When reading emails from a specific folder (Office365), the body of the message is a random long string that makes no sense.
Initially, I sent those emails to my personal mail as copy, to a specific folder. Reading from there I haven't had problems.
But when I try to read directly from the main inbox, the messages are long strings that makes no sense (so, I can´t parse anything)
mail = imaplib.IMAP4_SSL(SMTP_SERVER)
mail.login(FROM_EMAIL, FROM_PWD)
boxes = mail.list()
mail.select('INBOX/netstatus', readonly=True)
(type, data) = mail.uid('SEARCH', None, '(UNSEEN)')
mail_ids = data[0]
id_list = mail_ids.split()
def read_email(self, *id_list):
id_list = list(id_list)
for i in reversed(id_list):
(typ, data) = mail.uid('FETCH', i, '(RFC822)')
for response_part in data:
if isinstance(response_part, tuple):
print("Response: ",response_part)
msg = \ email.message_from_string(response_part[1].decode('utf-8'))
body = ''
email_subject = msg['subject']
email_from = msg['from']
email_date = msg['date']
message= msg.get_payload().encode('utf-8')
print(message)
I receive something like this when I read from my personal inbox:
mymail: b'Status from Device (x.x.x.x) to AnotherDevice (y.y.y.y), interface A B, in Protocol came up\r\n'
But when I read from the main inbox:
'QWRq4N5IGZyb20gVVREFxLXJlMSAuMTAuMjUsIDAxMDAuMTAwMS4w\r\nMDI1LjAwKSB0byBVU0FEQUxIERS4MC4xMC4xMMDAxLjAw\r\nMjUuMDQpLCBpbnZhY2UgMTAuMTA4AuMjI5IHRvIDEwLjEwLjIwLjEzMCwgaW4gSVNJUy9M\r\nZXZlbDIgd2VudCBkkNvbmZ3VyZWQgd2F0Y2hsaXN0OiI1OQ0K\r\n'
With Python3.6 or later, you can use EmailMessage.get_content() to decode the encoded content automatically.
import email
from email import policy
# Email based on one of the examples at https://docs.python.org/3.6/library/email.examples.html
s = """Subject: Ayons asperges pour le =?utf-8?q?d=C3=A9jeuner?=
From: =?utf-8?q?Pep=C3=A9?= Le Pew <pepe#example.com>
To: Penelope Pussycat <penelope#example.com>,
Fabrette Pussycat <fabrette#example.com>
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64
MIME-Version: 1.0
ICAgIFNhbHV0IQoKICAgIENlbGEgcmVzc2VtYmxlIMOgIHVuIGV4Y2VsbGVudCByZWNpcGllWzFd
IGTDqWpldW5lci4KCiAgICBbMV0gaHR0cDovL3d3dy55dW1tbHkuY29tL3JlY2lwZS9Sb2FzdGVk
LUFzcGFyYWd1cy1FcGljdXJpb3VzLTIwMzcxOAoKICAgIC0tUGVww6kKICAgIAo=
"""
msg = email.message_from_string(s, policy=policy.default)
print(msg.get_content())
Result:
Salut!
Cela ressemble à un excellent recipie[1] déjeuner.
[1] http://www.yummly.com/recipe/Roasted-Asparagus-Epicurious-203718
--Pepé
For multipart messages you need to iterate over the parts:
for part in msg.iter_parts():
print(part.get_content())
Related
I'm writing a Python program that generates an invitation. The code below works approximately.
It send the invitation, the invitation is recognized by an Outlook client, but is not treated by Mac OS calendar.
What is missing or what's wrong ?
Thanks
subject = "Mail subject"
sender_email = "someone#domain.com"
to = ["someone_else#domain.com"]
cc = []
# Create the plain-text and HTML version of your message
text = """\
Text message version
"""
html = """\
<!DOCTYPE html><html><head></head><body><p>The HTML message</p></body></html>
"""
# Create a multipart message and set headers
message = MIMEMultipart("mixed")
message["From"] = sender_email
message["To"] = ", ".join(to)
message["Cc"] = ", ".join(cc)
message["Subject"] = subject
message['Date'] = formatdate(localtime=True)
message_alternative = MIMEMultipart('alternative')
message_related = MIMEMultipart('related')
message_related.attach(MIMEText(html, 'html'))
message_alternative.attach(message_related)
#message_alternative.attach((text, 'plain'))
# --- Begin ical integration ---
ical = CreateIcs() # this function return an ICS content
ical_atch = MIMEBase("text", "calendar", method="REQUEST", name="invite.ics")
ical_atch.set_payload(ical)
encoders.encode_quopri(ical_atch)
message_alternative.attach(ical_atch)
# --- END ical integration ---
message.attach(message_alternative)
# convert message to string
text = message.as_string()
# Send mail
host='127.0.0.1'
port=25
context = None
with smtplib.SMTP(host=host, port=port) as server:
server.sendmail(sender_email, message["To"].split(",") + message["Cc"].split(","), text)
print(f"Mail sent TO : {message['To'].split(',')}, CC : {message['Cc'].split(',')}")
I've created a function in Python which will build an email in html format and send it anonymously through an Exchange server which accepts anonymous email sends.
However, no one in the CC list gets a copy and only the FIRST person in the TO list gets a copy. I've tried 'comma' and 'semicolon' as the email separator but neither works. Curiously, in the email received ALL of the email addresses can be seen (and in the correct format) despite the fact that only one person (the person listed first in the TO list) gets a copy.
This is my code
def send_email(sender, subject, sendAsDraft = True): # sendAsDraft not implemented
global toEmail
global ccEmail
# create the email message
htmlFile = open(saveFolder + '\\' + htmlReportBaseName + '.html',encoding = 'utf-8')
message = htmlFile.read().replace('\n', '')
message = message.replace(chr(8217), "'") # converts a strange single quote into the standard one
message = message.replace("'",''') # makes a single quote html friendly
htmlFile.close()
# get the email body text
emailBody = open('emailBody.txt')
bodyText = emailBody.read()
emailBody.close()
now = dt.datetime.today().replace(second = 0, microsecond = 0)
timeStr = returnReadableDate(now) # returns the date as a string in the 'normal' reading format
# insert the current date/time into the marker in the email body
bodyText = bodyText.replace('xxxx', timeStr)
# insert the email body text into the HTML
message = message.replace('xxxx', bodyText)
msg = MIMEMultipart('Report Email')
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = '; '.join(toEmail)
msg['Cc'] = '; '.join(ccEmail)
msg.add_header('Content-Type','text/html')
msg.set_payload(message)
# complete the email send
message = ''
try:
smtpObj = sl.SMTP('1.2.3.4:5') # obviously IP address anonymised
smtpObj.sendmail(sender, '; '.join(toEmail), msg.as_string())
message = 'Email successfully sent'
except:
message = 'Email could not be sent due to an error'
finally:
# write the outcome to the Instance log
global InstanceLog
# write the log
log(InstanceLog, message)
# close object and exit function
smtpObj.quit()
Is it possible that the issue is at the email server? That where an un-authenticated email can only be sent to one person? That would seem strange in one way, and make sense in another.
Thanks all.
Seamie
Beware. send_message can use the headers of a message to build its recipient list, but sendmail requires a list of addresses or handle a string as a single recipicient address.
And the headers should contain comma instead of semicolons.
So you should have:
...
msg['To'] = ' '.join(toEmail)
msg['Cc'] = ','.join(ccEmail)
...
and later either:
smtpObj.sendmail(sender, toEmail + ccEmail, msg.as_string())
or:
smtpObj.send_message(msg)
Looking at the docs, this should be a comma seperated string:
msg['To'] = ', '.join(toEmail)
I'm using python 2.7 and boto3.
I can not figure out a way to add attachments to SES in python.
The closest thing I found was this page.
So far what I have is this:
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
import boto3
# via http://codeadict.wordpress.com/2010/02/11/send-e-mails-with-attachment-in-python/
ses = boto3.client('ses')
msg = MIMEMultipart()
msg['Subject'] = 'weekly report'
msg['From'] = email
msg['To'] = other_email
# what a recipient sees if they don't use an email reader
msg.preamble = 'Multipart message.\n'
# the message body
part = MIMEText('Howdy -- here is the data from last week.')
msg.attach(part)
# the attachment
part = MIMEApplication(open('cat.jpg', 'rb').read())
part.add_header('Content-Disposition', 'attachment', filename='cat.jpg')
msg.attach(part)
result = ses.send_raw_email(
Source=msg['From'],
Destinations=msg['To'],
RawMessage=msg
)
# and send the message
print result
And i get:
ParamValidationError: Parameter validation failed:
Invalid type for parameter RawMessage, value: From nobody Tue Jul 25 11:21:41 2017
Content-Type: multipart/mixed; boundary="===============0285276385=="
MIME-Version: 1.0
Subject: weekly report
From: email
To: other_email
"email" and "other_email" are censored but in String format 'e#mail.xxx'.
The address is authorized through AWS and the Key and Secret key are already implemented through boto3.
Also got this at the bottom of the output:
type: <type 'instance'>, valid types: <type 'dict'>
Invalid type for parameter Destinations,
value: other_email,
type: <type 'str'>, valid types: <type 'list'>, <type 'tuple'>
I figured it out! There are probably better ways of doing this, but it worked for me. Please let me know how to improve on this. Thank you.
New code:
to_emails = [target_email1, target_email2]
ses = boto3.client('ses')
msg = MIMEMultipart()
msg['Subject'] = 'weekly report'
msg['From'] = from_email
msg['To'] = to_emails[0]
# what a recipient sees if they don't use an email reader
msg.preamble = 'Multipart message.\n'
# the message body
part = MIMEText('Howdy -- here is the data from last week.')
msg.attach(part)
# the attachment
part = MIMEApplication(open('cat.jpg', 'rb').read())
part.add_header('Content-Disposition', 'attachment', filename='cat.jpg')
msg.attach(part)
result = ses.send_raw_email(
Source=msg['From'],
Destinations=to_emails,
RawMessage={'Data': msg.as_string()}
)
# and send the message
print result
I already programmed a function which sends mails with atachments, images on text and other things, but now I need the function to use de Cc (Carbon Copy) function in order to send copies to different emails.
I have done some changes on the function and it works but not as I want.
THe email is sent to the address ("toaddr") and the mail shows that there are other emails added as Cc("tocc") emails, but the Cc emails do not recieve the email.
To be more clear (because I think I am not being very clear) here is an example:
Sender: from#hotmail.com
Receiver: to#hotmail.com
Copied: cc#hotmail.com
to#hotmail.com receives the email and can see that cc#hotmail.com is copied on it.
cc#hotmail.com does not get the email.
if to#hotmail.com reply to all the email, THEN cc#hotmail gets the email.
Can anyone help me telling me what do I need to change on the function?? I guees the problem is with the server.sendmail() function
This is my function:
def enviarCorreo(fromaddr, toaddr, tocc, subject, text, file, imagenes):
msg = MIMEMultipart('mixed')
msg['From'] = fromaddr
msg['To'] = ','.join(toaddr)
msg['Cc'] = ','.join(tocc) # <-- I added this
msg['Subject'] = subject
msg.attach(MIMEText(text,'HTML'))
#Attached Images--------------
if imagenes:
imagenes = imagenes.split('--')
for i in range(len(imagenes)):
adjuntoImagen = MIMEBase('application', "octet-stream")
adjuntoImagen.set_payload(open(imagenes[i], "rb").read())
encode_base64(adjuntoImagen)
anexoImagen = os.path.basename(imagenes[i])
adjuntoImagen.add_header('Content-Disposition', 'attachment; filename= "%s"' % anexoImagen)
adjuntoImagen.add_header('Content-ID','<imagen_%s>' % (i+1))
msg.attach(adjuntoImagen)
#Files Attached ---------------
if file:
file = file.split('--')
for i in range(len(file)):
adjunto = MIMEBase('application', "octet-stream")
adjunto.set_payload(open(file[i], "rb").read())
encode_base64(adjunto)
anexo = os.path.basename(file[i])
adjunto.add_header('Content-Disposition', 'attachment; filename= "%s"' % anexo)
msg.attach(adjunto)
#Send ---------------------
server = smtplib.SMTP('localhost')
server.set_debuglevel(1)
server.sendmail(fromaddr,[toaddr,tocc], msg.as_string()) #<-- I modify this with the tocc
server.quit()
return
In your sendmail call, you're passing [toaddr, tocc] which is a list of lists, have you tried passing toaddr + tocc instead?
I'd like to fetch the whole message from IMAP4 server.
In python docs if found this bit of code that works:
>>> t, data = M.fetch('1', '(RFC822)')
>>> body = data[0][1]
I'm wondering if I can always trust that data[0][1] returns the body of the message. When I've run 'RFC822.SIZE' I've got just a string instead of a tuple.
I've skimmed through rfc1730 but I wasn't able to figure out the proper response structure for the 'RFC822'. It is also hard to tell the fetch result structure from imaplib documentation.
Here is what I'm getting when fetching RFC822:
('OK', [('1 (RFC822 {858569}', 'body of the message', ')')])
But when I fetch RFC822.SIZE I'm getting:
('OK', ['1 (RFC822.SIZE 847403)'])
How should I properly handle the data[0] list?
Can I trust that when it is a list of tuples the tuples has exactly 3 parts and the second part is the payload?
Maybe you know any better library for imap4?
No... imaplib is a pretty good library, it's imap that's so unintelligible.
You may wish to check that t == 'OK', but data[0][1] works as expected for as much as I've used it.
Here's a quick example I use to extract signed certificates I've received by email, not bomb-proof, but suits my purposes:
import getpass, os, imaplib, email
from OpenSSL.crypto import load_certificate, FILETYPE_PEM
def getMsgs(servername="myimapserverfqdn"):
usernm = getpass.getuser()
passwd = getpass.getpass()
subject = 'Your SSL Certificate'
conn = imaplib.IMAP4_SSL(servername)
conn.login(usernm,passwd)
conn.select('Inbox')
typ, data = conn.search(None,'(UNSEEN SUBJECT "%s")' % subject)
for num in data[0].split():
typ, data = conn.fetch(num,'(RFC822)')
msg = email.message_from_string(data[0][1])
typ, data = conn.store(num,'-FLAGS','\\Seen')
yield msg
def getAttachment(msg,check):
for part in msg.walk():
if part.get_content_type() == 'application/octet-stream':
if check(part.get_filename()):
return part.get_payload(decode=1)
if __name__ == '__main__':
for msg in getMsgs():
payload = getAttachment(msg,lambda x: x.endswith('.pem'))
if not payload:
continue
try:
cert = load_certificate(FILETYPE_PEM,payload)
except:
cert = None
if cert:
cn = cert.get_subject().commonName
filename = "%s.pem" % cn
if not os.path.exists(filename):
open(filename,'w').write(payload)
print "Writing to %s" % filename
else:
print "%s already exists" % filename
The IMAPClient package is a fair bit easier to work with. From the description:
Easy-to-use, Pythonic and complete
IMAP client library.
Try my package:
https://pypi.org/project/imap-tools/
example:
from imap_tools import MailBox
# get list of email bodies from INBOX folder
with MailBox('imap.mail.com').login('test#mail.com', 'password', 'INBOX') as mailbox:
bodies = [msg.text or msg.html for msg in mailbox.fetch()]
Features:
Parsed email message attributes
Query builder for searching emails
Work with emails in folders (copy, delete, flag, move, append)
Work with mailbox folders (list, set, get, create, exists, rename, delete, status)
No dependencies
This was my solution to extract the useful bits of information. It's been reliable so far:
import datetime
import email
import imaplib
import mailbox
EMAIL_ACCOUNT = "your#gmail.com"
PASSWORD = "your password"
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login(EMAIL_ACCOUNT, PASSWORD)
mail.list()
mail.select('inbox')
result, data = mail.uid('search', None, "UNSEEN") # (ALL/UNSEEN)
i = len(data[0].split())
for x in range(i):
latest_email_uid = data[0].split()[x]
result, email_data = mail.uid('fetch', latest_email_uid, '(RFC822)')
# result, email_data = conn.store(num,'-FLAGS','\\Seen')
# this might work to set flag to seen, if it doesn't already
raw_email = email_data[0][1]
raw_email_string = raw_email.decode('utf-8')
email_message = email.message_from_string(raw_email_string)
# Header Details
date_tuple = email.utils.parsedate_tz(email_message['Date'])
if date_tuple:
local_date = datetime.datetime.fromtimestamp(email.utils.mktime_tz(date_tuple))
local_message_date = "%s" %(str(local_date.strftime("%a, %d %b %Y %H:%M:%S")))
email_from = str(email.header.make_header(email.header.decode_header(email_message['From'])))
email_to = str(email.header.make_header(email.header.decode_header(email_message['To'])))
subject = str(email.header.make_header(email.header.decode_header(email_message['Subject'])))
# Body details
for part in email_message.walk():
if part.get_content_type() == "text/plain":
body = part.get_payload(decode=True)
file_name = "email_" + str(x) + ".txt"
output_file = open(file_name, 'w')
output_file.write("From: %s\nTo: %s\nDate: %s\nSubject: %s\n\nBody: \n\n%s" %(email_from, email_to,local_message_date, subject, body.decode('utf-8')))
output_file.close()
else:
continue