Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我无法使用Python3发送带有excel附件的电子邮件_Python_Python 3.x_Smtplib - Fatal编程技术网

为什么我无法使用Python3发送带有excel附件的电子邮件

为什么我无法使用Python3发送带有excel附件的电子邮件,python,python-3.x,smtplib,Python,Python 3.x,Smtplib,我认为问题在于参数server=…我应该在这里放什么 import smtplib import os from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email.mime.text import MIMEText from email.utils import COMMASPACE, formatdate from email import encoders d

我认为问题在于参数
server=…
我应该在这里放什么

import smtplib
import os
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate
from email import encoders


def send_mail(send_from, send_to, subject, text, files=[], server=r'\\myserver\User\name\PythonProject\'):
  assert type(send_to)==['sendto@.com']
  assert type(files)==['File Name.xlsx']
  msg = MIMEMultipart()
  msg['From'] = "sendfrom@.com"
  msg['To'] = COMMASPACE.join(send_to)
  msg['Date'] = formatdate(localtime=True)
  msg['Subject'] = subject
  msg.attach( MIMEText(text) )
  for f in files:
    part = MIMEBase('application', "octet-stream")
    part.set_payload( open(files,"rb").read() )
    encoders.encode_base64(part)
    part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
    msg.attach(part)
  smtp = smtplib.SMTP('1.1.1.1: 25')
  smtp.sendmail(send_from, send_to, msg.as_string())
  smtp.close()

此代码有许多问题

  • 函数签名中服务器的默认值
    • 字符串以
      \'
      结尾,它转义结束引号:删除它或转义它(“r”前缀转义字符串内部的反斜杠,但终端反斜杠被解释为转义结束引号,因此在字符串外部)
    • 服务器
      未在函数中使用
  • 使用like
    files=[]
    通常是个坏主意;最好使用
    files=None
    并在尝试迭代代码之前检查代码中的值
  • assert
    语句不起作用。
    • 使用而不是
      类型
      检查对象类型
  • part.set\u有效载荷(open(files,“rb”).read()
    正在尝试打开文件列表,它只需要打开循环中的当前文件:
    open(f,…)
下面是代码的“固定”版本

def send_mail(
    send_from,
    send_to,
    subject,
    text,
    files=None,
    server=r"\\myserver\User\name\PythonProject\\",
):
    assert isinstance(send_to, list)
    assert isinstance(files, list)
    msg = MIMEMultipart()
    msg["From"] = "sendfrom@.com"
    msg["To"] = COMMASPACE.join(send_to)
    msg["Date"] = formatdate(localtime=True)
    msg["Subject"] = subject
    msg.attach(MIMEText(text))
    if files is not None:
        for f in files:
            part = MIMEBase("application", "octet-stream")
            part.set_payload(open(f, "rb").read())
            encoders.encode_base64(part)
            part.add_header(
                "Content-Disposition", 'attachment; filename="%s"' % os.path.basename(f)
            )   
        msg.attach(part)
    smtp = smtplib.SMTP('1.1.1.1:25')
    smtp.sendmail(send_from, send_to, msg.as_string())
    smtp.close()