smtp_mail.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. @dataclass
  10. class MailConfig:
  11. server: str
  12. port: int
  13. secure: str
  14. username: str
  15. password: str
  16. email: str
  17. @dataclass
  18. class Message:
  19. mailto: str
  20. subject: str
  21. html: str
  22. attachment: list
  23. class SmtpMailer:
  24. connected = False
  25. def __init__(self, cfg=None):
  26. if cfg is None:
  27. cfg = default_cfg
  28. self.mail_cfg = cfg
  29. self.reply_to = "versand@global-cube.com"
  30. self.context = ssl.create_default_context()
  31. def __enter__(self):
  32. try:
  33. if self.mail_cfg.secure == "ssl":
  34. self.mailserver = smtplib.SMTP_SSL(self.mail_cfg.server, self.mail_cfg.port, context=self.context)
  35. else:
  36. self.mailserver = smtplib.SMTP(self.mail_cfg.server, self.mail_cfg.port)
  37. if self.mail_cfg.secure == "tls":
  38. self.context.minimum_version = ssl.TLSVersion["TLSv1_2"]
  39. self.mailserver.ehlo()
  40. self.mailserver.starttls(context=self.context)
  41. self.mailserver.login(self.mail_cfg.username, self.mail_cfg.password)
  42. self.connected = True
  43. except smtplib.SMTPException as e:
  44. print(e)
  45. return self
  46. def __exit__(self, exc_type, exc_val, exc_tb):
  47. if self.connected:
  48. self.mailserver.quit()
  49. def send(self, mailto, subject, html, attachment=None):
  50. msg = self.message(mailto, subject, html, attachment)
  51. msg["Message-ID"] = make_msgid(domain=self.mail_cfg.server)
  52. # res = {"id": msg["Message-ID"], "mailto": mailto, "status": "OK"}
  53. try:
  54. self.mailserver.sendmail(self.mail_cfg.email, mailto, msg.as_string())
  55. except smtplib.SMTPException as e:
  56. print(e)
  57. pass
  58. def message(self, mailto, subject, html, attachment):
  59. msg = MIMEMultipart("alternative")
  60. msg["From"] = self.mail_cfg.email
  61. msg["To"] = mailto
  62. msg["Subject"] = subject
  63. msg.attach(MIMEText(html, "html"))
  64. for name, filename in attachment or []:
  65. with open(filename, "rb") as f:
  66. part = MIMEApplication(f.read(), Name=name)
  67. part["Content-Disposition"] = f'attachment; filename="{name}"'
  68. msg.attach(part)
  69. return msg
  70. default_cfg = MailConfig(
  71. server="mail.global-cube.com",
  72. port=465,
  73. secure="ssl",
  74. username="versand",
  75. password="y6!avXX3tQvr",
  76. email="versand@global-cube.com",
  77. )
  78. if __name__ == "__main__":
  79. base_dir = str(Path(__file__).parent.parent)
  80. with SmtpMailer() as m:
  81. m.send(
  82. "robert.bedner@gmail.com",
  83. "Test 1234567",
  84. "kein Kommentar",
  85. [
  86. (
  87. "Umsatz_Bruttoertrag_Aftersales.pdf",
  88. base_dir + "/Portal/Publish/daten/gaps_vers_tag/Umsatz_Bruttoertrag_Aftersales_0.pdf",
  89. )
  90. ],
  91. )