xsmtp_zl.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import smtplib
  2. from email.mime.text import MIMEText
  3. from email.header import Header
  4. from email.mime.multipart import MIMEMultipart
  5. from email.mime.application import MIMEApplication
  6. class Client(object):
  7. def __init__(self):
  8. self.username = 'xuzhongyiqiang@secdeer.com'
  9. self.password = 'YIqiang521..'
  10. self.ip = 'smtp.exmail.qq.com'
  11. self.port = 45
  12. def send_email(self, body, send_part2=None, smtp_server="smtp.exmail.qq.com"):
  13. """
  14. :param body:文件内容
  15. :param send_part2: 附件地址
  16. :param smtp_server: smtp地址
  17. :return:
  18. """
  19. # 1.发件人、授权码,收件人信息
  20. from_addr = "xuzhongyiqiang@secdeer.com"
  21. pwd = "YIqiang521.."
  22. to_addr = "1092364772@qq.com"
  23. # 2.创建实例对象,设置主题等信息
  24. msg = MIMEMultipart()
  25. msg["Subject"] = "清零安全日报"
  26. msg["From"] = from_addr
  27. msg["To"] = to_addr
  28. # 邮件内容(按每个部分)
  29. part1 = MIMEText(body, "html", 'utf-8')
  30. msg.attach(part1)
  31. print(part1)
  32. # send_part2 :附件地址
  33. if send_part2:
  34. part2 = MIMEApplication(open(send_part2, 'rb').read())
  35. part2.add_header('Content-Disposition', 'attachment', filename=send_part2)
  36. msg.attach(part2)
  37. # 3.连接smtp服务器,登录服务器并发送文本
  38. smtp_server = smtp_server
  39. server = smtplib.SMTP(smtp_server, 25)
  40. server.login(from_addr, pwd)
  41. server.sendmail(from_addr, to_addr, msg.as_string()) # as_string()把MIMEText变成一个str
  42. server.close()
  43. def send_report_mail(self, addrs=['zhangyouqian@secdeer.com'],
  44. title='爬虫报告', text='爬虫报告', addressee='尊敬的用户', addresser='泽廘安全'):
  45. addrs.append('daien@secdeer.com')
  46. # addrs.append('lijiaming@secdeer.com')
  47. message = MIMEText(text, 'plain', 'utf-8')
  48. message['Subject'] = Header(title, 'utf-8')
  49. message['From'] = Header(addresser, 'utf-8')
  50. message['To'] = Header(addressee, 'utf-8')
  51. try:
  52. server = smtplib.SMTP_SSL(self.ip, self.port)
  53. server.login(self.username, self.password)
  54. server.sendmail(from_addr=self.username, to_addrs=addrs, msg=message.as_string())
  55. print('邮件发送成功')
  56. except Exception as e:
  57. import traceback
  58. print('Error: %s' % e.__class__.__name__)
  59. print('Message: %s' % str(traceback.format_exc()))
  60. print('邮件发送失败')
  61. if __name__ == '__main__':
  62. s = Client()
  63. s.send_report_mail(addrs=['zhangyouqian@secdeer.com'])