mail_sender.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # -*- coding:utf-8 -*-
  2. import smtplib
  3. from email.mime.text import MIMEText
  4. from email.mime.multipart import MIMEMultipart
  5. from email import Utils
  6. import zipfile
  7. import os.path
  8. import re
  9. import sys
  10. from taobao import mail
  11. class MailSender:
  12. def __init__(self, username, mail_authorization_code):
  13. '''
  14. :param username: 发送邮件的邮箱账号
  15. :param mail_authorization_code: 发送邮件的邮箱授权码
  16. '''
  17. self.__username = username
  18. self.__mail_authorization_code = mail_authorization_code
  19. __username = ""
  20. __mail_authorization_code = ""
  21. def send(self, mail: mail) -> bool:
  22. '''
  23. :param mail: 邮件对象
  24. :return: 是否发送成功
  25. '''
  26. if mail.text is "":
  27. return False
  28. server = smtplib.SMTP_SSL(mail.server, mail.port)
  29. server.login(self.__username, self.__mail_authorization_code) # 仅smtp服务器需要验证时
  30. # 构造MIMEMultipart对象做为邮件主体
  31. body_mail = MIMEMultipart()
  32. # 构造显示内容并添加到邮件主体
  33. if mail.mode == 0:
  34. text_mail = MIMEText(mail.text, _charset="utf-8")
  35. elif mail.mode == 1:
  36. text_mail = MIMEText(mail.file_path.decode("gbk") + "\r\n\r\n\r\n" + mail.text, _charset="utf-8")
  37. else: # 构造附件并添加到邮件主体
  38. text_mail = MIMEText(mail.text, _charset="utf-8")
  39. file_mail = MIMEText(open(mail.file_path, 'rb').read(), 'base64', 'utf-8')
  40. file_mail["Content-Type"] = 'application/x-zip-compressed'
  41. basename = os.path.basename(mail.file_path)
  42. file_mail["Content-Disposition"] = 'attachment; filename=' + basename
  43. body_mail.attach(file_mail)
  44. body_mail.attach(text_mail)
  45. # 设置邮件主体属性
  46. body_mail['From'] = mail.Special_Form
  47. body_mail['To'] = mail.Special_To
  48. body_mail['Reply-to'] = mail.Special_Reply
  49. body_mail['Subject'] = mail.subject
  50. body_mail['Date'] = Utils.formatdate()
  51. # 得到格式化后的完整文本
  52. full_text = body_mail.as_string()
  53. # 用smtp发送邮件
  54. try:
  55. server.sendmail(self.__username, mail.to_user, full_text)
  56. return True
  57. except smtplib.SMTPDataError:
  58. return False
  59. finally:
  60. server.quit()
  61. class Mail:
  62. '''
  63. 邮件类
  64. '''
  65. # 发送模式
  66. mode = 1
  67. # 邮箱个性显示
  68. Special_Form = "自动发货机器人"
  69. Special_To = "【尊贵的淘宝买家】"
  70. Special_Reply = "请旺旺联系!"
  71. # 邮箱内容及附件
  72. subject = "CSDN资源文件"
  73. text = "\r\nTrip:如果有任何问题,请旺旺上联系客服,客服将每天定时处理【问题订单】,谢谢支持!"
  74. file_path = "" # 附件地址(本地路径 | 网上路径)
  75. # 邮箱配置
  76. server = "smtp.qq.com"
  77. port = 465
  78. to_user = ""
  79. def __init__(self, to_user, file_path, mode=1):
  80. '''
  81. :param to_user: 收件人
  82. :param file_path: 文件路径
  83. :param mode: 发送模式,0=邮件提醒,1=仅发送资源地址,2=发送资源附件
  84. '''
  85. self.to_user = to_user
  86. self.file_path = file_path.encode("gbk")
  87. self.mode = mode
  88. if mode == 0:
  89. self.subject = "邮件提醒"
  90. self.Special_To = "【尊贵的主人】"
  91. self.Special_Reply = "不用回复!"
  92. self.text = "\r\n" + file_path
  93. elif mode == 1:
  94. self.subject += "-下载地址"
  95. else:
  96. # 如果不是压缩文件,则打包成zip
  97. if re.match(r"^[\s\S]+\.((?!(RAR|ZIP|TAR|ARJ|CAB|LZH|ACE|GZ|UUE|BZ2|JAR|ISO)).)+$", self.file_path.upper()):
  98. zip_path = re.sub(r"\.((?!\.).)+$", ".zip", self.file_path, 1)
  99. zip_file = zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED)
  100. zip_file.write(self.file_path)
  101. self.file_path = zip_path
  102. if __name__ == "__main__":
  103. mail_sender = MailSender("test@qq.com", "123456")
  104. mail_sender.send(Mail("test02@qq.com", "c://Robot_Download/大话Oracle_RAC:集群、高可用性、备份与恢复.pdf", 2))