使用电子邮件发送多个txt文件,并且跳过不存在的一个

时间:2019-01-24 09:16:44

标签: python

我正在尝试通过电子邮件发送多个txt文件(file,file2和file3),但是我的代码仅发送了其中一个文件,如果不存在一个文件,则脚本不起作用

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

while True:

  email = '********@gmail.com'
  password = '*******'
  send_to_email = '*****@gmail.com'
  subject = 'This is the subject'
  message = 'This is my message'
  file_location = 'E:\\New folder\\file.txt'
  file_location = 'E:\\New folder\\file2.txt'
  file_location = 'E:\\New folder\\file3.txt'

  msg = MIMEMultipart()
  msg['From'] = email
  msg['To'] = send_to_email
  msg['Subject'] = subject

  msg.attach(MIMEText(message, 'plain'))

  filename = os.path.basename(file_location)
  attachment = open(file_location, "rb")
  part = MIMEBase('application', 'octet-stream')
  part.set_payload((attachment).read())
  encoders.encode_base64(part)
  part.add_header('Content-Disposition', "attachment; filename= %s" % filename)

  msg.attach(part)

  server = smtplib.SMTP('smtp.gmail.com', 587)
  server.starttls()
  server.login(email, password)
  text = msg.as_string()
  server.sendmail(email, send_to_email, text)
  server.quit()

  time.sleep(600)

我尝试仅使用相同的代码发送一个文件,并且确实可以正常工作并且没有问题

多文件怎么了?!

谢谢

0 个答案:

没有答案