当我从某个电子邮件地址(例如johndoe@gmail.com
)收到新电子邮件时,我正在尝试接收文字。我得到它的工作,我可以使用我的电子邮件发送自己的文本。我正在努力弄清楚如何设置它以搜索特定的电子邮件并让它每15分钟不断检查一次。
import smtplib
smtpObj = smtplib.SMTP('smtp.gmail.com', 587)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.login('EMAIL@gmail.com', 'PASSWORD')
smtpObj.sendmail('EMAIL@gmail.com',
'phoneNumber@messaging.sprintpcs.com',
'You have received an email from john doe')
{}
smtpObj.quit()
# When receives email from johndoe@gmail.com text my phone number
# Start an infinite loop that checks email and carries out instructions.
# Wait 15 minutes before checking again
--- UPDATE 我有几个部分可以工作,但我正在努力解决如何让cronTab工作,因为我认为这更像是我试图用这个程序做的事情。我找到了一个特定的电子邮件,但我如何设置它只是通知我,自上次检查后我收到了来自特定发件人的新电子邮件,然后将其标记为已读或某事我不会一直收到通知那封邮件。我还想让它检查当前日期而不是我以前的所有电子邮件,以便它保持最新状态。
import smtplib, imaplib, pprint, imapclient, pyzmail, datetime
import time
import crontab
cron = Crontab()
starttime = time.time()
now = datetime.datetime.now()
job = cron.new(command='/usr/bin/echo')
smtpObj = smtplib.SMTP('smtp.gmail.com', 587)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.login('userName@gmail.com', 'Password')
smtpObj.sendmail('userName@gmail.com', 'phoneNum@messaging.sprintpcs.com',
'Subject: Received Email:')
{}
smtpObj.quit()
# When recieves email from johndoe@gmail.com text my phone number
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('userName@gmail.com', 'Password')
mail.list()
# Out: list of "folders" aka labels in gmail.
mail.select("inbox") # connect to inbox.
result, data = mail.search(now.day, '(FROM "userName" SUBJECT "Test")')
ids = data[0] # data is a list.
id_list = ids.split() # ids is a space separated string
latest_email_id = id_list[0] # get the latest
result, data = mail.fetch(latest_email_id, "(RFC822)") # fetch the email body (RFC822) for the given ID
raw_email = data[0][1] # here's the body, which is raw text of the whole email
# including headers and alternate payloads
pprint.pprint(raw_email)
# Start an infinite loop that checks email and carries out instructions.
# Wait 15 minutes before checking again
job.minute.every(15)