Skip to content

Instantly share code, notes, and snippets.

@MakStashkevich
Forked from 1900/gist:3719878
Created August 31, 2022 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MakStashkevich/edea6936c34d9a5f6e11610332a0e5d8 to your computer and use it in GitHub Desktop.
Save MakStashkevich/edea6936c34d9a5f6e11610332a0e5d8 to your computer and use it in GitHub Desktop.
send email in Python via SMTPLIB
import smtplib
import sys
import email.mime.text
mail_username='gmailUser@gmail.com'
mail_password='password'
from_addr = mail_username
to_addrs=('gmailUser@gmail.com')
HOST = 'smtp.gmail.com'
PORT = 25
# Create SMTP Object
smtp = smtplib.SMTP()
print 'connecting ...'
# show the debug log
smtp.set_debuglevel(1)
# connet
try:
print smtp.connect(HOST,PORT)
except:
print 'CONNECT ERROR ****'
# gmail uses ssl
smtp.starttls()
# login with username & password
try:
print 'loginning ...'
smtp.login(mail_username,mail_password)
except:
print 'LOGIN ERROR ****'
# fill content with MIMEText's object
msg = email.mime.text.MIMEText('Hi ,All')
msg['From'] = from_addr
msg['To'] = ';'.join(to_addrs)
msg['Subject']='this is test msg'
print msg.as_string()
smtp.sendmail(from_addr,to_addrs,msg.as_string())
smtp.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment