Python Forum
Yahoo email "search and destroy"!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Yahoo email "search and destroy"!
#1
Heart 
Greetings friends,

My first post, very new to programing, and I must also tell you I'm fascinated and in love with Python!! Heart

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. Shy Heart Idea

Blessings,

LL
Reply
#2
(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.
Linh_lee likes this post
Reply
#3
@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?!
Reply
#4
(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:\>
Linh_lee likes this post
Reply
#5
(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
Reply
#6
Okay, just when I thought I was past Kindergarten, I'm being dragged back in! Cry

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?
Reply
#7
  • 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
Linh_lee likes this post
Reply
#8
(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)
Reply
#9
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.
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why does [root.destroy, exit()]) fail after pyinstaller? Rpi Edward_ 4 633 Oct-18-2023, 11:09 PM
Last Post: Edward_
  Python help for yahoo finance! Zshanar 7 3,375 Jul-31-2022, 05:27 PM
Last Post: paulyan
  Real time pricing Yahoo finance nathanz 1 2,522 Aug-09-2018, 01:02 PM
Last Post: metulburr
  An email with inline jpg cannot be read by all email clients fpiraneo 4 3,994 Feb-25-2018, 07:17 PM
Last Post: fpiraneo
  Email - Send email using Windows Live Mail cyberzen 2 5,932 Apr-13-2017, 03:14 AM
Last Post: cyberzen
  how to destroy or remove object rendered with opengl from the screen? hsunteik 1 7,063 Apr-09-2017, 01:30 PM
Last Post: hsunteik

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020