| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- from random import random
- import configparser
- import base64
- from email.MIMEMultipart import MIMEMultipart
- from email.MIMEText import MIMEText
- from email.MIMEImage import MIMEImage
- class MailAdress:
- def __init__(self,EMail:str="",Name:str=None):
- self._name=Name
- self._mail=EMail
- @property
- def Name(self):
- if self._name==None or self._name=="":
- return self._mail
- return self._name
- @Name.setter
- def Name(self,value):
- self._name = value
- @property
- def Mail(self):
- return self._mail
- @Mail.setter
- def Mail(self,value):
- self._mail = value
- class Attachment:
- def __init__(self):
- self._size = 0
- self._fileName=""
- self._content=""
- @property
- def Size(self) -> int:
- return self._size
- @Size.setter
- def Size(self,value:int):
- self._size = value
- @property
- def FileName(self) -> str:
- return self._fileName
- @FileName.setter
- def FileName(self,value:str):
- self._fileName = value
- @property
- def Content(self) -> str:
- return self._content
- @Content.setter
- def Content(self,value:str):
- self._content = value
- class Mail:
- def __init__(self):
- self._from = MailAdress()
- self._to = MailAdress()
- self._toName=""
- self._subject=""
- self._message=""
- self._html=""
- self._attachments=list[Attachment]
- self._messageID="" #TODO (example Microsoft: Message-Id: <4b5fad31-dcb0-4112-9a75-34bafd9addaa@az.northcentralus.unknown.microsoft.com>)
- self._headers=dict[str,str]
- self._boundary = self.CreateBoundary()
- @property
- def From(self) -> MailAdress:
- return self._from
- @property
- def To(self) -> MailAdress:
- return self._to
- @property
- def Subject(self) -> str:
- return self._subject
- @Subject.setter
- def Subject(self,value:str):
- self._subject = value
- @property
- def Message(self)->str:
- return self._message
- @Message.setter
- def Message(self,value:str):
- self._message = value
- @property
- def HTML(self)->str:
- return self._html
- @HTML.setter
- def HTML(self,value:str):
- self._html = value
- @property
- def Attachments(self)->list[Attachment]:
- return self._attachments
- @property
- def MessageID(self) -> str:
- return self._messageID
- @property
- def Headers(self) -> dict[str,str]:
- return self._headers
- def Base64_Encode(self, text:str) -> str:
- return base64.b64encode(text)
- def Base64_Decode(self, text:str) -> str:
- return base64.b64decode(text)
- def Send(self, profile:str=""):
- #TODO load Template - format text
- # Read Config and Sender-Profile
- configMain = configparser.ConfigParser()
- configMainFile="config/config.ini"
- configMain.read(configMainFile)
- deviceName=configMain["global"]["devicename"]
- config = configparser.ConfigParser()
- configFile="config/email.ini"
- config.read(configFile)
- if profile=="":
- if config and "general" in config and "send" in config["general"]:
- profile=config["general"]["send"]
- if profile=="":
- print("Failed to send E-Mail. No profile found")
- return
- if profile not in config:
- print(f"Profile '{profile}' not found in {configFile}")
- return
- # Read SMTP Info
- smtpHost=config[profile]["smtp"]
- smtpUser=config[profile]["login"]
- smtpPass=config[profile]["password"]
- smtpMail=config[profile]["email"]
- if smtpHost=="" or smtpUser=="" or smtpPass=="":
- print(f"Failed to load SMTP Host / Login")
- return
- # Set From / To
- strFrom = self.From.Name+" <"+smtpMail+">"
- strTo = self.To.Name+" <"+self.To.Mail+">"
- # Prepare E-Mail
- msgRoot = MIMEMultipart('related')
- msgRoot['Subject'] = self._subject
- msgRoot['From'] = strFrom
- msgRoot['To'] = strTo
- msgRoot.preamble = 'This is a multi-part message in MIME format.'
- msgAlternative = MIMEMultipart('alternative')
- msgRoot.attach(msgAlternative)
- msgText = MIMEText(self._message)
- msgAlternative.attach(msgText)
- msgText = MIMEText(self._html, 'html')
- msgAlternative.attach(msgText)
- #TODO add images
- ## HTML <img src="cid:image1">
- ## This example assumes the image is in the current directory
- #fp = open('test.jpg', 'rb')
- #msgImage = MIMEImage(fp.read())
- #fp.close()
- ## Define the image's ID as referenced above
- #msgImage.add_header('Content-ID', '<image1>')
- #msgRoot.attach(msgImage)
- #TODO add custom headers
- # Create SMTP Connection and send E-Mail
- import smtplib
- smtp = smtplib.SMTP()
- smtp.connect(smtpHost)
- smtp.starttls() #TODO check if TLS is working - maybe config.ini option
- smtp.login(smtpUser, smtpPass)
- smtp.sendmail(strFrom, strTo, msgRoot.as_string())
- smtp.quit()
|