繁体   English   中英

如何使用下面显示的Python脚本获取电子邮件附件(.html,.txt)以显示在已发送的电子邮件中?

[英]How do I get email attachments (.html, .txt) to show in the sent email using Python script seen below?

我的电子邮件显示附件而不显示附件。 我希望指定一个.txt和五个.html 有没有人建议解决此问题?

我的代码如下:

import os
import smtplib
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

COMMASPACE = ', '

def email_main():
sender = 'xxxxxx@gmail.com'
gmail_password = 'xxxxxx123'
recipients = ['xxx@xxx.com', 'xxxyyy@yahoo.com']

# Create the enclosing (outer) message
outer = MIMEMultipart()
outer['Subject'] = 'VECTOR CAST AUTOMATED TEST RESULTS'
outer['To'] = COMMASPACE.join(recipients)
outer['From'] = sender
outer.preamble = 'You will not see this in a MIME-aware mail reader.\n'

# email message 
body = MIMEText('Test results attached.', 'html', 'text')  
outer.attach(body)  # add message body (text or html)

# List of attachments to email
emailfiles = ['VCAST Test Times.txt', \
         'AECP_MAIN_INTEGRATION_management_report.html', \
         'BECP_MAIN_INTEGRATION_management_report.html', \
         'SWITCH_BOARD_INTEGRATION_management_report.html', \
         'EVENT_MANAGER_INTEGRATION_management_report.html', \
         'EXTERNAL_ALARM_INTEGRATION_management_report.html']

# get the path of the folder this lives in 
attachments = [os.getcwd()]

# Add the attachments to the message
for file in attachments:
    try:
        for line in emailfiles:              
            fp = open(line , 'rb')            
            msg = MIMEBase('application', "octet-stream")
            msg.set_payload(fp.read()) 
            encoders.encode_base64(msg)
            msg.add_header('Content-Disposition', 'attachment', filename=os.path.basename(file))

            """
            file_path = os.path.join(dir_path, f)
            msg = MIMEApplication(open(file_path, "r").read(), _subtype="txt")
            msg.add_header('Content-Disposition','attachment', filename=f)
            """
            outer.attach(msg)
    except:
        print("Unable to open one of the attachments. Error: ", sys.exc_info()[0])
        raise

composed = outer.as_string()

# Send the email
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(sender, gmail_password)
text = msg.as_string()
server.sendmail(sender, recipients, composed)
server.quit()   

if __name__ == '__main__':
    email_main()

在此处输入图片说明

os.getcwd()

给您当前目录,例如C:\\Projects\\EmailTest So

[os.getcwd()]

给您一个包含一个字符串的列表( C:\\Projects\\EmailTest

所以循环

for file in attachments:

仅执行一次,其中file=C:\\Projects\\EmailTest

当最终在内部循环中进行设置时,您将使用外部循环中的file :(这是C:\\Projects\\EmailTest

filename=os.path.basename(file)

您只需从文件中提取文件夹名称,即EmailTest

我猜想您想要存储在emailfiles的文件名,在您的情况下是内循环中的line 并且由于它已经只是filanem了,因此您可以跳过os.path.basename

msg.add_header('Content-Disposition', 'attachment', filename=line)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM