Python MIME附件赢得';不要用主题行发送

Python MIME附件赢得';不要用主题行发送,python,email,attachment,mime,Python,Email,Attachment,Mime,我在发送带有附件和主题行的电子邮件时遇到了一些代码问题 # 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, te

我在发送带有附件和主题行的电子邮件时遇到了一些代码问题

# 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消息的第一部分时显示的内容:

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
如果我一个小时接一个小时的电源,我也许能解决这个问题,但我希望能避免为这样一个琐碎的修复而做额外的工作


非常感谢您的帮助!

您也可以使用专门用于编写HTML电子邮件、在线显示图片和轻松附加文件的软件包

我所指的包是,我是开发人员/维护人员

import yagmail
yag = yagmail.SMTP('email@email.com', 'email_pwd')
file_names = ['/local/path/f.mp3', '/local/path/f.txt', '/local/path/f.avi']
yag.send('to@email.com', 'Sample subject', contents = ['This is text'] + filenames)
就这些

使用
pip安装yagmail
获取您的副本

内容可以是一个列表,您也可以在其中添加文本,您只能将
文件名
作为内容,不是吗

它读取文件,神奇地确定编码,并附加它:)


阅读github,了解其他技巧,如无密码脚本、别名等等。

您还可以使用专门用于编写HTML电子邮件、内联显示图片和轻松附加文件的软件包

我所指的包是,我是开发人员/维护人员

import yagmail
yag = yagmail.SMTP('email@email.com', 'email_pwd')
file_names = ['/local/path/f.mp3', '/local/path/f.txt', '/local/path/f.avi']
yag.send('to@email.com', 'Sample subject', contents = ['This is text'] + filenames)
就这些

使用
pip安装yagmail
获取您的副本

内容可以是一个列表,您也可以在其中添加文本,您只能将
文件名
作为内容,不是吗

它读取文件,神奇地确定编码,并附加它:)


阅读github了解其他技巧,如无密码脚本、别名等。

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

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; .......
从nobody到2015年10月29日16:17:38
日期:2015年10月29日星期四16:17:38+0000
致:
发件人:
主题:测试
内容类型:多部分/混合;
boundary=“======================06224753054669306134==”
MIME版本:1.0
--===============0622475305469306134==
内容类型:文本/纯文本。。。。。。。

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

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; .......
从nobody到2015年10月29日16:17:38
日期:2015年10月29日星期四16:17:38+0000
致:
发件人:
主题:测试
内容类型:多部分/混合;
boundary=“======================06224753054669306134==”
MIME版本:1.0
--===============0622475305469306134==
内容类型:文本/纯文本。。。。。。。

所以…我以前试过,但没用。哦,好吧,现在似乎有效。谢谢!!所以…我以前试过,但没用。哦,好吧,现在似乎有效。谢谢!!