在尝试用Python发送电子邮件时解决AttributeError问题?

2024-06-06 06:27:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用库“yagmail”发送电子邮件,但收到错误“AttributeError:模块“smtplib”没有属性“SMTP\u 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()


Tags: csvnameinpyimportselftxtemail
1条回答
网友
1楼 · 发布于 2024-06-06 06:27:28

大家好,Alex,正如在comments and chat中所讨论的,之所以出现AttributeError: module 'smtplib' has no attribute 'SMTP_SSL'问题,是因为python环境中没有安装ssl模块

smtplib模块仅在_has_ssl为true时加载smtp\u SSL

if _have_ssl:

    class SMTP_SSL(SMTP):

....


    __all__.append("SMTP_SSL")

变量_has_ssl是通过尝试导入ssl模块来设置的。如果导入失败,那么\u has \u ssl将被设置为False,否则设置为true

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

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

相关问题 更多 >