Skip to content

Instantly share code, notes, and snippets.

@kavehbc
Created May 27, 2020 20:47
Show Gist options
  • Save kavehbc/57133569d33f87eac32e00d3e4bad1d0 to your computer and use it in GitHub Desktop.
Save kavehbc/57133569d33f87eac32e00d3e4bad1d0 to your computer and use it in GitHub Desktop.
Python - Sending email
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def sendEmail(name, email, filename='message.txt'):
# open a template file
with open(filename, 'r', encoding='utf-8') as template_file:
template_file_content = template_file.read()
message_template = template_file_content
# set up the SMTP server
server = smtplib.SMTP(host='your_host_address', port=your_port)
server.starttls()
server.login(MY_ADDRESS, PASSWORD)
# create a message
msg = MIMEMultipart()
# add in the actual person name or any other info to the message template
message = message_template.replace(PERSON_NAME=name)
# setup the parameters of the message
msg['From']=MY_ADDRESS
msg['To']=email
msg['Subject']="Email Subject here"
# add in the message body
msg.attach(MIMEText(message, 'plain'))
# send the message
server.send_message(msg)
del msg
# Terminate the SMTP session
server.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment