How can I create a python program that can determine whether an email is a spam or not?

595 Views Asked by At

I'm new to python, and I want to create a program that can determine whether an email is a spam or not based on three factors.

The subject (if it's empty, it's spam), the sender (I only want people whose email addresses end with '.com,' for example, otherwise it's spam), and the date (I only want emails on non-weekend days, otherwise it's spam).

I did the subject part, and it works successfully.

The code is attached below. But I need help with the sender and the date part.

import pandas as pd
ExcelFile = pd.read_excel(r'C:\Users\Email Table.xlsx')
Subject = pd.DataFrame(ExcelFile, columns=['Subject'])

def spam(Subject):
    df_multiindex = ExcelFile.set_index(['Subject'])
    n = len(df_multiindex)
    
    for x in range(n):
        if ((pd.isnull(ExcelFile.loc[x, 'Subject'])) == True):
            print("Spam")
        else:
            print("not spam")

spam(Subject)
1

There are 1 best solutions below

0
AudioBubble On

You don't provide the format/type of your mail address, so this is just an idea. Check if the sender address ends with ".com":

if address.endswith(".com"):
    print("Spam")
else:
    print("not spam")

You also do not provide information on how the date is formatted. Given a unix timestamp, it'd work like this:

from datetime import datetime

ts = 1652734079
dt_object = datetime.fromtimestamp(ts)
# Check if weekday is saturday/sunday
if dt_object.weekday() in [5, 6]:
    print("Spam")
else:
    print("not spam")