0

我正在尝试使用库 'yagmail' 发送电子邮件,但我收到错误“AttributeError:模块 'smtplib' 没有属性 'SMTP_SSL'”。我过去已经成功地做到了这一点,所以我不确定代码编辑或库更新是否会导致这里出现问题。该代码接收包含名称、电子邮件地址和文件名的 csv,同时提示用户输入此文件以及电子邮件文本模板和包含附件的文件。

这个问题的其他答案是关于名为“email.py”的文件,但我的文件名为“ResidualsScript.py”,所以我认为这不是问题所在。任何帮助是极大的赞赏。

完整的错误消息如下:

Traceback (most recent call last):
  File "C:/Users/Name/Desktop/ResidualsScript.py", line 99, in <module>
    send_email()
  File "C:/Users/Name/Desktop/ResidualsScript.py", line 91, in send_email
    contents=[txt, file_list])
  File "C:\Users\Name\Anaconda3\envs\Miniconda\lib\site-packages\yagmail\sender.py", line 147, in send
    self.login()
  File "C:\Users\Name\Anaconda3\envs\Miniconda\lib\site-packages\yagmail\sender.py", line 246, in login
    self._login(self.credentials)
  File "C:\Users\Name\Anaconda3\envs\Miniconda\lib\site-packages\yagmail\sender.py", line 192, in _login
    self.smtp = self.connection(self.host, self.port, **self.kwargs)
  File "C:\Users\Name\Anaconda3\envs\Miniconda\lib\site-packages\yagmail\sender.py", line 76, in connection
    return smtplib.SMTP_SSL if self.ssl else smtplib.SMTP
AttributeError: module 'smtplib' has no attribute 'SMTP_SSL'

下面的代码

import csv
import os

import smtplib
import yagmail

import tkinter as tk
from tkinter import *
from tkinter import simpledialog
from tkinter import filedialog

root = tk.Tk()
root.withdraw()

your_email = simpledialog.askstring("Email", "Enter your Email")
your_password = simpledialog.askstring("Password", "Enter your Password", show="*")

subject_line = 'Test'

LNAMES = []
FNAMES = []
EMAILS = []
FILES = []

yag = yagmail.SMTP(your_email, your_password)

email_data = filedialog.askopenfilename(filetypes=[('.csv', '.csv')],
                                        title='Select the Email Data file')

txt_file = filedialog.askopenfilename(filetypes=[('.txt', '.txt')],
                                      title='Select the EMail Template')

dir_name = filedialog.askdirectory(title='Select Folder Containing Files')
os.chdir(dir_name)


class EmailAttachmentNotFoundException(Exception):
    pass


def send_email():

    with open(email_data) as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')
        line_count = 0

        try:
            for row in csv_reader:
                last_name = row[0]
                first_name = row[1]
                email = row[2]
                file1 = row[3]
                file2 = row[4]
                file_list = []

                if not os.path.isfile(file1):
                    raise EmailAttachmentNotFoundException('The attachment file "{}" was not found in the directory'
                                                           .format(file1))
                if not os.path.isfile(file2):
                    raise EmailAttachmentNotFoundException('The attachment file "{}" was not found in the directory'
                                                           .format(file2))

                file_list.append(file1)
                file_list.append(file2)

                LNAMES.append(last_name)
                FNAMES.append(first_name)
                EMAILS.append(email)
                FILES.append(file_list)

                line_count += 1

        except EmailAttachmentNotFoundException as a:
            print(str(a))
            input('Press Enter to exit')
            sys.exit(1)

    with open(txt_file) as f:
        email_template = f.read()

    try:
        for first_name, last_name, email, file_list in zip(FNAMES, LNAMES, EMAILS, FILES):
            txt = email_template.format(first_name=first_name,
                                        last_name=last_name)

            yag.send(to=email,
                     subject=subject_line,
                     contents=[txt, file_list])

    except smtplib.SMTPAuthenticationError:
        print('Incorrect Email or Password entered')
        input('Press Enter to exit')
        sys.exit(1)


send_email()

4

1 回答 1

2

嗨,亚历克斯,正如评论和聊天中所讨论的那样,您遇到AttributeError: module 'smtplib' has no attribute 'SMTP_SSL'问题的原因是因为您的 python 环境中没有安装 ssl 模块。

smtplib 模块仅在为 true 时才加载 smtp_SSL _has_ssl

if _have_ssl:

    class SMTP_SSL(SMTP):

....


    __all__.append("SMTP_SSL")

_has_ssl通过尝试导入ssl模块来设置变量。如果导入失败,则 _has_ssl 将设置为否则False设置为 true

try:
    import ssl
except ImportError:
    _have_ssl = False
else:
    _have_ssl = True

我建议如果您的 python 环境不是很重要,可以尝试重新安装 python 或创建一个新的 python 环境。

于 2019-12-18T21:54:59.663 回答