parthosutradhor

Python Mail

Apr 28th, 2020
881
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.09 KB | None | 0 0
  1. import smtplib
  2. from email.message import EmailMessage
  3. from email.mime.text import MIMEText
  4.  
  5.  
  6. #for python 2.7
  7. def send_mail(from_email, to_email, subject, message, password):
  8.     msg = MIMEText(message)
  9.     msg['From'] = from_email
  10.     msg['To'] = to_email
  11.     msg['Subject'] = subject
  12.     server = smtplib.SMTP("smtp.gmail.com", 587)
  13.     server.starttls()
  14.     server.login(from_email, password)
  15.     server.sendmail(from_email, [to_email], msg.as_string())
  16.     server.quit()
  17.  
  18. #with subject
  19. def send_mail(from_email, to_email, subject, message, password):
  20.     msg = EmailMessage()
  21.     msg['From'] = from_email
  22.     msg['To'] = to_email
  23.     msg['Subject'] = subject
  24.     msg.set_content(message)
  25.     server = smtplib.SMTP("smtp.gmail.com", 587)
  26.     server.starttls()
  27.     server.login(from_email, password)
  28.     server.send_message(msg)
  29.     server.quit()
  30.  
  31.  
  32. #without subject
  33. def send_mail(from_email, to_email, body, password):
  34.     server = smtplib.SMTP("smtp.gmail.com", 587)
  35.     server.starttls()
  36.     server.login(from_email, password)
  37.     server.sendmail(from_email, to_email, body)
  38.     server.quit()
Add Comment
Please, Sign In to add comment