Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29769

Python smtplib email does not send even without error

$
0
0

I am trying to send a small simple email with a message of just a string. This string will look something like 'The GB vs DEN game just ended at {some time} '.

My function is as below:

def send_email(msg,address):
        context = ssl.create_default_context()
        with smtplib.SMTP_SSL('smtp.gmail.com',port,context=context) as server:
                print('Sending Email')
                server.login(user,password)
                try:
                        server.sendmail(user,address,msg)
                        print('Email Sent!')
                except:
                        print('Message Sending Failed')

I know that this works because using it elsewhere in my program works. The only difference between the times I use it is the message.

The context that I am using it in is this.

 if played_status == 'COMPLETED' or played_status == 'FINAL' or played_status == 'POSTGAME-REVIEWING':
                    message = 'The {} vs {} game just ended at {}.'.format(away_team,home_team,time.asctime())
                    send_email(message, receiver)
                    print(message)

When this code segment executes the output is as follows:

Sending Email
Email Sent!
The GB vs DEN game just ended at Sun Dec  7 01:19:10 2019.

Based on this output, the email should have sent correct? Is there a better way I can figure out why this isn't working?

EDIT: After adding a print statement to display sendmail return code, this is my code.

def send_email(msg,address):
        context = ssl.create_default_context()
        with smtplib.SMTP_SSL('smtp.gmail.com',port,context=context) as server:
                print('Sending Email')
                print(server.login(user,password))
                try:
                        server.sendmail(user,address,msg)
                        print('Email Sent!')
                except:
                        print('Message Sending Failed')

And this is the return value printed. According to what I've found online that means it was successful?

The WAS vs GB game just ended at Sun Dec  8 12:59:23 2019.
Sending Email
(235, b'2.7.0 Accepted')
Email Sent!

Viewing all articles
Browse latest Browse all 29769

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>