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

763 views Asked by At

I'm attempting to send an email using the library 'yagmail', but I'm getting the error "AttributeError: module 'smtplib' has no attribute 'SMTP_SSL'". I've successfully done this in the past, so I'm not sure if a code edit or a library update is causing an issue here. The code takes in a csv with a name, email address, and files names, while the user is prompted for this file as well as the email text template and the file containing the attachments.

Other answers for this question were about the file being named "email.py", but my file is named "ResidualsScript.py", so I don't think that's the issue. Any help is greatly appreciated.

The full error message is below:

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'

Code Below

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()

1

There are 1 answers

1
Chris Doyle On BEST ANSWER

Hi Alex as discussed in comments and chat the reason you get the AttributeError: module 'smtplib' has no attribute 'SMTP_SSL' issue is because you dont have the ssl module installed in your python env.

the smtplib module only loads smtp_SSL if _has_ssl is true.

if _have_ssl:

    class SMTP_SSL(SMTP):

....


    __all__.append("SMTP_SSL")

the variable _has_ssl is set by trying to import the ssl module. if the import fails then _has_ssl will be set as False other wise its set as true

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

I would suggest if your python env is not crucial maybe try to reinstall python or create a new python env.