簡體   English   中英

TypeError:'utf8' 是 Compat32 smtplib email 錯誤消息的無效關鍵字參數

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

我以前可以使用下面的腳本發送個性化的電子郵件,但突然之間我收到以下錯誤。

我正在嘗試從文件中讀取名稱和 email ID,並向每個人發送個性化的 email。 它從主題列表中選擇一個隨機主題並使用隨機延遲。 到目前為止它工作得很好,但現在不行。

*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:\Users\91880\Desktop\template.txt'
filename = r'C:\Users\91880\Downloads\2307.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()

發生此錯誤是因為“to”或“from”地址之一中至少有一個非 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行結尾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM