Python: Automating Emails - python

Here is what I want to achieve :
1) An email should be sent to using "email address & details" mentioned in a Google docs/excel file.
2) The email should also pick up some content from a web page .i.e Title, URL etc. & include in the email sent.
Do let me know, how or where do I begin?

Start reading this http://docs.python.org/library/email

You might want to start by writing something that just downloads the document and writes it to file. Python has a module for using most of its services: Youtube, gmail, docs, etc.
You could get the gdata client here: http://code.google.com/p/gdata-python-client/downloads/list
And a good place to start would be here: http://code.google.com/apis/documents/

Related

Python / Email processing: How to get the date an attachment was created?

I have a Python program that will read an Outlook inbox using these Python libraries:
1. IMAPClient
2. email
I want to know if it is possible to get the date the email attachment was created.
I don't see anything in email headers that stand out. I can get the date an email was sent (or forwarded), but it is the case when an email is forwarded that prompts this question.
I want to get the date of the attachment inside the email. If anyone has done this, and has a full working code snippet to share, it would be greatly appreciated.
I have done several searches, looked carefully through email headers, looked at the two library documentation I am using (IMAPClient, and email), and see nothing that stands out that would lead to a solution.
Some file formats include this, most don't, some may. For example the EXIF data in some JPEG files includes it. To read that you'll need an EXIF library, not an IMAP library. Microsoft Word files include a creation date, IIRC that's mandatory for that format, but again you'll need a type-specific library.
IMAP is merely the channel through which you download the data you want to examine.

HTML e-mail works fine in browser and gmail app but not in native iphone e-mail client

I wrote a python script that sends an html email that includes texts and images. I can view e-mail perfectly in gmail through gmail app or dekstop browser but images got distorted when I open it in my native iphone e-mail client(not with gamil but with my hotmail account, if that matters).
My first question: Is there any way to fix this?
If no, then my second question is: I always see beautiful e-mail campaigns that works in every email clients. How do companies achieve that?
note/ My final code to send the e-mail:
message = MIMEText(text,'html')
message['to'] = all_list
message['from'] = ''
message['subject'] = ""
raw = base64.urlsafe_b64encode(message.as_bytes())
raw = raw.decode()
message = {'raw': raw}
gmail_service.users().messages().send(userId='me', body=message).execute()
As far as I know, email HTML gets parsed differently in every mail client and the way to generate an email template that would look as expected on all of them is based on correct table usage.
If you're interested in learning more about how to do it correctly, read this article.
If you're just looking for a way to make it work, you may assist one of those email template builders:
https://stripo.email/
https://beefree.io/
Number 1 did the trick for me.
Answer to first question: Yes you can fix it but it is tedious and alot of work (or use simple email templates)
Second: Bigger companies use software like Mailchimp or Selligent. They make the mailings and compatible for other devices (emails are really anoying because every client interpret html differently)
Base64 images do not work in Hotmail or Outlook.
The best way to display images in html emails is to host them on a web site and include a link to them in the email. This works on all modern email clients.
example:
<img src="https://via.placeholder.com/170" width="170" height="170" alt="alt_text" border="0">
More Information:
https://www.campaignmonitor.com/blog/email-marketing/2019/04/embedded-images-in-html-email/

API General Understanding in regards to Python Gmail-api

