There is a blank NatMailer/__init__.py.
Here's:
NatMailer/NatMailer.py
# python -m smtpd -n -c DebuggingServer localhost:1025
class NatMailer:
def __init__(self, smtp_server="localhost", port=1025, sender_email="[email protected]", debug=0):
import logging
logging.basicConfig(filename='example.log', level=logging.DEBUG)
logging.info("Initiating NatMailer")
import smtplib, ssl
import json
import csv
import sqlite3
sql = sqlite3.connect('example.db')
self.debug = debug
if (debug):
self.smtp_server = "localhost"
self.port = 1025
self.sender_email = "[email protected]"
else:
self.smtp_server = smtp_server
self.port = port
self.sender_email = sender_email
def send_email(self, receiver_email, message_contents):
# Create a secure SSL context
context = ssl.create_default_context()
logging.info("Sending new email")
# Try to log in to server and send email
try:
server = smtplib.SMTP(self.smtp_server,self.port)
server.ehlo() # Can be omitted
if (not self.debug):
logging.info("Logging into " + self.sender_email)
server.starttls(context=context) # Secure the connection
server.ehlo() # Can be omitted
server.login(self.sender_email, self.password)
logging.info("Sending email to " + receiver_email)
server.sendmail(self.sender_email, receiver_email, message_contents)
except Exception as e:
# Print any error messages to stdout
logging.debug(e)
finally:
server.quit()
Then there is a debug_driver.py outside of NatMailer/.
import NatMailer
debug = 1
nm = NatMailer.NatMailer(debug=debug)
message = """\
Subject: Hi there
This message is sent from Python."""
nm.send_email('[email protected]', message)
I get this error:
Traceback (most recent call last):
File "C:/Users/pat/PycharmProjects/NatMailer/debug_driver.py", line 3, in <module>
nm = NatMailer.NatMailer(debug=debug)
AttributeError: module 'NatMailer' has no attribute 'NatMailer'
Process finished with exit code 1
What am I doing wrong? I want to be able to import a custom class into my debug_driver.py script.
natmailer.natmailerthat contains a classNatMailer. Also, consider whether you really need a module with the same name as its containing package; you can put the classNatMailerdirectly innatmailer/__init__.pyto do away with the intermediate module.