0

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
Lacer
  • 5,668
  • 10
  • 33
  • 45
  • 1
    this should do the job: https://stackoverflow.com/questions/13115724/new-to-python-gmail-smtp-error – zoidgerd Oct 12 '21 at 15:45

2 Answers2

1

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

Frank Yellin
  • 9,127
  • 1
  • 12
  • 22
0

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

Moado
  • 1
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 12 '21 at 15:53