在Python中发送邮件:解析时意外的EOF
我刚开始学习Python编程,正在做一个项目,想给自己发一封简单的邮件来测试一下,但总是遇到一些错误。一开始,我尝试使用Python官网上email: Examples中的第一个模板来发送一条简单的信息。我创建了一个包含一些文字的纯文本文件来读取。
这是我的代码:
# Import smtplib for the actual sending function
import smtplib
# Import the email modules we'll need
from email.mime.text import MIMEText
# Open a plain text file for reading. For this example, assume that
# the text file contains only ASCII characters.
fp = open('test.txt', 'r')
# Create a text/plain message
msg = MIMEText(fp.read())
fp.close()
# me == the sender's email address
# you == the recipient's email address
me = "myname@email.com"
you = "myname@email.com"
msg['Subject'] = 'The contents of %s' % 'test.txt'
msg['From'] = me
msg['To'] = you
# Send the message via our own SMTP server, but don't include the
# envelope header.
s = smtplib.SMTP('my Outlook account server')
s.sendmail(me, [you], msg.as_string())
s.quit()
然后我得到了这个错误:
Traceback (most recent call last) - on line 18:
msg['Subject'] = 'The contents of %s' % 'test.txt'
所以我尝试了另一种格式的代码,来自pythonRocks_1:
import smtplib
SERVER = 'mail.company.com'
FROM = 'jdoe@company.com'
TO = ['receiver1@company.com']
SUBJECT = "Test Subject SMTP"
TEXT = "If this is in the body of the email, test is a success!"
message = """From: %s\r\nTo: %s\r\nSubject: %s\r\n
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
try:
s = smtplib.SMTP(SERVER)
s.sendmail(FROM, TO, message) # this line is not correctly putting info in proper fields for Outlook 2010
s.quit()
print "Successfully sent email."
except:
import sys, traceback
tb = sys.exc_info()[2]
print "An error occurred on line " + str(tb.tb_lineno)
print "Error: unable to send email"
结果出现了这个错误:
An error occurred on line 19
Error: unable to send email
我的第19行代码是:
s = smtplib.SMTP(SERVER)
你觉得我的SMTP服务器设置不对吗?我在Outlook 2010的账户设置中找到了SMTP服务器的信息,但我不太确定还有什么其他问题。
1 个回答
0
我注意到的第一件事是,你没有设置密码,除非你有一个非常奇怪的邮件服务器(或者你在本地运行一个,即使这样……你也需要在某个地方进行设置)。另外,你没有给邮件服务器指定端口,而根据我的经验,这是必须的。
这里有一个修改过的代码版本,可以在gmail上使用。当然,你需要根据自己的环境做一些调整。这个版本把你的密码以明文形式存储在代码里,这样做其实很不安全,你应该考虑更改这一点。之前有一个类似的问题(在这里:确定我的交换服务用于发送邮件的端口 -> cakePhP CakeEmail),里面有关于交换服务器的设置、端口等信息。
import smtplib
TO = '<user>@gmail.com'
FROM = '<target>@gmail.com'
PWD = '<password>'
SERVER = "smtp.gmail.com" # gmail settings, this will come from your mail server
PORT = 587 #gmail settings, this will come from your server
SUBJECT = "Test Email"
TEXT = "This is a test"
message = """To: {TO}\r\nFrom: {FROM}\r\nSubject:{SUBJECT}\r\n{TEXT}""".format(TO=TO, FROM=FROM, SUBJECT=SUBJECT, TEXT=TEXT)
try:
# connect to the server and log in
smtpserver = smtplib.SMTP(SERVER, PORT)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(FROM, PWD)
# print our message just to ensure we have everything
print message
# send the message
smtpserver.sendmail(FROM, TO, message)
# close the server
smtpserver.close()
# let ues know it is done
print 'done!'
except:
import sys, traceback
tb = sys.exc_info()[2]
print "An error occurred on line " + str(tb.tb_lineno)
print "Error: unable to send email"