无法使用SMTPLIB打开附件 [英] Can't open attachments using SMTPLIB

查看:49
本文介绍了无法使用SMTPLIB打开附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些是我用来发送电子邮件的邮件:

 将tkinter导入为tk从tkinter导入文件对话框从email.mime.text导入MIMEText从email.mime.multipart导入MIMEMultipart从email.mime.base导入MIMEBase导入电子邮件,smtplib,ssl从电子邮件导入编码器导入操作系统 

我制作了一个脚本,您可以在其中使用 tkfiledialog 将电子邮件添加到附件,以使用以下功能选择文件:

  def Browse_file():#选择要打开的文件全局文件列表文件列表= filedialog.askopenfilenames()files_attached_tk.set("附加的文件:" + str(len(文件列表))) 

这是脚本的一部分,用于附加和发送文件:(与"和与"使用相同的缩进)

文件列表中文件的

 :attachment_part = MIMEBase(应用程序",八位字节流")attachment_part.set_payload(open(file,"rb").read())encoders.encode_base64(attachment_part)attachment_part.add_header("Content-Disposition","attachment; filename ='%s'"%os.path.basename(file))message.attach(attachment_part)#创建服务器连接使用smtplib.SMTP_SSL("smtp.gmail.com",465,context = context)作为服务器:server.login(config.email_sender,config.email_password)server.sendmail(sender_email,reciever_email,message.as_string()) 

问题在于,文件确实可以发送,但是它们似乎被包裹在电子邮件附件的''中.它们看起来像这样:'document.pdf',使该文档不可读,例如,由于电子邮件被包裹在''中,因此在电子邮件中没有说PDF File.

我已经设法在计算机上打开文件,但是无法在手机上打开它们.如何才能从文件名中删除''?我尝试做 os.path.basename(file).strip("\'") .strip(''"),但是''仍在包裹文件名.如何删除这些?

很乐意提供更多详细信息.

解决方案

将'application/pdf'设置为mimetype可能会有所帮助-一些邮件客户端依靠mimetype来确定应打开附件的应用程序.

 #其他导入从email.mime.application导入MIMEApplication导入模仿...对于文件列表中的文件:mimetype,_ = mimetypes.guess_type(文件)如果mimetype为None,则mimetype ='application/octet-stream'_,_,子类型= mimetype.partition('/')annex_part = MIMEApplication(open(file,"rb").read(),子类型)attachment_part.add_header("Content-Disposition","attachment; filename ='%s'"%os.path.basename(file))message.attach(attachment_part)... 

These are the imports I used to send the email:

import tkinter as tk
from tkinter import filedialog
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
import email, smtplib, ssl
from email import encoders
import os

I have made a script where you can add attachments to emails using tkfiledialog to select files using this function:

def browse_file(): # Select a file to open
        global filelist

        filelist = filedialog.askopenfilenames()
        files_attached_tk.set("Files Attached: " + str(len(filelist)))

This is the part of the script that attaches and sends the file(s): (For and With are on same indentation)

for file in filelist:
            attachment_part = MIMEBase("application", "octet-stream")
            attachment_part.set_payload(open(file, "rb").read())
            encoders.encode_base64(attachment_part)
            attachment_part.add_header("Content-Disposition", "attachment; filename='%s'" % os.path.basename(file))
            message.attach(attachment_part)

# Create Server Connection
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
            server.login(config.email_sender, config.email_password)
            server.sendmail(
                sender_email, reciever_email, message.as_string()
            )

The problem is that, the files do send, but, they appear to be wrapped in ' ' in the email attachment. They look like this: 'document.pdf' which makes the document unreadable, for example, it does not say PDF File in the email because it is wrapped in ' '.

I have managed to open the files on my computer but I cannot open them on my phone. How would I be able to remove the ' ' from the file name? I have tried to do os.path.basename(file).strip("\'") or .strip("'") but the ' ' are still wrapping the filename. How can I remove these?

Happy to provide more details.

解决方案

It might help to set 'application/pdf' as the mimetype - some mail clients rely on the mimetype to work out which application should open the attachment.

# Additional imports

from email.mime.application import MIMEApplication
import mimetypes

...


for file in filelist:
    mimetype, _ = mimetypes.guess_type(file)
    mimetype = 'application/octet-stream' if mimetype is None else mimetype
    _, _, subtype = mimetype.partition('/')
    attachment_part = MIMEApplication(open(file, "rb").read(), subtype)
    attachment_part.add_header("Content-Disposition", "attachment; filename='%s'" % os.path.basename(file))
    message.attach(attachment_part)

...

这篇关于无法使用SMTPLIB打开附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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