python构建电子邮件时遇到问题

python构建电子邮件时遇到问题,python,email,Python,Email,我使用email.mime库为我的客户开发了一个电子邮件系统。我为一些邮件客户工作得很好。例如,在gmail上,我可以看到html正文和附件文件,但在yahoo上,我收到一封空电子邮件。代码如下: import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.base import MIMEBase import email.ch

我使用email.mime库为我的客户开发了一个电子邮件系统。我为一些邮件客户工作得很好。例如,在gmail上,我可以看到html正文和附件文件,但在yahoo上,我收到一封空电子邮件。代码如下:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
import email.charset

from email import Encoders
def buildEmail(from_email, to_email, subject, text_msg, html_msg, attach_pdf_file):
    """
    This function build a body for text/html message and if attachment file provide
    then it will attach it too with email.
    """

    # Create message container - the correct MIME type is multipart/alternative.

    msgRoot = MIMEMultipart('alternative')
    msgRoot['Subject'] = subject

    msgRoot.preamble = 'This is a multi-part message in MIME format.'
    msgRoot.epilogue = ''


    # Record the MIME types of both parts - text/plain and text/html.
    text_part = MIMEText(text_msg, 'plain')
    msgRoot.attach(text_part)

    if html_msg is not None and html_msg != '':
        html_part = MIMEText(html_msg, 'html')
        msgRoot.attach(html_part)


    # Attach a file if provided
    if attach_file is not None and attach_file != '':
        fp = open(attach_pdf_file, 'rb')
        part = MIMEBase('application', 'pdf')
        part.set_payload( fp.read() )
        Encoders.encode_base64(part)
        part.add_header('Content-Disposition', 'attachment', filename="%s"%          os.path.basename(attach_pdf_file))
        msgRoot.attach(part)
        fp.close()


    s = smtplib.SMTP('localhost')
    s.sendmail(from_email, to_email, msgRoot.as_string())
    s.quit()

知道我在哪里犯了错误吗?

我发现这段代码存在一些问题,这些问题应该会阻止它在Python2.x上工作

1) 您没有在任何地方定义
attach\u file
,因此此行引发了一个异常:

if attach_file is not None and attach_file != '':
Traceback (most recent call last):
  File "/home/ra/Desktop/stack-overflow.py", line 50, in <module>
    "output.pdf")
  File "/home/ra/Desktop/stack-overflow.py", line 44, in buildEmail
    s = smtplib.SMTP('localhost')
  File "/usr/lib/python2.7/smtplib.py", line 249, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python2.7/smtplib.py", line 309, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/lib/python2.7/smtplib.py", line 284, in _get_socket
    return socket.create_connection((port, host), timeout)
  File "/usr/lib/python2.7/socket.py", line 571, in create_connection
    raise err
error: [Errno 111] Connection refused
你是说附上pdf文件吗?将
attach_file
更改为
attach_pdf_file
可更正该异常

2) 也许您只是在代码示例中遗漏了它,但是您从
os.path
调用了一个函数,但是您从未在脚本中的任何地方
导入os
。添加该选项可以纠正该异常

一旦这些问题得到纠正,在我的系统上使用
from
地址作为gmail地址运行脚本会引发此异常:

if attach_file is not None and attach_file != '':
Traceback (most recent call last):
  File "/home/ra/Desktop/stack-overflow.py", line 50, in <module>
    "output.pdf")
  File "/home/ra/Desktop/stack-overflow.py", line 44, in buildEmail
    s = smtplib.SMTP('localhost')
  File "/usr/lib/python2.7/smtplib.py", line 249, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python2.7/smtplib.py", line 309, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/lib/python2.7/smtplib.py", line 284, in _get_socket
    return socket.create_connection((port, host), timeout)
  File "/usr/lib/python2.7/socket.py", line 571, in create_connection
    raise err
error: [Errno 111] Connection refused
回溯(最近一次呼叫最后一次):
文件“/home/ra/Desktop/stack overflow.py”,第50行,在
“output.pdf”)
buildEmail中的第44行文件“/home/ra/Desktop/stack overflow.py”
s=smtplib.SMTP('localhost')
文件“/usr/lib/python2.7/smtplib.py”,第249行,在__
(代码,消息)=自连接(主机,端口)
文件“/usr/lib/python2.7/smtplib.py”,第309行,在connect中
self.sock=self.\u获取\u套接字(主机、端口、self.timeout)
文件“/usr/lib/python2.7/smtplib.py”,第284行,在get套接字中
返回套接字。创建_连接((端口、主机),超时)
文件“/usr/lib/python2.7/socket.py”,第571行,在create_connection中
提出错误
错误:[Errno 111]连接被拒绝
我没有在这台机器上本地配置SMTP服务器,这就解释了为什么我会出现这样的错误。在您的情况下,如果电子邮件发送正确,但仅针对某些电子邮件提供商,可能是因为电子邮件提供商意识到,即使地址是Yahoo,SMTP服务器也不是,因此拒绝了电子邮件。这只是一种预感,但它可能解释了这一点(如果不是这样的话,任何人从任何地址发送电子邮件都会更容易)