Skip to content

Instantly share code, notes, and snippets.

@bryanseah234
Created October 26, 2021 08:47
Show Gist options
  • Save bryanseah234/23e69d97c07913cb03dda29584b011c0 to your computer and use it in GitHub Desktop.
Save bryanseah234/23e69d97c07913cb03dda29584b011c0 to your computer and use it in GitHub Desktop.
email-client-code (code to send an email using SMTP from Gmail)
import os
import datetime
from datetime import datetime
import smtplib, ssl
from email import encoders
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
try:
with open('message.txt', 'r') as f:
message = f.read()
context = ssl.create_default_context()
server = smtplib.SMTP_SSL('smtp.gmail.com', 465, context=context)
server.ehlo()
server.login('email', 'password') #replace
msg = MIMEMultipart()
msg['From'] = "from who name" #replace
msg['To'] = "send to email" #replace
now = datetime.now()
date_time = now.strftime("%m/%d/%Y, %H:%M:%S")
subject = date_time + ' logs'
msg['Subject'] = subject
msg.attach(MIMEText(message, "plain"))
text = msg.as_string()
server.sendmail('from who email', 'send to email', text) #replace
server.quit()
except Exception as e:
print(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment