TypeError: 'utf8' is an invalid keyword argument for Compat32 smtplib email error message

TypeError: 'utf8' is an invalid keyword argument for Compat32 smtplib email error message

我以前可以使用下面的脚本发送个性化电子邮件,但突然出现以下错误。

我正在尝试从文件中读取姓名和电子邮件 ID,并向每个人发送一封个性化的电子邮件。它从主题列表中随机选择一个主题并使用随机延迟。到现在为止都很好,但现在不行了。

*Traceback (most recent call last):
  File "C:/myprojects/normal_email.py", line 64, in <module>
    main()
  File "C:/myprojects/normal_email.py", line 57, in main
    smtp.send_message(msg)
  File "C:\Python\lib\smtplib.py", line 964, in send_message
    bytesmsg, policy=msg.policy.clone(utf8=True))
  File "C:\Python\lib\email\_policybase.py", line 72, in clone
    raise TypeError(
TypeError: 'utf8' is an invalid keyword argument for Compat32*

import csv
from string import Template
import smtplib
import random
import time
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart




template_file = r'C:\Users880\Desktop\template.txt'
filename = r'C:\Users880\Downloads07.csv'


#  reads the file and returns the names, emails in a list
def get_contacts(file):
    names_list = []
    emails_list = []
    with open(file, 'r') as mail_data:
        data = csv.reader(mail_data)
        for details in data:
            names_list.append(details[0])
            emails_list.append(details[1])
    return names_list, emails_list


# reads the template from a text file and returns
def get_template(file):
    with open(file, 'r') as template_info:
        template_data = template_info.read()
    return Template(template_data)


def main():
    email_user = 'myemail@mail.com'
    password = 'mypassword'

    subs = ['Hi', 'Hello']

    names, emails = get_contacts(filename)
    template = get_template(template_file)

    with smtplib.SMTP('smtp.office365.com', '587') as smtp:
        smtp.ehlo()
        smtp.starttls()
        smtp.ehlo()
        smtp.login(email_user, password)
        for name, email in zip(names, emails):
            subject = random.choice(subs)
            number = random.randint(10, 30)
            msg = MIMEMultipart()
            message = template.substitute(name=name.title())
            msg['From'] = email_user
            msg['To'] = email
            msg['Subject'] = subject
            msg.attach(MIMEText(message, 'html'))
            smtp.send_message(msg)
            print(f'sent email to {name} at {email}')
            del msg
            time.sleep(number)


if __name__ == '__main__':
    main()

发生此错误是因为“收件人”或“发件人”地址之一中至少有一个非 ascii 字符。应该可以通过将消息策略设置为 email.policy.default:

来修复它
from email import policy

...

msg = MIMEMultipart(policy=policy.default)

自 Python 3.6 以来,较新的 EmailMessage/EmailPolicy API 优于旧的 email.mime 工具。使用首选工具,代码将如下所示:

from email.message import EmailMessage
from email import policy

...

            msg = EmailMessage(policy=policy.default)
            message = template.substitute(name=name.title())
            msg['From'] = email_user
            msg['To'] = email
            msg['Subject'] = subject
            msg.set_content(message, subtype='html')
            smtp.send_message(msg)

为了绝对的 RFC 兼容性,您可以考虑将策略设置为 SMTP,以生成 \r\n 行结尾。