I'm working on a script in Python that checks an IP address against a blacklist and sends an email only if the IP shows up on the list. The script will be setup to be run every 15 minutes, but I only want it to send an email if the IP is on the list and an email hasn't been sent in the last 24 hours. Current code:
import sys
import subprocess
import smtplib
import datetime
username = ''
password = ''
fromaddr = ''
toaddr = ''
server = smtplib.SMTP(host=,port=)
server.starttls()
server.ehlo()
server.esmtp_features["auth"] = "LOGIN PLAIN"
server.login(username,password)
sentFolder = server.select("SENT",readonly=TRUE)
recentSent = sentFolder["Date"]
OneDayAgo = date.today()-timedelta(days=1)
msg = ''
staticIPAddress = ''
dnsHostname = staticIPAddress + ".bl.spamcop.net"
p = subprocess.check_output("nslookup " + dnsHostname1,stderr=subprocess.STDOUT,shell=False)
if ('Non-existent' not in str(p) and recentSent < OneDayAgo):
server.sendmail(fromaddr, toaddrs, msg)
The error I run into occurs at:
sentFolder = server.select("SENT",readonly=TRUE)
The error code is:
AttributeError: 'SMTP' object has no attribute 'select'
I've tested the rest of the script (without that piece and without the recentSent < OneDayAgo pieces) and it seems to work fine.
Any help in figuring out how to make the "only send if not sent within the last 24 hours" piece work would be really appreciated.