Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python正在执行sendmail但不发送电子邮件_Python_Email_Sendmail - Fatal编程技术网

Python正在执行sendmail但不发送电子邮件

Python正在执行sendmail但不发送电子邮件,python,email,sendmail,Python,Email,Sendmail,我检查了Stackoverflow,发现了类似的问题,但似乎没有一个能回答我的具体问题。我正在使用Python 2.6 以下功能用于通过sendmail成功发送带有附件的纯文本电子邮件: def sndmsg(u,A,a): """ + sends email(s) with plain text and attachment u = str - company url A = str - manager's name a = list of email

我检查了Stackoverflow,发现了类似的问题,但似乎没有一个能回答我的具体问题。我正在使用Python 2.6

以下功能用于通过sendmail成功发送带有附件的纯文本电子邮件:

def sndmsg(u,A,a):
    """
    + sends email(s) with plain text and attachment

    u = str - company url
    A = str - manager's name
    a = list of email addresses to send to
    """
    u=u.replace('/','')
    file = open('message.txt','rb')
    txt=file.read()
    file.close()
    for t in a:
        out = MIMEMultipart()
        out['Subject'] = 'Free Report'
        out['From'] = 'admin@example.com'
        out['To'] = t.strip()
        out['Date'] = date()

        pt1 = MIMEText(txt.format(A,u))
        out.attach(pt1)

        att = MIMEApplication(open('/path/'+u+'.xls',"rb").read())
        att.add_header('Content-Disposition', 'attachment', filename=u+'.xls')
        out.attach(att)

        p = Popen(["/usr/sbin/sendmail", "-t"], stdin=PIPE)
        p.communicate(out.as_string())
然后对其进行修改,以包含html格式的文本(见下文)。代码执行得很好,没有错误地进入下一个函数,但这次我的收件箱或垃圾邮件文件夹中没有收到任何电子邮件

def sndmsg(u,A,a):
    """
    + sends email(s) with html text, plain text and attachment

    u = str - company url
    A = str - manager's name
    a = list of email addresses to send to

    H[0] = str - html table
    """
    u=u.replace('/','')
    file = open('message.txt','rb')
    txt=file.read()
    file.close()
    file = open('message.html','rb')
    htm=file.read()
    file.close()
    for t in a:
        out = MIMEMultipart()
        out['Subject'] = 'Free Report'
        out['From'] = 'admin@example.com'
        out['To'] = t.strip()
        out['Date'] = date()

        inn = MIMEMultipart('alternative')
        pt1 = MIMEText(txt.format(A,u))
        pt2 = MIMEText(htm.format(A,u,H[0]), 'html')
        inn.attach(pt1)
        inn.attach(pt2)
        out.attach(inn)

        att = MIMEApplication(open('/path/'+u+'.xls',"rb").read())
        att.add_header('Content-Disposition', 'attachment', filename=u+'.xls')
        out.attach(att)

        p = Popen(["/usr/sbin/sendmail", "-t"], stdin=PIPE)
        p.communicate(out.as_string())
我的主要问题是这是一个编码问题还是一个
Sendmail
问题

我尝试独立使用第二个功能,并在浏览器中单独打开html消息,看起来不错,但电子邮件仍然没有发送

我没有进入服务器并检查
/var/log/mail
SSH
的root访问权限。我已经问过服务器管理员,但没有得到任何帮助

如果是编码错误,需要修复或改进什么

谢谢你对我的无知的耐心和善意的帮助

  • -i
    添加到sendmail的命令行选项中

    p=Popen([“/usr/sbin/sendmail”、“-t”、“-i”],stdin=PIPE)

  • 显式关闭Popen创建的管道

    rc=p.close()
    如果rc不是None且rc>>8:
    打印“有一些错误”

  • 如果无法修复,则将
    -v
    (verbose/debug)选项添加到sendmail的命令行选项中,并在终端中执行sctipt以查看其stderr输出



  • 首先,
    H
    没有定义。谢谢你的评论。是的,H是一个全局变量,包含str列表,在其他函数中调用该列表以生成html表,其中一个H[0]被插入到该模板电子邮件中。非常感谢!另外,感谢您提供的文档。知道去哪里找真的很有帮助。我真的应该先看看那里,但我不知道该找什么。现在我知道了。再次感谢。:)