Making a Python program handle mailto links - python

I made a Python program that is capable of sending email. However, I want to make it capable of being a default email client such that it will capture the email address and subject of HTML email links (mailto links) when the user clicks them.
How do I get the email address and subject to my client?
Currently, I can set my program as the default mail client, but I don't know what information, or format of information, it's getting from the web browser; so, I'm not sure how to parse it.

Assuming the complete link is passed in as sys.argv[1], you need to do something like this:
import urllib.parse
parsed = urllib.parse.urlparse(sys.argv[1])
mail_addr = parsed.path
fields = urllib.parse.parse_qs(parsed.query)
This sets mail_addr to the address to send the email to, while field will be a dictionary of additional parameters.
The fields that you can expect to be present are specified in RFC 6068

Related

Unable to add multiple email addresses to sendkeys() while using Selenium

Unable to add multiple email addresses to sendkeys() while using Selenium. What I'm trying to do is send an email to multiple addreses using selenium remote webdriver to build some test cases.
The below logic only sends the email to the first recipient.
email = "xyz#gmail.com,abc#gmail.com"
driver.find_element_by_name("to").send_keys(email)
The below logic executes fine without throwing any exception but it does not generate the email at all.
emails = ["xyz#gmail.com","abc#gmail.com"]
for email in emails:
time.sleep(5) #to wait for the element to be interactable
driver.find_element_by_name("to").send_keys(email)
Could someone please guide in the right direction? Thanks!
It’s convenient to use loop if you want to add multiple emails but i am not sure what exactly you are trying to do here, but simple solution could be
emails = "xyz#gmail.com,abc#gmail.com"
#split funtion will convert string into list split wrt “,”
emails =emails.split(',')
for email in emails:
driver.find_element_by_name("to").send_keys(email)
UPDATE: SOLUTION FOUND
The email addresses require space in between and it works just fine without looping.

How to use HTML in Google App Engine Mail API

I'm currently setting up email verification for my website that is working with google app engine. I want the users to click a verification link and I would also like to style the email a little using html and css.
So I have my python script all set up. And I have a mail objekt and I do this:
mail.send_mail(sender="...",
to=".....",
subject="Verification Mail"
body="""
Dear ....
Thanks for signing up on example.com
Please click the link below to verify your email adress.
http://example.com/verifymail?email="""+adress+""""&hash="""+hash+"""
We hope you enjoy our platform.
""")
Now this works perfectly fine.
But what I would like is to place an anchor tag inside my mail like
<a href='http://example.com/verifymail?email="""+adress+""""&hash="""+hash+"""'>Verify Email</a>
But doing that will literally print out the html source inside the mail, instead of understanding this as HTML. (It correctly fills in the variables btw). I would also like to add some more things to the newsletter but I guess getting it to understand the tags at all would solve all my problems.
Is there any solution to this?
The reason that your email is interpreted as plain text instead of HTML is that body is interpreted as plain text.
In order for your content to be interpreted as HTML, you will need to set the html attribute as the HTML version of the email, i.e. add something like html="<h3>Hello World</h3>". Check the official API for reference: https://cloud.google.com/appengine/docs/python/refdocs/google.appengine.api.mail
More background about content type: some email clients cannot interpret HTML messages, so usually both HTML and plain text version are sent to the user to guarantee compatibility. That's why an email has both "plain text" and "HTML" fields.

Sending Google Docs content in HTML format via email using GMail API

We are using Google Docs as a Email template resource in one of our application.
We have n number of templates for different different emails.
When the mail is to be sent, what we do is:
Get the html content of that particular template using Google Drive api.
Send that html content in Email using GMail api.
Now the problem is, the some of html attributes changes automatically when person receives email eg. the bold/italics/underline, fonts are replaced by the default fonts of Google Mail.
I have created a sample email template here. You can try playing with it.
Have anyone came across such problem ?
Is there any other alternative to accomplish ?

Parsing emails with bad mime types in python

I am trying to use python's imaplib and email.feedparser to grab an attachment out of a gmail inbox. The email is generated by an external party and sent to us, so I have no control over it.
The trouble is that the message I am trying to parse has msg.get_content_maintype() return 'text' instead of 'multitype'. As a result the uuencoded attachment gets concatenated with the rest of the message and I don't see an ease way to pull it out of email.message.Message.
Any ideas how I can extract the attachment out of such an email?
If it is any help, the email has 'Produced By Microsoft MimeOLE V6.00.3790.4862' in it. Thunderbird also had trouble rendering this email and wasn't able to figure out that it had an attachment. Otherwise, the message looks ok in Outlook and Gmail web client.
As email.parser.FeedParser documentation says you can use _factory parameter to provide your own Message class. You could put your own class derived from email.message.Message that will replace message contenttype with the correct one if this message was composed in "Produced By Microsoft MimeOLE ...blablabla".
I think it is safe to expect that the messagesin this particular case will always be multipart.

extracting individual email from a single thunderbird email data file

I have a thunderbird email data file from which I need to extract individual email. I tried to use regex and do plain vanilla extract based on from tag but this doesn't give me the required result. An email can have another email attached within the body hence a single email can have more than one "From:" strings. How can I extract individual emails out of this data file?
Try the python mailbox module.
The mailbox module can read mbox/maildir message stores.

Categories