Python imaplib recover emails with \\Deleted flag and move to trash - python

I've been getting a lot of spam emails from various colleges. I wrote a simple Python program to go through my emails, and find all the ones whose sender ends in .edu.
Then, to delete it, I though I was supposed to add the \\Deleted flag to those emails using: Gmail.store(id, "+FLAGS", "\\Deleted"). This did not work, and some of them didn't even disappear from the inbox.
After more research, I found that instead I had to use Gmail.store(id, "+X-GM-LABELS", "\\Trash").
So I updated the program to use that, but now it doesn't see any of the emails that I previously added the \\Deleted flag to.
Is there any way I can reset all those emails and then trash them afterward?

They should be in your All Mail folder. Use the WebUI and search for them and trash them, or select the "[Gmail]\All Mail" folder (watch out for localization, this can change name for non-English users).

Related

Python: how to click on an opened outlook email (win32)

I need to click on an opened outlook email, a specific approve link that says "Approve request".
I opened the wanted email correctly, but I can't click on the specific link.
Here is the code:
import win32com.client
outlook=win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox=outlook.GetDefaultFolder(6)
messages = inbox.Items
messages.Sort("[ReceivedTime]", Descending=True)
for i in range(100):
message = messages.GetNext()
print(""+message.Subject, str(message.ReceivedTime))
if message.Subject == "Mail to approve request":
message.Display(False)
else:
pass
You will need to parse the MailItem.HTMLBody property, extract the relevant link, launch the browser with that link.
Also, never loop through all items in a folder, use Items.Find/FindNext or Items.Restrict with a query like "[Subject]='Mail to approve request'"
There are several ways to open a hyperlink programmatically in Python. The How to open a URL in python page explains possible scenarios. For example:
import os
os.system("start \"\" https://example.com")
Use the HTMLBody property which returns the a string representing the HTML body of the specified item. So, you may find the required URL programmatically by parsing the message body and execute it programmatically.
And, finally, to find items that correspond to your conditions use the Find/FindNext or Restrict methods of the Items class. Read more about them in the following articles:
How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)
How To: Use Restrict method to retrieve Outlook mail items from a folder
See Filtering Items Using Query Keywords for building a search criteria string properly.
Also you may find the AdvancedSearch method of the Application class helpful. The key benefits of using the AdvancedSearch method in Outlook are:
The search is performed in another thread. You don’t need to run another thread manually since the AdvancedSearch method runs it automatically in the background.
Possibility to search for any item types: mail, appointment, calendar, notes etc. in any location, i.e. beyond the scope of a certain folder. The Restrict and Find/FindNext methods can be applied to a particular Items collection (see the Items property of the Folder class in Outlook).
Full support for DASL queries (custom properties can be used for searching too). You can read more about this in the Filtering article in MSDN. To improve the search performance, Instant Search keywords can be used if Instant Search is enabled for the store (see the IsInstantSearchEnabled property of the Store class).
You can stop the search process at any moment using the Stop method of the Search class.
Read more about this method in the Advanced search in Outlook programmatically: C#, VB.NET article.

Looking for a 1-click add a web (or local) file to email solution. Not sure if it exists

I'm sorry if this is the wrong place for this, but I not exactly sure where is the right place to pose this issue.
I'm being asked to make a web page or spread sheet with a table of PDFs and add a button where each PDF can be attached to an email. As in, a one-click solution to add the actual PDF into an email, not a link to the file. I don't think this is possible since the file is hosted on the web... BUT...
If the file is hosted locally on our internal server is there a code or function that would be able to do that?
Basically, the company owner is old and doesn't want to have to search for files to add to emails. He wants to just be able to open a list (web, excel, whatever), click on the one(s) he wants, and have them add to an email. I need to be able to manage and update the PDF files on the back end and update the table.
Of course the easy thing is to have a shared folder, but it lacks organization of an easily readable table. Alternately, he want to attach the actual file, not send a link.
I can't think of a way to do this, but plenty of people are smarter than I.
And advice or help is appreciated.

Need help understanding looping a program/ nested loops in Python

Working on a program to automate some daily functions that I do using Selenium to interact with a browser.
I have the script working exactly as I want it to with one email account, however I have several I would like to automate. I would like it to be able to choose one email, complete the entirety of the code, loop back to the beginning, and continue from the beginning, but with my next account in the variable.
For example, my login data is stored like this:
emails=('email1#email.com',
'email2#email.com',
'email3#email.com',
)
myPassword=('passwordtext')
And is called during the login process in this way, just for reference:
for email in emails:
emailid=driver.find_element_by_name('email')
emailid.send_keys('email') # this is the variable that would need to change each time
password=driver.find_element_by_name('password')
password.click()
password.send_keys(myPassword)
password.send_keys(Keys.ENTER)
# then, several more work functions are completed using Selenium
Obviously, this doesn't work and leads to all of the email addresses in the variable being cycled through, and then the code continues. What I would like is for one email address to be selected, the rest of the program is executed, and then it loops back to the beginning and continues with the next email address in the variable.
I'm pretty sure the solution is a nested loop here and that this is a pretty basic question, but I can't get my head around how to set that up. Again, the rest of the program works perfectly, I'm just trying to figure out a way to loop through the program, changing only the login information, until the end of the variable. I hope this makes sense.
Everything that you want to run for each email address needs to be indented under your for email in emails: line.
There is no need for a nested loop.
for email in emails:
stuff_that_runs_once_per_email_address
stuff_that_runs_independent_of_email_address

How to access GMail (IMAP Email) from my Shell/Python script to download a zip file attached to an email and process it?

I have to process a file everyday. This file is sent to my Email once everyday. If I can get to this email once every day and download the attachment, that had be awesome. Is it even remotely possible to do such a thing?
Thanks!
Please see How can I download all emails with attachments from Gmail? for a practical example.
This is certainly possible. Check out imaplib in Python's standard library; with it doing what you want should be quite straightforward. Also, you can process zip files directly in Python using the zipfile library.
Your best bet is to create an IMAP Folder for your daily emails to be sent to and then create a filter in GMail to send those files there. Your Python script can then check ONLY that folder on some interval and assume that whatever ends up in there is the file you want.
A quick search yielded sooo many results for IMAP fetching examples in Python, I'll leave that part up to you, but I will say that libgmail looks pretty neat.

Best Python Library for Downloading and Extracting Addresses

I've just been given a project which involves the following steps
Grab an email from a POP3 address
Open an attachment from the email
Extract the To: email address
Add this to a global suppression list
I'd like to try and do this in Python even though I could it in PHP in half the time (this is because I dont know anywhere near as much Python as PHP)
My question would be.
Can anyone recommend a Python library for interacting with email in the way described above?
Many thanks in advance
Two bits from the standard library: poplib to grab the email via POP3, email to slice and dice it as you wish.

Categories