使用python的smtplib,我将来如何发送电子邮件?
问题描述:
我在这里有一些示例Python代码https://docs.python.org/2/library/email-examples.html我可以成功发送电子邮件。使用python的smtplib,我将来如何发送电子邮件?
# Import smtplib for the actual sending function
import smtplib
# Import the email modules we'll need
from email.mime.text import MIMEText
me = "[email protected]"
you = "[email protected]"
textfile = 'msg.txt'
# Open a plain text file for reading. For this example, assume that
# the text file contains only ASCII characters.
fp = open(textfile, 'rb')
# Create a text/plain message
msg = MIMEText(fp.read())
fp.close()
# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = 'The contents of %s' % textfile
msg['From'] = me
msg['To'] = you
# Send the message via our own SMTP server, but don't include the
# envelope header.
s = smtplib.SMTP('localhost')
s.sendmail(me, [you], msg.as_string())
s.quit()
我想从现在起延迟发送电子邮件两天。有smtplib允许延迟发送电子邮件的参数?我一直无法自己找到任何东西。
答
如果你在linux上,你可以使用cron作业。
Smtplib只是一个图书馆,电子邮件是由您指定的提供商发送的。如果你能找到一个支持发送延迟消息的电子邮件服务提供商,那么这是可能的
如果你可以运行脚本并使用time.sleep(2 * 24 * 60 * 60)但是这意味着脚本必须运行2天
答
最有可能不是。
即使你这样做,用正确的数字替换5:
import time
time.sleep(5) # delays for 5 seconds
Python程序将需要运行的全部时间