【问题标题】:Sending email with multiple attachments using smpt使用 smtp 发送带有多个附件的电子邮件
【发布时间】:2020-09-12 15:31:09
【问题描述】:

我有这个脚本可以向多个用户发送带有多个附件的电子邮件。但是,附件的文件名被设置为它们的路径。

收到的文件

终端输出

如何将它们的名称设置为实际的文件名,谢谢。

"""

import os 
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
from email.MIMEBase import MIMEBase
from email import Encoders


#Set up crap for the attachments
files = "/tmp/test/dbfiles"
filenames = [os.path.join(files, f) for f in os.listdir(files)]
#print filenames


#Set up users for email
gmail_user = "joe@email.com"
gmail_pwd = "somepasswd"
recipients = ['recipient1','recipient2']

#Create Module
def mail(to, subject, text, attach):
   msg = MIMEMultipart()
   msg['From'] = gmail_user
   msg['To'] = ", ".join(recipients)
   msg['Subject'] = subject

   msg.attach(MIMEText(text))

   #get all the attachments
   for file in filenames:
      part = MIMEBase('application', 'octet-stream')
      part.set_payload(open(file, 'rb').read())
      Encoders.encode_base64(part)
      part.add_header('Content-Disposition', 'attachment; filename="%s"' % file)
      msg.attach(part)

   mailServer = smtplib.SMTP("smtp.gmail.com", 587)
   mailServer.ehlo()
   mailServer.starttls()
   mailServer.ehlo()
   mailServer.login(gmail_user, gmail_pwd)
   mailServer.sendmail(gmail_user, to, msg.as_string())
   # Should be mailServer.quit(), but that crashes...
   mailServer.close()

#send it
mail(recipients,
   "Todays report",
   "Test email",
   filenames)

"""

【问题讨论】:

    标签: python python-3.x email smtp smtplib


    【解决方案1】:

    此链接可能有解决方案:

    Python email MIME attachment filename

    基本上,根据该帖子的解决方案是更改您的这一行:

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

    到这一行:

    part.add_header('Content-Disposition', 'attachment', filename=AFileName)
    

    这归结为最后的变化:

      #get all the attachments
      for file in filenames:
      
      part = MIMEBase('application', 'octet-stream')
      part.set_payload(open(file, 'rb').read())
      Encoders.encode_base64(part)
      
      ***part.add_header('Content-Disposition', 'attachment', filename=file)***
    
      msg.attach(part)
    

    Documentation on how to use add_header

    希望对您有所帮助! :D

    更新

    在你的 for 循环中有这个应该给你文件名:

    for file in filenames:
    
        actual_filenames = os.path.basename(file)
    
        #Your code
    
        part.add_header('Content-Disposition', 'attachment', filename=actual_filenames)
    

    【讨论】:

    • 还是一样的结果,不知道哪里出了问题。
    • 在你的 for 循环中," for file in filenames:" 你能做一个 print(file) 来向我们展示 @SaadAbdulla 的输出
    • 请在您的 cmets 或您的原始帖子中添加请求的图像i.stack.imgur.com/JED2E.png@SaadAbdulla
    • 当然,我已将其添加到我的帖子中。
    • @SaadAbdulla 我已经编辑了我的答案,请查看所做的更改,如果有帮助请告诉我。
    【解决方案2】:

    如果在同一个目录:

    import os                                  
    for f in os.listdir('.'):                                                                                               
        fn, fext = os.path.splitext(f)                                                  
    

    【讨论】:

    • 第二行没看懂,你确定是syntix吗?它将完全适合脚本的哪个位置?
    • f = 文件名:因此,如果您将脚本放在 for 循环中并发送“f”,它将发送目录中的所有文件
    猜你喜欢
    • 2012-08-11
    • 2013-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 2021-03-26
    相关资源
    最近更新 更多