mail.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. from dataclasses import dataclass
  2. import smtplib
  3. import ssl
  4. from email.mime.text import MIMEText
  5. from email.mime.multipart import MIMEMultipart
  6. from email.mime.application import MIMEApplication
  7. from email.utils import make_msgid
  8. import config
  9. @dataclass
  10. class Message:
  11. mailto: str
  12. subject: str
  13. html: str
  14. attachment: list
  15. class SMTP_365:
  16. def __init__(self, host='', port=0, local_hostname=None,
  17. timeout=30,
  18. source_address=None):
  19. pass
  20. def login(self, user, password, *, initial_response_ok=True):
  21. pass
  22. def sendmail(self, from_addr, to_addrs, msg, mail_options=(),
  23. rcpt_options=()):
  24. pass
  25. class mail:
  26. connected = False
  27. def __init__(self, cfg=None):
  28. self.cfg = cfg
  29. if cfg is None:
  30. self.cfg = config.config()
  31. self.mail_cfg = self.cfg.smtp
  32. self.reply_to = self.cfg.kunde.replace(' ', '-').lower() + '@global-cube.com'
  33. self.context = ssl.create_default_context()
  34. def __enter__(self):
  35. try:
  36. if self.mail_cfg.secure == 'ssl':
  37. self.mailserver = smtplib.SMTP_SSL(self.mail_cfg.server, self.mail_cfg.port, context=self.context)
  38. else:
  39. self.mailserver = smtplib.SMTP(self.mail_cfg.server, self.mail_cfg.port)
  40. # self.mailserver.set_debuglevel(1)
  41. if self.mail_cfg.secure == 'tls':
  42. # self.context.options |= ssl.OP_NO_TLSv1_2 | ssl.OP_NO_TLSv1_3
  43. self.context.minimum_version = ssl.TLSVersion["TLSv1_2"]
  44. self.mailserver.ehlo()
  45. self.mailserver.starttls(context=self.context)
  46. self.mailserver.login(self.mail_cfg.username, self.mail_cfg.password)
  47. # self.mailserver.noop()
  48. self.connected = True
  49. except smtplib.SMTPException as e:
  50. print(e)
  51. return self
  52. def __exit__(self, exc_type, exc_val, exc_tb):
  53. if self.connected:
  54. self.mailserver.quit()
  55. def send(self, mailto, subject, html, attachment=None):
  56. msg = self.message(mailto, subject, html, attachment)
  57. msg['Message-ID'] = make_msgid(domain=self.mail_cfg.server)
  58. res = {'id': msg['Message-ID'], 'mailto': mailto, 'status': 'OK'}
  59. try:
  60. result = self.mailserver.sendmail(self.mail_cfg.email, mailto, msg.as_string())
  61. with open('mail.txt', 'w') as fwh:
  62. fwh.write(msg.as_string())
  63. except smtplib.SMTPException as e:
  64. print(e)
  65. pass
  66. def message(self, mailto, subject, html, attachment):
  67. msg = MIMEMultipart('alternative')
  68. msg['From'] = self.mail_cfg.email
  69. msg['To'] = mailto
  70. # msg['Reply-To'] = self.reply_to
  71. msg['Subject'] = subject
  72. # text = '\n'.join([x for x, _ in attachment])
  73. # msg.attach(MIMEText(text, 'plain'))
  74. msg.attach(MIMEText(html, 'html'))
  75. for name, filename in attachment or []:
  76. with open(filename, "rb") as f:
  77. part = MIMEApplication(f.read(), Name=name)
  78. part['Content-Disposition'] = f'attachment; filename="{name}"'
  79. msg.attach(part)
  80. return msg
  81. if __name__ == '__main__':
  82. with mail() as m:
  83. m.send('robert.bedner@gmail.com', 'Test 123', 'kein Kommentar',
  84. [('Umsatz_Bruttoertrag_Aftersales.pdf', '/home/robert/projekte/python/Portal/Publish/daten/gaps_vers_tag/Umsatz_Bruttoertrag_Aftersales_0.pdf')])