Advertisement
Guest User

Untitled

a guest
Jul 15th, 2017
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. '''
  2. Email Sender
  3. '''
  4. import smtplib
  5. import traceback
  6.  
  7. from email.mime.multipart import MIMEMultipart
  8. from email.mime.text import MIMEText
  9.  
  10.  
  11. def send_mail(smtp_user, smtp_password, mail_to, subject, message, smtp_host, smtp_port):
  12. '''
  13. Send an email based on the parameters passed
  14.  
  15. Args:
  16. smtp_user (string): the email address of the sender. Also used to login into the
  17. mail server.
  18. smtp_password (string): the email password of the sender. Only used to login into
  19. the mail server.
  20. mail_to (string): the receiver's email address
  21. subject (string): the subject of the message
  22. message (string): the message to send to the user
  23. smtp_host (string): the email providers smtp host
  24. smtp_port (int): the email providers smtp port
  25. '''
  26. msg = MIMEMultipart('alternative')
  27. msg['Subject'] = subject
  28. msg['From'] = smtp_user
  29. msg['To'] = mail_to
  30.  
  31. html = """
  32. <html>
  33. <head></head>
  34. <body>
  35. <p>Good Day Receiver,</p>
  36. <p>Sending emails this way has never been this fun :P</p>
  37. <p>{0}</p>
  38. <br>
  39. <p>Cheers,<br>
  40. <br>
  41. Mammal from Earth<br>
  42. </p>
  43. </body>
  44. </html>
  45. """.format(message)
  46.  
  47. html_message = MIMEText(html, 'html')
  48. msg.attach(html_message)
  49.  
  50. try:
  51. smtp_server = smtplib.SMTP(host=smtp_host, port=smtp_port)
  52. smtp_server.ehlo()
  53. smtp_server.starttls()
  54. smtp_server.login(user=smtp_user, password=smtp_password)
  55. smtp_server.sendmail(from_addr=smtp_user, to_addrs=mail_to, msg=msg.as_string())
  56. smtp_server.close()
  57. except smtplib.SMTPException:
  58. print(traceback.format_exc())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement