133 lines
3.8 KiB
Python
Executable File
133 lines
3.8 KiB
Python
Executable File
#! /usr/bin/env python3
|
|
|
|
import smtplib, ssl
|
|
from email import utils
|
|
from email.mime.text import MIMEText
|
|
from email.mime.multipart import MIMEMultipart
|
|
import pypandoc
|
|
import yaml
|
|
import argparse
|
|
from datetime import datetime
|
|
import certifi
|
|
import os
|
|
|
|
def get_yaml(f):
|
|
'''Extracts the yamlHeader from a Markdown file'''
|
|
pointer = f.tell()
|
|
if f.readline() != '---\n':
|
|
f.seek(pointer)
|
|
return ''
|
|
readline = iter(f.readline, '')
|
|
readline = iter(readline.__next__, '---\n')
|
|
return ''.join(readline)
|
|
|
|
|
|
def checkHeader(header):
|
|
"""check the header for validity
|
|
|
|
useful, if the title was forgotten
|
|
|
|
Args:
|
|
header (dict): yamlHeader of the mdNewsletter
|
|
|
|
Returns:
|
|
bool: true if header is alright, false if an error was detected
|
|
"""
|
|
retVal = True
|
|
if not 'title' in header:
|
|
print("Header has no 'title' attribute")
|
|
retVal = False
|
|
else:
|
|
if header['title'] is False:
|
|
print("Empty title!")
|
|
retVal = False
|
|
return retVal
|
|
|
|
argParser = argparse.ArgumentParser(
|
|
description="Send an Markdown-File as eMail"
|
|
)
|
|
argParser.add_argument(
|
|
"mdFilePath",
|
|
help="Path to MarkdownFile to send"
|
|
)
|
|
argParser.add_argument(
|
|
"-E", "--EmailAddressFilePath",
|
|
default=None,
|
|
help="File with eMailAddresses, one per line, to wich to send the eMail TO"
|
|
)
|
|
argParser.add_argument(
|
|
"-e", "--toEmailAddress",
|
|
default=None,
|
|
help="address, to wich to send the eMail TO"
|
|
)
|
|
|
|
# StandardValues
|
|
receiverEmails = [
|
|
"cwsvjudo@arcor.de",
|
|
"marko.bunzel@arcor.de",
|
|
"cwsvjudo@gmail.com",
|
|
"judo.cwsv@t-online.de",
|
|
]
|
|
|
|
config = None
|
|
with open('./config.yaml', 'r') as file:
|
|
config = yaml.safe_load(file)
|
|
|
|
if __name__=="__main__":
|
|
argv = argParser.parse_args()
|
|
|
|
# Loading the EmailAdresses from a file
|
|
if argv.EmailAddressFilePath:
|
|
receiverEmails = []
|
|
with open(argv.EmailAddressFilePath) as inFile:
|
|
for line in inFile:
|
|
receiverEmails.append(line)
|
|
|
|
if argv.toEmailAddress:
|
|
receiverEmails = [argv.toEmailAddress]
|
|
|
|
# read markdownfile as header and text
|
|
mdHeader =[]
|
|
mdText = []
|
|
with open(argv.mdFilePath) as f:
|
|
mdHeader = yaml.safe_load(get_yaml(f))
|
|
|
|
if not checkHeader(mdHeader):
|
|
print("Header not valid!")
|
|
|
|
# Create the plain-text and HTML version of your message
|
|
text = pypandoc.convert_file(argv.mdFilePath, "markdown", extra_args=["--self-contained", "--resource-path=../aufgaben"])
|
|
html = pypandoc.convert_file(argv.mdFilePath, "html", extra_args=["--self-contained", "--resource-path=../aufgaben"])
|
|
|
|
# Turn these into plain/html MIMEText objects
|
|
txtMimeText = MIMEText(text, "plain")
|
|
htmlMimeText = MIMEText(html, "html")
|
|
|
|
# Create a secure SSL context
|
|
context = ssl.create_default_context()
|
|
# @todo This is a very bad hack, because the cert checking doesn't wor anymore
|
|
context.check_hostname = False
|
|
context.verify_mode = ssl.CERT_NONE
|
|
with smtplib.SMTP_SSL(config['smtp']['serverAddress'], config['smtp']['serverPort'], context=context) as server:
|
|
server.login(config['smtp']['login'], config['smtp']['password'])
|
|
for receiverEmail in receiverEmails:
|
|
# create the mail
|
|
message = MIMEMultipart("alternative")
|
|
# Setting header data
|
|
message["Subject"] = mdHeader['title']
|
|
message["From"] = config['senderEmailAddress']
|
|
message["Reply-To"] = config['senderEmailAddress']
|
|
message["Date"] = str(utils.formatdate(localtime=True))
|
|
# only set the to-header one time: setting it multiple
|
|
# times results in a multiple to-entries in the header!
|
|
# Meanig the mail has to be recreated for each to address.
|
|
# @todo Find a way to reuse the created mail for every recipent
|
|
message["To"] = receiverEmail
|
|
|
|
# Add HTML/plain-text parts to MIMEMultipart message
|
|
# The email client will try to render the last part first
|
|
message.attach(htmlMimeText)
|
|
message.attach(txtMimeText)
|
|
|
|
server.sendmail(config['senderEmailAddress'], receiverEmail, message.as_string())
|