Skip to content

Instantly share code, notes, and snippets.

@nicc777
Created September 29, 2022 09:23
Show Gist options
  • Save nicc777/37d0f9066e6d8e51078cda45f515c1d4 to your computer and use it in GitHub Desktop.
Save nicc777/37d0f9066e6d8e51078cda45f515c1d4 to your computer and use it in GitHub Desktop.
Python raw SMTP ()unauthenticated)
#!/usr/bin/python
import smtplib
import os
SMTP_SERVER = os.getenv('SMTP_SERVER', 'localhost')
SMTP_PORT = os.getenv('SMTP_PORT', '25')
SENDER_EMAIL_ADDR = os.getenv('SENDER_EMAIL_ADDR', '[email protected]')
SENDER_NAME = os.getenv('SENDER_NAME', 'NO-REPLY')
RECEIVER_EMAIL_ADDR = os.getenv('RECEIVER_EMAIL_ADDR', 'root@localhost')
RECEIVER_NAME = os.getenv('RECEIVER_NAME', 'Root')
MESSAGE = os.getenv('MESSAGE', 'Test Message')
SUBJECT = os.getenv('SUBJECT', 'This is a test message')
sender = SENDER_EMAIL_ADDR
receivers = [RECEIVER_EMAIL_ADDR, ]
message = """From: {} <{}>
To: {} <{}>
Subject: {}
{}
""".format(
SENDER_NAME,
SENDER_EMAIL_ADDR,
RECEIVER_NAME,
RECEIVER_EMAIL_ADDR,
SUBJECT,
MESSAGE
)
try:
smtpObj = smtplib.SMTP(SMTP_SERVER, int(SMTP_PORT))
smtpObj.sendmail(sender, receivers, message)
print "Successfully sent email"
except SMTPException:
print "Error: unable to send email"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment