我正在尝试从一本书(“Hello!Python”)中学习 python。根据这本书,这段代码应该发送一封电子邮件。到目前为止没有运气。
import os
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
def send_message(message):
s = smtplib.SMTP('smtp.me.com')
s.sendmail(message['From'], message['To'], message.as_string())
s.quit()
def mail_report(to, ticker_name):
outer = MIMEMultipart()
outer['Subject'] = "Stock report for " + ticker_name
outer['From'] = "myemail@mac.com"
outer['To'] = to
# Internal text container
inner = MIMEMultipart('alternative')
text = "Here is the stock report for " + ticker_name
html = """\
<html>
<head></head>
<body>
<p>Here is teh stock report for
<b> """ + ticker_name + """ </b>
</p>
</body>
</html>
"""
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
inner.attach(part1)
inner.attach(part2)
outer.attach(inner)
filename = 'stocktracker-%s.csv' % ticker_name
csv_text = ''.join(file(filename).readlines())
csv_part = MIMEText(csv_text, 'csv')
csv_part.add_header('Content-Disposition', 'attachment', filename=filename), outer.attach(csv_part)
return outer
if __name__ == '__main__':
email = mail_report('myemail@mac.com', 'GOOG')
send_message(email)
我没有收到错误消息,但我也没有收到电子邮件。(不用说,我使用的是我的实际电子邮件,而不是'myemail@mac.com')所有建议和建议阅读都表示赞赏。