2013-04-30 47 views
0

我们试图发送带有附件的电子邮件,但由于某些原因,附件不会显示给使用Outlook的用户。当通过python发送附件时不会在Outlook中显示

如果他们转发电子邮件给使用gmail的人,它在gmail中显示正常。
如果gmail用户将电子邮件转发给outlook用户,它会在outlook中显示(可能是因为gmail重新生成邮件)。

这是我们用来发送电子邮件的代码:

def send_email(headers={}, attachments=[], body={}): 
    ADDRESS_HEADERS = set(['from', 'to', 'cc', 'bcc', 'reply-to']) 
    msg = MIMEMultipart('alternative') 
    msg.preamble = "You need a MIME-aware email client to read this email.\n" 

    def add_headers(): 
     def encode_address(v): 
      (name, address) = parseaddr(v) 
      name = str(Header(unicode(name), 'utf-8')) 
      address = address.encode('ascii') 
      return formataddr((name, address)) 

     for key, value in headers.iteritems(): 
      if not isinstance(value, list): 
       value = [value] 
      if key.lower() in ADDRESS_HEADERS: 
       value = map(encode_address, value) 
      msg[key.title()] = u';'.join(value) 

    def set_body(): 
     msg.attach(MIMEText(body.get('text', ''), 'plain', _charset='utf-8')) 
     if 'html' in body: 
      msg.attach(MIMEText(body['html'], 'html', _charset='utf-8')) 

    def attach_file(attachment): 
     maintype, subtype = attachment['mimetype'].split("/", 1) 
     part = MIMEBase(maintype, subtype) 
     filename = attachment['filename'] 
     name = attachment.get('name', os.path.basename(filename)) 
     with open(filename, 'rb') as f: 
      part.set_payload(f.read()) 
     encoders.encode_base64(part) 
     part.add_header('Content-Disposition', 'attachment', filename=name) 
     msg.attach(part) 

    add_headers() 
    map(attach_file, attachments) 
    set_body() 
    composed = msg.as_string() 

    p = subprocess.Popen("sendmail -t", shell=True, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE) 
    (stdout, stderr) = p.communicate(composed) 
    if p.returncode != 0: 
     raise IOError(u'{}\n\n{}'.format(stdout, stderr).strip()) 

这是非常难找到任何相关信息,由于电子邮件实现的碎片。

我们要附加的文件是出类拔萃的MIME类型application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

我试图找到正在使用的Outlook版本的详细信息的文件。

回答

1

你可以尝试两件事。首先,我已经受够了的MIME类型设置为标准袋的位,任何类型的你真的发送更好的可靠性:

part = MIMEBase('application', "octet-stream")

其次,看是否改变连接头这样的帮助:

part.add_header('Content-Disposition', 'attachment; filename="%s"' % name) 

像这样设置标题可以让我们发送到Outlook。这是使用电子邮件版本'4.0.3'。我不知道你使用的是什么版本。如你所知,有一大堆。

+0

'part.add_header('Content-Disposition','attachment',filename = name)'应该和你写的一样。我会尝试概括MIME类型。 – 2013-05-01 17:21:10

+0

我在使用'part = MIMEBase('application','vnd.openxmlformats-officedocument.spreadsheetml.sheet')时也遇到了问题。 IMO'part = MIMEBase('application',“octet-stream”)'适用于任何类型 – jes516 2015-07-29 15:28:26

0

使用win32com.client

一旦你创建SendEmail对象,这是非常简单时,这是非常简单的。

import win32com.client 
outlook_obj = win32com.client.Dispatch ("Outlook Application") 
sendEmail_obj = outlook_obj.CreateItem(0x0) 

创建一个字符串列表,每个字符串作为完整路径要附加到当前SendEmail对象的文件(即“C:/User/SubFolder/Filename.pdf”)。

加入相关的字符串,如收件人的电子邮件地址,主题和正文像这样经过:

sendEmail_obj.To ("[email protected]") 
sendEmail_obj.Subject ("Subject Title String") 
sendEmail_obj.Body ("Dear Friend:\n \t I wanted to...") 

您的附件路径字符串的产品清单应各自代表完整的文件系统路径,你想项连接。我们将这个路径字符串列表称为attachment_pathlist。

for CurrentAttachmentPath in attachment_pathlist : 
    sendEmail_obj.Attachments.Add (CurrentAttachmentPath) 

这应该准备所有发送附件。然后,剩下的就是......

sendEmail_obj.Send() 
相关问题