you could use email parser to pick up on a specific part of the email such as sender, subject etc then have your code run if the email is the actual email you want to use and not spam..
this example uses gmail and requires gmail settings to allow less secure apps and allow use of pop3. it sends an email to itself, then attempts to read said email(email delay may prevent a read first time around)
Code: Select all
import poplib
from email import parser
import smtplib
frm = "your_gmail_email"
password = "your_gmail_password"
subject = "subject test"
msg = "message test"
def email_send(user, pwd, recipient, subject, body):
gmail_user = frm
gmail_pwd = password
FROM = user
TO = recipient if type(recipient) is list else [recipient]
SUBJECT = subject
TEXT = body
# Prepare actual message
message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
try:
print "trying to send email"
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
server.sendmail(FROM, TO, message)
server.close()
print 'successfully sent the mail'
except:
print "failed to send mail"
def email_check():
pop_conn = poplib.POP3_SSL('pop.gmail.com')
pop_conn.user(frm)
pop_conn.pass_(password)
# Get messages from server:
messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
# Concat message pieces:
messages = ["\n".join(mssg[1]) for mssg in messages]
# Parse message into an email object:
messages = [parser.Parser().parsestr(mssg) for mssg in messages]
for message in messages:
if message['subject'] == 'subject test':
sender = (message['From'])
sendee = sender.split("<")[-1].split(">")[0]
print sendee
print subject
payload = message.get_payload()
print payload
# do_stuff based on the email subject here
else:
print message['subject'] + " failed to meet requirement"
pop_conn.quit()
send_to = frm
email_send(frm, password, send_to, subject, msg)
email_check()