Need your assistance in saving the email as .png. The code below will get the body of the email, but it is unable to get a screenshot in the body of the email.
with open(r"output.txt", "w") as output:
for item in fromfolder.filter(is_read=False):
output.write('{}\n'.format(item.body))
item.is_read = True
item.save()
item.move(archieve)
Have tried saving email as eml and msg, but nothing is working out.
item.body contains the entire body of the email message, not just the image contained in the body.
exchangelib does not offer methods to parse the body of the email. You'll need to use other packages for that.
I think your best bet would be to parse the item.mime_content field which contains the raw email content. You can use e.g. email.parser.BytesParser.parse_bytes(mime_content) from https://docs.python.org/3/library/email.parser.html. This will return an EmailMessage with your PNG image.
Related
Kindly what would be the method for message object within imapclient library,
for example, when fetching a message via UIDs, I can use header = str(message.get_subject())
to get the email message subject, what would be the method for attachments please?
I have a question related to last answer in How do I send HTML Formatted emails, through the gmail-api for python but unfortunately the answer does not work for me. If I attach both the 'plain' and 'html' parts, it only accepts the LAST 'attach' call I make. That is, if I attach as 'plain' AFTER 'html', it only sends as 'plain',(which looks unappealing on devices/apps with HTML rendering)., but if I attach the 'html' AFTER 'plain', it only sends the 'html' format (which looks bad on devices/apps without HTML rendering). Unlike the person who posted that question, I do need both parts because some of the devices/apps that receive my emails do not render HTML and need the plain text part.
This is not a problem if I used 'smtplib' instead of GMAIL-API, but I want to use gmail api for better security in my app.
Here is my code:
message = MIMEMultipart('alternative')
message['to'] = to_email
message['from'] = from_email
message['subject'] = subject
body_plain = MIMEText(email_body,'plain')
message.attach(body_plain)
body_html_format="<u><b>html:<br>"+email_body+"</b></u>"
body_html = MIMEText(body_html_format,'html')
message.attach(body_html) # PROBLEM: Will only send as HTML since this was added LAST.
raw_string = base64.urlsafe_b64encode(message.as_bytes()).decode()
request = service.users().messages().send(userId='my.email#gmail.com',body={'raw':raw_string})
message = request.execute()
Thanks and regards,
Doug
I'm trying to send a simple mail with SendGrid which must contain one hyperlink.
I'm not doing anything fancy, just following the documentation example with some changes
import os
from sendgrid.helpers.mail import *
sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))
from_email = Email("test#example.com")
to_email = To("test#example.com")
subject = "Sending with SendGrid is Fun"
content = Content("text/html", '<html>google</html>')
mail = Mail(from_email, to_email, subject, content)
response = sg.client.mail.send.post(request_body=mail.get())
It looks fine to me, but once I run the script and the mail is sent, it shows up like plain text I cannot click on.
I also tried many other combinations removing the <html> tag, using single and double quotes with the backslash, but nothing really worked. I even tried to do the same thing without the Mail Helper Class, but it didn't work.
Thanks very much for the help.
content = Content(
"text/html", "Hi User, \n This is a test email.\n This is to also check if hyperlinks work <a href='https://www.google./com'> Google </a> Regards Karthik")
This helped me. I believe you don't need to mention the html tags
I just started up with APIs and figured I'd play around with Gmail's. I'm looking to scrape all emails sent to me in the past month for some text analysis. I might just be being thick here or have missed some documentation somewhere (probably), but I can't figure out how to get the body of emails that had attachments. I'm not interested in the attachment, just the body of the email.
results = gmail.users().messages().list(labelIds=['INBOX'], q='to:me after: '+str(date),userId='me').execute()
mssg_list = results['messages']
for mssg in mssg_list:
m_id = mssg['id']
message = gmail.users().messages().get(userId='me', id=m_id).execute()
body = message['payload']['parts'][1]['body']
final_body = base64.urlsafe_b64decode(body['data'].decode("utf-8"))
For messages with attachments, it returns only attachmentId and size, rather than size and data. I tried reading the attachmentId to see if perhaps data was kept there instead, but no dice; it appears to just refer to the attachment. Where is the actual text body living?
I am using email.message_from_string to parse an email message into Python. The documentation doesn't seem to say what standard fields there are.
How do I know what fields are available to read from, such as msg['to'], msg['from'], etc.? Can I still find this if I don't have an email message to experiment with on the command line?
email.message_from_string() just parses the headers from the email. Using keys() you get all present headers from the email.
import email
e = """Sender: test#test.dk
From: test#test.dk
HelloWorld: test
test email
"""
a = email.message_from_string(e)
print a.keys()
Outputs: ['Sender', 'From', 'HelloWorld']
Therefore, you will never find a manual that includes from, to, sender etc. as they are not part of the API, but just parsed from the headers.