mail.py 3.3 KB

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