SMTP-Python

Last Updated: Aug 18, 2017

Send a mail via SMTP protocol using Python

  1. # -*- coding:utf-8 -*-
  2. import smtplib
  3. import email
  4. from email.mime.multipart import MIMEMultipart
  5. from email.mime.text import MIMEText
  6. from email.mime.image import MIMEImage
  7. from email.mime.base import MIMEBase
  8. from email.mime.application import MIMEApplication
  9. from email.header import Header
  10. # The sender address, created in the DirectMail console.
  11. username = '***'
  12. # The sender password, created in the DirectMail console.
  13. password = '***'
  14. # The reply-to address
  15. replyto = '***'
  16. # Recipient addresses or recipient address lists. Support up to 30 recipients.
  17. #rcptto = ['***', '***']
  18. rcptto = '***'
  19. # Build the alternative structure
  20. msg = MIMEMultipart('alternative')
  21. msg['Subject'] = Header('Custom mail subject'.decode('utf-8')).encode()
  22. msg['From'] = '%s <%s>' % (Header('Nickname'.decode('utf-8')).encode(), username)
  23. msg['To'] = rcptto
  24. msg['Reply-to'] = replyto
  25. msg['Message-id'] = email.utils.make_msgid()
  26. msg['Date'] = email.utils.formatdate()
  27. # Construct the alternative text/plain
  28. textplain = MIMEText('Custom TEXT part plain TEXT', _subtype='plain', _charset='UTF-8')
  29. msg.attach(textplain)
  30. # Build the alternative text/HTML
  31. texthtml = MIMEText('The custom HTML hypertext part', _subtype='html', _charset='UTF-8')
  32. msg.attach(texthtml)
  33. # Send mails
  34. try:
  35. client = smtplib.SMTP()
  36. #Python version 2.7 or later, if you need to use SSL, can create the client
  37. #client = smtplib.SMTP_SSL()
  38. #The general port for SMTP is 25 or 80.
  39. client.connect('smtpdm.aliyun.com', 25)
  40. #Open the DEBUG mode
  41. client.set_debuglevel(0)
  42. client.login(username, password)
  43. #The sender and the address must be consistent.
  44. #Note: if you want to get the DATA command return value, refer to sendmaili smtplib encapsulation methods:
  45. # Using SMTP. Mail/SMTP. RCPT/SMTP data method
  46. client.sendmail(username, rcptto, msg.as_string())
  47. client.quit()
  48. print 'Mail sent successfully!'
  49. except smtplib.SMTPConnectError, e:
  50. print 'Mail delivery fails, the connection fails:', e.smtp_code, e.smtp_error
  51. except smtplib.SMTPAuthenticationError, e:
  52. print 'Mail delivery failure, certification error:', e.smtp_code, e.smtp_error
  53. except smtplib.SMTPSenderRefused, e:
  54. print 'Send mail failed, the sender is rejected:', e.smtp_code, e.smtp_error
  55. except smtplib.SMTPRecipientsRefused, e:
  56. print 'Send mail failed, the recipient was rejected:', e.smtp_code, e.smtp_error
  57. except smtplib.SMTPDataError, e:
  58. print 'Send mail failed, data reception to refuse:', e.smtp_code, e.smtp_error
  59. except smtplib.SMTPException, e:
  60. print 'Send mail failed, ', e.message
  61. except Exception, e:
  62. print 'Send mail abnormal, ', str(e)
Thank you! We've received your feedback.