簡體   English   中英

嘗試使用 Python 發送電子郵件時解決 AttributeError 問題?

[英]Solving for an AttributeError when attempting to send an email with Python?

我正在嘗試使用庫 'yagmail' 發送電子郵件,但出現錯誤“AttributeError: module 'smtplib' has no attribute '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()

嗨亞歷克斯,正如評論和聊天中所討論的那樣,你得到AttributeError: module 'smtplib' has no attribute 'SMTP_SSL'問題的原因是你的 python 環境中沒有安裝 ssl 模塊。

如果_has_ssl為真,smtplib 模塊僅加載 smtp_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 env 不重要,可以嘗試重新安裝 python 或創建一個新的 python env。

暫無
暫無

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

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