def sendmail(empfaenger, header, a_sendmsg, attachment=None): if A_MAIL is None or A_MAIL_PASSWORD is None: log.critical("Missing mail logins") raise Exception("LOGIN DATA INCOMPLETE") msg = MIMEMultipart() msg['Subject'] = header msg['From'] = A_MAIL msg['To'] = empfaenger msg.attach(a_sendmsg) if attachment is not None: log.info("Attachment: " + attachment) try: fp = open(attachment, "rb") att = MIMEApplication(fp.read(), _subtype=os.path.splitext(attachment)[1]) fp.close() att.add_header("Content-Disposition", "attachment", filename=os.path.basename(attachment)) msg.attach(att) except: log.critical("Attachment not found: " + attachment) raise Exception("FILE NOT FOUND") server = smtplib.SMTP('smtp.gmail.com:587') server.starttls() server.login(A_MAIL, A_MAIL_PASSWORD) server.sendmail(A_MAIL, empfaenger, msg.as_string()) log.info("Email sent to: " + empfaenger) server.quit()
def create_mail(greeting="", token="", website="", template=""): with open(template, "r", encoding="utf-8") as f: a_file_content = f.read() a_file_content = a_file_content.replace("{{%greeting%}}", greeting) a_file_content = a_file_content.replace("{{%cur_date%}}", datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) a_file_content = a_file_content.replace("{{%token%}}", token) a_file_content = a_file_content.replace("{{%website%}}", website) a_file_content = MIMEText(a_file_content.encode("utf-8"), _charset="utf-8") return a_file_content log.critical("Missing template: " + template) raise Exception("FAILED OPEN TEMPLATE")