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?
Related
For my use case, I would like to manually set the displayed email addresses in the "to" and "from" field headers of the email, separate from the actual email recipient and sender. I am currently using the smtplib library in python and have managed to accomplish the desired effect with the "to" field and was looking to replicate it for the "from" field as well.
What I have so far:
EMAIL_ADDRESS_G = 'ayush.warikoo77#gmail.com'
from email.message import EmailMessage
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS_G, EMAIL_PASSWORD_G)
# What I would like to be displayed in the email
msg = EmailMessage()
msg["Subject"] = "Test"
msg["To"] = 'test#gmail.com' # shows up
msg['From'] = 'test#gmail.com' # does not show up
msg.set_content("Test body")
# Where I would like to be setting the actual email sender and recipient
smtp.send_message(msg, from_addr=EMAIL_ADDRESS_G, to_addrs=EMAIL_ADDRESS_G)
The above code produces the following:
As shown, the "to" field displays the desired set address, while the "from" field displays my actual email instead of "test#gmail.com". I believe it is being set when I call login with the account, but I am unsure if I can override it. Also happy to use another python email library, if it is not possible with smtplib.
Current --> Desired
To: test#gmail.com
From: ayush.warikoo77#gmail.com --> test#gmail.com
Actual Sender: ayush.warikoo77#gmail.com
Actual Reciever: ayush.warikoo77#gmail.com
Note that this would be used for archiving purposes, where a designated email client might actually be sending the emails, however, I would like the email to use the to and from fields of the message it is trying to document. So the desired displayed "from" field is separate from the actual sender.
Authenticated Gmail SMTP prevents you from spoofing the From header, presumably to prevent abuse.
For archiving purposes, using IMAP’s APPEND command will allow you to place whatever you like in your own mailbox (as it doesn’t count as sending email) and may be a better solution. (You will need to use an App Specific Password or OAUTH to login though).
I am having trouble sending an email to a list of recipients using an smtp.
I can send the email to the first recipient but not the rest. My recipients are in a list. I have tried turning the list into a string. As well as adding a comma or a semicolon to each email in the list but each to no avail.
My email list is formatted like this:
['name#email.com', 'name#email.com']
And I am using this to send it:
from Dmail import Email
sender_email = 'sender#email.com'
with Email(mail_server="smtp.myserver.org", sender_email=sender_email, mail_port=25, mail_use_ssl=False,
mail_use_tls=False) as email:
email.send("Test Body", email_list, subject="test")
Any help on this appreciated.
Currently, I have the email sending to myself and I can see that there are multiple recipients in the "to" column, but none of them are actually receiving the email.
Using Python 3.9+
Thank you.
I was able to fix this by doing:
email_list= '; '.join(email_list)
and
email.send("Test Body", email_list.split(';'), subject="test")
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.
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 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.