I just got started programming a year ago (on and off) and I am trying to save an attachment to a folder offline from my personal GMAIL account.
I was advised to use :
https://developers.google.com/gmail/api/quickstart/python
I set up the
I authenticated the my account and now am trying to get comfortable with this tool.
There are some initial questions that I have
What is User ID ?
is this my email ( tttt#xxxx.xxxx)
or someone else's email (ppppp#yyyy.yyy)
How do I get a email ID's ?
These questions stem from ...
GET https://www.googleapis.com/gmail/v1/users/userId/messages/messageId/attachments/id
from the page :
https://developers.google.com/gmail/api/v1/reference/users/messages/attachments/get
Again I am just learning from a beginner place..
Thanks
Just use "me" as the userId as it says in the doc.
To get a messageId first you have to search (list) messages, using something like:
resp = gmail.users().messages().list(userId="me", q="has:attachment subject:'foo bar' before:"2014-01-05").execute()
you can then iterate through the 'messages' in that resp and
gmail.users().messages().get(userId="me", id=message['id']).execute()
The Gmail API guides are quite helpful, take a look at them, for example:
https://developers.google.com/gmail/api/guides/filtering

How To Create a draft email?

I want to write a python that can sends data to a draft message, but NOT Send the mail directly.
Situation:
I'm a 3D animator. After done some shots, we need to send email(not internet, just in our company) to Lighting Team.
So we have some mail form, but sometimes we need to fix some words manual(version,texture..etc).
like below:
Shot Name : XXXX_seqXXX_scXXX
File Locate(hair + cloth) : please import X:\XX\XX\XX\XXX_ABC.ma
File include:
XXX_hair **(use version 1)**
XXX_cloth
ZZZ_cloth **(No texture)**
any problem please tell me.
Thanks.
Ezylryb
Question is:
Now I can write into a file, but I don't know how to open mail software(win7 Livemail) and create a new mail has these content. I'm try to use smtplib, but it will send mail directly..
Could anyone help me??
Many Thanks!!!!
Ezylryb
==============================
Finally, I write HTML code into a .html file, and a eml file with email address + Title + CC, and use os.startfile to open both files.
And I can copy/paste to draft email ^O^.
Coz we need some chinese words, so use big5 code in eml, and use qupori module to decode MIME words to Chinese words in html file.
A "draft" is a feature which your email client (in this case Win7 livemail) implements by itself. It's more or less just an email in a mailbox that the client hasn't sent yet. Mail folders like your "outbox" etc. are similar.
It's not something that Python has access to. If the mail client uses a standard mailbox format, you might be able to create the email and write it into the mailbox using mailbox module. If not, the best you can do is to write out your draft email into a file and then open that in your mail client and manually edit it.

Python read my outlook email mailbox and parse messages [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Reading e-mails from Outlook with Python through MAPI
I am completely new to Python and have been given the task to write a program that connects to my Microsoft Outlook mailbox, goes through all the emails and if the subject has a certain word, then the details of the email time and subject will be saved in variables, as well as the email message body will be parsed and relevant information will be stored in variables. Then this information will be stored in an external server/database. It also needs to be able to monitor any new emails that comes to my mailbox and repeat the drill of checking the subject line and taking appropriate action.
I have written exactly the same kind of program in C# earlier using the Interop library, but now need to do this in Python. I can figure out the nitty-gritty details by readin gthe module documentations later on, but from a high level perspective what modules should I use. I have been doing my research and some modules that have been mentioned include email, procmail and imaplib, but what do the Python veterans here recommend for the kind of project I am overtaking?
Thanks in advance for any help you might be able to provide!
At one company I worked we have a mailbox for suggestion with websites that had 'adult' material and one mailbox for spam mail that should be blocked.
Once I began working I was in 'charge' of this 'gracious' jobs.
Checking it there was something like 2000 unread mails to block and 4000 spam mails to block too.
Of course that is a function to be automatized and I looked for a good solution for me.
What I did:
[1] Used python IMAP to connect to Exchange server
[2] Used beatifulsoup (python) to parse the href values inside the email
[3] After that send a email 'thanking' the user for its collaboration (very important)
Three days after my boss thanked me for the great effort I was doing answering all the e-mails and that we got compliments. Because NOW we are answering back the customers. (not me the script)
Ok. now lets do a plan
Check the imap python module [1], and after take one tutorial using ssl imap4 [4]
Decide What is best for YOUR problem? Download the emails (pop3) or search and browse it at server (IMAP).
CHECK if you can connect using the protocols IMAP4 or POP3 Before, exchange is buggy in this part please check this bug report too [3]
Ok, you are sure you can connect using IMAP4 or POP3, now fetch one message and parse it with beatiful soup or lxml. (my case I looked for href and 'mailto:')
Do a nice message using the field 'from:' the email making it personal
PROFIT
[1] google it imap python
[2] google it BeautifulSoup python
[3] http://support.microsoft.com/kb/296387
[4] http://yuji.wordpress.com/2011/06/22/python-imaplib-imap-example-with-gmail/
Sorry but I had to give the google urls because of my low score.
I hope this answer give you some good pointers to your solution.
Of course you can make it more hax0r using lxml, sending the data to a DB.
But after you connect and start manipulating you can do anything :)

Categories

Resources