使用python的smtplib,以后怎么发邮件?

Using python's smtplib, how can I send an email in the future?

我从这里 https://docs.python.org/2/library/email-examples.html 获得了一些示例 Python 代码,我可以在其中成功发送电子邮件。

# Import smtplib for the actual sending function
import smtplib

# Import the email modules we'll need
from email.mime.text import MIMEText

me = "my@email.com"
you = "your@email.com"
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 程序需要一直 运行