使用python的smtplib,我将来如何发送电子邮件?

时间:2017-05-31 20:02:25

标签: python email-integration smtplib

我有一些示例Python代码https://docs.python.org/2/library/email-examples.html,我可以成功发送电子邮件。

# 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参数允许延迟发送电子邮件?我自己找不到任何东西。

2 个答案:

答案 0 :(得分:1)

如果您使用的是Linux,则可以使用cron job。

Smtplib只是一个库,电子邮件由您指定的提供商发送。如果您能找到支持发送延迟消息的电子邮件提供商,那么可以

如果可以,你可以运行脚本并使用time.sleep(2 * 24 * 60 * 60)但这意味着脚本必须运行2天

答案 1 :(得分:0)

很可能不是。

即使你这样做,用正确的数字代替5:

import time
time.sleep(5) # delays for 5 seconds

Python程序需要一直运行

相关问题