python - send email locally through smtplib error

I'm trying to send an email using smtplib without storing smtp credentials locally. I found this article https://www.tutorialspoint.com/python/python_sending_email.htm but get an error. I've fairly new with smtplib. Any help would be appreciated.

#!/usr/bin/python

import smtplib

message = """From: From Person <from@fromdomain.com>
To: To Person <to@todomain.com>
MIME-Version: 1.0
Content-type: text/html
Subject: SMTP HTML e-mail test

This is an e-mail message to be sent in HTML format

<b>This is HTML message.</b>
<h1>This is headline.</h1>
"""

try:
   smtpObj = smtplib.SMTP('localhost')
   smtpObj.sendmail(sender, receivers, message)         
   print "Successfully sent email"
except SMTPException:
   print "Error: unable to send email"

error

NameError: name 'SMTPException' is not defined

2 answers

  • answered 2021-10-12 15:44 Frank Yellin

    SMTPException is in the module smtplib. So you either need to import that name, or use smtplib.SMTPException

  • answered 2021-10-12 15:47 Moado

    u should do that smtplib.SMTPException or do this line of the code to import it from smtplib import SMTPException

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum