Click here to Skip to main content
15,885,998 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I run this program with an actual gmail address and the correct password, the terminal output is:

➜  Program python3 mail.py
Something went wrong...
Something went wrong...
Something went wrong...


Here is the entire program file mail.py:

import smtplib
import csv

sender_email = '[EMAIL WILL GO HERE]'
sender_password = '[PASSWORD WILL GO HERE]'
body = 'This email was sent using Python.'

with open('emails.csv', 'r') as csv_file:
    read_file = csv.reader(csv_file)
    for row in read_file:
        if len(row[2]) > 1:
            person_email = row[2]
            try:
                server_ssl = smtplib.SMTP_SSL('smtp.gmail.com', 465)
                server_ssl.login(sender_email, sender_password)
                server_ssl.sendmail(sender_email, person_email, body)
                server_ssl.close()
                print('Emails sent!')
            except:
                print('Something went wrong...')


What I have tried:

I get the same terminal output when I run the program with an actual gmail account address and the correct password. I've tried searching Stack Overflow for answers, I've tried searching online for answers, but since I'm not getting a traceback, I'm getting the message from the except block, there is no information provided about which step is failing. How do I isolate a problem in a Python script when the terminal output is the print statement from the except block?
Posted
Updated 12-Sep-21 13:40pm

1 solution

Quote:
How do I isolate a problem in a Python script when the terminal output is the print statement from the except block?

By reading Python documentation for 'try' : 8. Errors and Exceptions — Python 3.9.7 documentation[^]
If you dare to read it until 'except', you will see how to get detail on the exception.
Python
... except Exception as inst:
...     print(type(inst))    # the exception instance
...     print(inst.args)     # arguments stored in .args
...     print(inst)          # __str__ allows args to be printed directly,
...                          # but may be overridden in exception subclasses

Also removing the 'try' will have your program crash with full error message from Python.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900