mail.py 3.6 KB

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