Skip to content

Instantly share code, notes, and snippets.

@haotian-liu
Created October 19, 2018 04:12
Show Gist options
  • Save haotian-liu/b415003319a9af75b34fbf1c3317900d to your computer and use it in GitHub Desktop.
Save haotian-liu/b415003319a9af75b34fbf1c3317900d to your computer and use it in GitHub Desktop.
Send Email with SMTP using Python
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
mail_host = "smpt.xxxx.com"
mail_user = "[email protected]"
mail_pass = "password"
mail_port = PORT_NUMBER
receiver = '[email protected]'
def mail():
ret = True
try:
msg = MIMEText('Instance has been terminated...', 'plain', 'utf-8')
msg['From'] = formataddr(["Notification Bot", mail_user])
msg['To'] = formataddr(["FK", receiver])
msg['Subject'] = "Terminated Instance Notification"
server=smtplib.SMTP_SSL(mail_host, mail_port)
server.login(mail_user, mail_pass)
server.sendmail(mail_user, [receiver,], msg.as_string())
server.quit()
except Exception:
ret = False
return ret
ret = mail()
if ret:
print("Success!")
else:
print("Failed!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment