Posts: 17
Threads: 4
Joined: Aug 2022
Aug-15-2022, 04:24 AM
(This post was last modified: Aug-16-2022, 07:59 PM by Linh_lee.)
Greetings friends,
My first post, very new to programing, and I must also tell you I'm fascinated and in love with Python!!
My email account has been blasted lately from a specific user, with spoofed domain names, well over 50 per day, non stop for over 9 weeks now!! Yes, I could just place them in the Spam folder, but, it makes it hard to search for possible legit emails, and I want to use this as a Python learning process.
Want to search for user@wildcard domain, move users to folder called DUMP, then delete contents of DUMP.
Below are the 3 working sections I have:
CREDIT: Sunny Solanki in coderzcolumn.com
import imaplib
import time
################# IMAP SSL ################################
start = time.time()
try:
imap_ssl = imaplib.IMAP4_SSL(host="imap.mail.yahoo.com", port=993)
except Exception as e:
print("ErrorType : {}, Error : {}".format(type(e).__name__, e))
imap_ssl = None
print("Connection Object : {}".format(imap_ssl))
print("Total Time Taken : {:,.2f} Seconds".format(time.time() - start))
############### Login to Mailbox ######################
print("Logging into mailbox...")
try:
resp_code, response = imap_ssl.login("[email protected]", "XXXXXXXXXXXXX")
except Exception as e:
print("ErrorType : {}, Error : {}".format(type(e).__name__, e))
resp_code, response = None, None
print("Response Code : {}".format(resp_code))
print("Response : {}\n".format(response[0].decode()))
############### Needed section to "search and destroy" ################
############### Logout of Mailbox ######################
print("\nLogging Out....")
try:
resp_code, response = imap_ssl.logout()
except Exception as e:
print("ErrorType : {}, Error : {}".format(type(e).__name__, e))
resp_code, response = None, None
print("Response Code : {}".format(resp_code))
print("Response : {}".format(response[0].decode())) I would sincerely appreciate your knowledge/expertise in helping me create the needed code section above!
Please break down each piece of code, so I can try and understand what it does.
Blessings,
LL
Posts: 7,091
Threads: 122
Joined: Sep 2016
(Aug-15-2022, 04:24 AM)Linh_lee Wrote: Section to search emails from specific_username@*.* from any domain, in all folders, and delete them. For easier search of specific username look at(install) imap_tools
pip install imap-tools A example i found.
from imap_tools import MailBox, A
with MailBox('imap.mail.com').login('[email protected]', 'pwd', 'INBOX') as mailbox:
# DELETE messages that contains '[email protected]' in body from INBOX folder
mailbox.delete(mailbox.fetch(A(body='[email protected]'))) Look at search-criteria.
Posts: 17
Threads: 4
Joined: Aug 2022
@ snippsat
imap-tools looks indeed to be much more flexible and robust!
imap-tools is run in Python?
imaplib was straight forward to run, but I haven't figured out how to run it/execute imap-tools from Windows?!
Posts: 7,091
Threads: 122
Joined: Sep 2016
Aug-26-2022, 07:17 PM
(This post was last modified: Aug-26-2022, 07:17 PM by snippsat.)
(Aug-26-2022, 05:48 PM)Linh_lee Wrote: imap-tools is run in Python?
imaplib was straight forward to run, but I haven't figured out how to run it/execute imap-tools from Windows?! Yes it for Python and run fine on Windows(not tested it yet).
Can do a quick example,learn about pip that what you use to install all external library's to Python.
All is done from command line,can show it from cmd i usually always use cmder(better) when on Windows .
# Test Python version
C:\>python -V
Python 3.10.5
# Test that pip works
C:\>pip -V
pip 22.2.2 from C:\python310\lib\site-packages\pip (python 3.10)
# Install imap-tools
C:\>pip install imap-tools
Collecting imap-tools
Downloading imap_tools-0.56.0-py3-none-any.whl (31 kB)
Installing collected packages: imap-tools
Successfully installed imap-tools-0.56.0
# Test that work use Doc of imap-tools
C:\>python
Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (
AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from imap_tools import MailBox, AND
# No errors then it works
>>> exit()
C:\>
Posts: 17
Threads: 4
Joined: Aug 2022
Aug-26-2022, 10:04 PM
(This post was last modified: Aug-26-2022, 10:06 PM by Linh_lee.)
(Aug-26-2022, 07:17 PM)snippsat Wrote: (Aug-26-2022, 05:48 PM)Linh_lee Wrote: imap-tools is run in Python?
imaplib was straight forward to run, but I haven't figured out how to run it/execute imap-tools from Windows?! Yes it for Python and run fine on Windows(not tested it yet).
Can do a quick example,learn about pip that what you use to install all external library's to Python.
All is done from command line,can show it from cmd i usually always use cmder(better) when on Windows .
# Test Python version
C:\>python -V
Python 3.10.5
# Test that pip works
C:\>pip -V
pip 22.2.2 from C:\python310\lib\site-packages\pip (python 3.10)
# Install imap-tools
C:\>pip install imap-tools
Collecting imap-tools
Downloading imap_tools-0.56.0-py3-none-any.whl (31 kB)
Installing collected packages: imap-tools
Successfully installed imap-tools-0.56.0
# Test that work use Doc of imap-tools
C:\>python
Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (
AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from imap_tools import MailBox, AND
# No errors then it works
>>> exit()
C:\>
Amazing and clear instructions!!!
Learned so much just from your post alone!!!
Thank you @ snippsat !!
The entire install and pip upgrade went flawlessly!!
Another thing, I had no idea cmder existed, great upgrade to the old cmd!!
Thanks so much, snippsat!!!
LL
Posts: 17
Threads: 4
Joined: Aug 2022
Okay, just when I thought I was past Kindergarten, I'm being dragged back in!
How do I execute script such as this?
From the Python Shell, line by line, or as a .py file?!
from imap_tools import MailBox, A
with MailBox('imap.mail.yahoo.com').login('[email protected]', 'pwd', 'Bulk') as mailbox:
# DELETE messages that contains 'a@*.*' in body from Bulk folder
mailbox.delete(mailbox.fetch(A(body='a@*.*'))) Doesn't Yahoo mail allows connection over SSL only, and requiring port=993, if so, how do I modify the above to connect over SSL?
Posts: 11,873
Threads: 474
Joined: Sep 2016
- save your script to DeleteEmailFiles.py
from imap_tools import MailBox, A
with MailBox("imap.mail.yahoo.com").login("[email protected]", "pwd", "Bulk") as mailbox:
# DELETE messages that contains 'a@*.*' in body from Bulk folder
mailbox.delete(mailbox.fetch(A(body="a@*.*")))
- from command line run:
python DeleteEmailFiles.py
That should do it
Posts: 17
Threads: 4
Joined: Aug 2022
(Aug-27-2022, 11:50 AM)Larz60+ Wrote: - save your script to DeleteEmailFiles.py
from imap_tools import MailBox, A
with MailBox("imap.mail.yahoo.com").login("[email protected]", "my_password", "Bulk") as mailbox:
# DELETE messages that contains 'a@*.*' in body from Bulk folder
mailbox.delete(mailbox.fetch(A(body="a@*.*")))
- from command line run:
python DeleteEmailFiles.py
That should do it
Good morning @ Larz60+
Getting error below:
Error: C:\Users\Desktop\Desktop\Python Spam Buster
λ python DeleteEmailFiles.py
Traceback (most recent call last):
File "C:\Users\Desktop\Desktop\Python Spam Buster\DeleteEmailFiles.py", line 2, in <module>
from imap_tools import MailBox, A
File "C:\Users\Desktop\Desktop\Python Spam Buster\imap_tools.py", line 2, in <module>
from imap_tools import MailBox, A
ImportError: cannot import name 'MailBox' from partially initialized module 'imap_tools' (most
likely due to a circular import) (C:\Users\Desktop\Desktop\Python Spam Buster\imap_tools.py)
Posts: 7,091
Threads: 122
Joined: Sep 2016
Aug-27-2022, 04:30 PM
(This post was last modified: Aug-27-2022, 04:30 PM by snippsat.)
Don't name your script imap_tools.py it will shadow(circular import) of the real imap_tools.
Rename imap_tools.py to eg my_tools.py .
Posts: 17
Threads: 4
Joined: Aug 2022
Aug-27-2022, 05:15 PM
(This post was last modified: Aug-27-2022, 05:15 PM by Linh_lee.)
I'm confuse, snippsat.
I named my script DeleteEmailFiles.py , not imap_tools.py
Update:
I did have a py file in the folder called imap_tools.py , changed name.
|