使用Python发送电子邮件:解析时出现意外的EOF

时间:2014-06-04 16:45:21

标签: python email outlook smtp

我是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 :(得分:0)

跳出来的第一件事就是你没有密码,除非你有一个非常奇怪的邮件服务器(或者在本地运行一个即使那样......你还需要它)在某个地方设置它。你也没有给我的电子邮件服务器端口,根据我的经验,这是一个端口。

以下是适用于gmail的代码的修改版本。当然,您必须进行一些更改以反映您的环境。这个版本在源代码中以纯文本形式存储您的密码,这是一个非常糟糕的主意,您将需要更改它。之前类似的问题(此处:Determine which port my exchange service is using for e-mail sending -> 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"