Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. # coding: utf-8
  2. #========================================
  3. # author: Changlong.Zang
  4. # mail: zclongpop123@163.com
  5. # time: Tue Sep 10 17:56:28 2019
  6. #========================================
  7. import glob, random
  8. import smtplib
  9. from email.header import Header
  10. from email.mime.text import MIMEText
  11. from email.mime.image import MIMEImage
  12. from email.mime.multipart import MIMEMultipart
  13. #--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  14. def send_mail():
  15. '''
  16. '''
  17. mail_host='smtp.exmail.qq.com'
  18. mail_user='username@mailserver.com'
  19. mail_pass='password'
  20.  
  21. sender = 'username@mailserver.com'
  22. receivers = ['my_email@qq.com']
  23.  
  24. message = MIMEMultipart('mixed')
  25. message['From'] = Header(u'发件人Title', 'utf-8')
  26. message['To'] = Header(u'收件人Title', 'utf-8')
  27. message['Subject'] = Header(u'邮件主题','utf-8')
  28.  
  29. text_message = 'text message...'
  30. message.attach(MIMEText('{0} <p><img src="cid:image1" width="50%" height="50%"></p>'.format(text_message), 'html', 'utf-8'))
  31.  
  32. image_path = random.choice(glob.glob('/root/images/*.jpg'))
  33. with open(image_path, 'rb') as f:
  34. image = MIMEImage(f.read(), 'html')
  35. image.add_header('Content-ID','<image1>')
  36. message.attach(image)
  37.  
  38. try:
  39. smtpObj = smtplib.SMTP()
  40. smtpObj.connect(mail_host, 25)
  41. smtpObj.login(mail_user, mail_pass)
  42. smtpObj.sendmail(sender, receivers, message.as_string())
  43. print u'邮件发送成功'
  44. except smtplib.SMTPException:
  45. print u'Error: 无法发送邮件'
  46.  
  47.  
  48. if __name__ == '__main__':
  49. send_mail()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement