Skip to content

Instantly share code, notes, and snippets.

@fgsoap
Last active November 5, 2024 14:09
Show Gist options
  • Select an option

  • Save fgsoap/b74c3b436e5c4af6f1bc to your computer and use it in GitHub Desktop.

Select an option

Save fgsoap/b74c3b436e5c4af6f1bc to your computer and use it in GitHub Desktop.
Auto Reply Emails By Python
#-*- coding:UTF-8 -*-
__author__ = nodejx
import imaplib, email, smtplib, time
server = "mail.test.com"
fadd = "[email protected]"
mbody = ["""This is an example!"""]
while True:
i = imaplib.IMAP4(server, 143)
i.login("admin", "passwd")
i.select("INBOX")
types, datas = i.search(None, "NEW")
ids = datas[0]
if ids == "":
i.logout()
pass
else:
for n in ids.split():
typs, dats = i.fetch(n, '(RFC822)')
for dat in dats:
if isinstance(dat, tuple):
rmsg = email.message_from_string(dat[1])
msub = rmsg['subject']
mfro = rmsg['from'].replace("<", '').replace(">",'').split()[-1]
if (mfro == "[email protected]") or (mfro == "[email protected]"):
pass
else:
mhead = ['From:%s' % fadd, 'To:%s' % mfrom ,'Subject:%s' % msub]
smsg = "\r\n\r\n".join(['\r\n'.join(mhead), '\r\n'.join(mbody)])
s = smtplib.SMTP()
s.connect(server)
s.starttls()
s.login("admin", "passwd")
s.sendmail(fadd, mfro, smsg)
print "Send to %s success!" % mfro
s.quit()
time.sleep(120)
@tharunkumargoka678
Copy link

how to execute ???

@fgsoap
Copy link
Author

fgsoap commented Nov 3, 2020

how to execute ???

python auto_reply_mail.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment