Skip to content

Instantly share code, notes, and snippets.

@vividvilla
Last active August 5, 2022 20:14
Show Gist options
  • Save vividvilla/7d6be11ea2b8364d99bea53b3929691f to your computer and use it in GitHub Desktop.
Save vividvilla/7d6be11ea2b8364d99bea53b3929691f to your computer and use it in GitHub Desktop.
Mailer - Simple Python wrapper to send email via SMTP
from smtplib import SMTP # Use this for standard SMTP protocol (port 25, no encryption)
# from smtplib import SMTP_SSL as SMTP # This invokes the secure SMTP protocol (port 465, uses SSL)
from email.mime.text import MIMEText
class Email(object):
SMTP_CONFIG = dict(
server="your_smtp_server_hostname",
username="your_smtp_server_username",
password="your_smtp_server_password"
)
# typical values for text_subtype are plain, html, xml
DEFAULT_CONTENT_TYPE = "plain"
DEFAULT_SENDER = "john.doe@example.com"
def __init__(self, SMTP_CONFIG=None, debug=False):
if not SMTP_CONFIG:
SMTP_CONFIG = self.SMTP_CONFIG
self.connection = SMTP(SMTP_CONFIG["server"])
self.connection.set_debuglevel(debug)
self.connection.login(SMTP_CONFIG["username"], SMTP_CONFIG["password"])
def send(self, subject, message, receivers, sender=None, content_type=None):
if not content_type:
content_type = self.DEFAULT_CONTENT_TYPE
if not sender:
sender = self.DEFAULT_SENDER
msg = MIMEText(message, content_type)
msg["Subject"] = subject
msg["From"] = sender
self.connection.sendmail(sender, receivers, msg.as_string())
if __name__ == '__main__':
email = Email()
email.send("Test", "Test email", ["someone.i.know@gmail.com"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment