MIME附件不会随主题行一起发送 [英] MIME Attachments won't send with Subject Line

查看:59
本文介绍了MIME附件不会随主题行一起发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在发送带有附件和主题行的电子邮件的代码方面遇到了麻烦.

I'm having trouble with a bit of code sending an email with attachments AND a Subject line.

# Code exerpt from Oli:     http://stackoverflow.com/questions/3362600/how-to-send-email-attachments-with-python
# Emails aren't sending with a subject--need to fix this.
def send_mail(self, send_from, send_to, subject, text, files=None, server="localhost"):
    assert isinstance(send_to, list)

    msg = MIMEMultipart(
        Subject=subject,
        From=send_from,
        To=COMMASPACE.join(send_to),
        Date=formatdate(localtime=True)
    )

    msg.attach(MIMEText(text))

    for f in files or []:
        with open(f, "rb") as fil:
            msg.attach(MIMEApplication(
            fil.read(),
               Content_Disposition='attachment; filename="%s"' % basename(f),
               Name=basename(f)
            ))

    smtp = smtplib.SMTP(server)
    smtp.sendmail(send_from, send_to, msg.as_string())
    smtp.close()

此代码可以很好地发送电子邮件,但是并不能排除主题"行,并且发送的电子邮件的主题行为无主题".这是我打印MIME消息的第一部分时显示的内容:

This code sends an email fine, but it is not deliminating the 'Subject' line and the emails it sends have a subject line of "NO SUBJECT.' Here's what it shows when I print the first part of the MIME msg:

From nobody Thu Oct 29 16:17:38 2015
Content-Type: multipart/mixed; date="Thu, 29 Oct 2015 16:17:38 +0000";
to="me@email.com";
from="someserver@somewhere.com"; subject="TESTING";
boundary="===============0622475305469306134=="
MIME-Version: 1.0

--===============0622475305469306134==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

Here we go, oh! ho! ho!
--===============0622475305469306134==
Content-Type: application/octet-stream; Content-  Disposition="attachment;
filename=\"Log_Mill.py\""; Name="Log_Mill.py"
MIME-Version: 1.0
Content-Transfer-Encoding: base64

如果我不停地插电几个小时,我也许能弄清楚,但我希望避免这种琐碎的修复工作.

I might be able to figure it out if I plug away for hours and hours, but I'm hoping to avoid the extra work for such a trivial fix.

感谢您的帮助!

推荐答案

您正在将Subject等分配为多部分容器的属性,但这是不正确的.您要指定的标头应作为标头传递给msg本身,如下所示:

You are assigning the Subject etc. as attributes of the multipart container, but that's incorrect. The headers you want to specify should be passed to the msg itself as headers instead, like this:

msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = send_from
msg['To'] = COMMASPACE.join(send_to)
msg['Date'] = formatdate(localtime=True)

输出应该更像

From nobody Thu Oct 29 16:17:38 2015
Date: Thu, 29 Oct 2015 16:17:38 +0000
To: <me@email.com>
From: <someserver@somewhere.com>
Subject: TESTING
Content-Type: multipart/mixed; 
   boundary="===============0622475305469306134=="
MIME-Version: 1.0

--===============0622475305469306134==
Content-Type: text/plain; .......

这篇关于MIME附件不会随主题行一起发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