Newsletter 2022-01-09 - Jahresmarken
This commit is contained in:
111
infoZettelOrg/coronaZeit/tools/sendMail.py
Executable file
111
infoZettelOrg/coronaZeit/tools/sendMail.py
Executable file
@@ -0,0 +1,111 @@
|
||||
#! /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 json
|
||||
import argparse
|
||||
import yaml
|
||||
from datetime import datetime
|
||||
|
||||
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)
|
||||
|
||||
|
||||
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 = {
|
||||
'smtp' :
|
||||
{
|
||||
'serverAddress' : "mail.arcor.de",
|
||||
'serverPort' : 465,
|
||||
'login' : "cwsvjudo",
|
||||
'password' : "***REMOVED***"
|
||||
},
|
||||
'senderEmailAddress': "cwsvjudo@arcor.de"
|
||||
}
|
||||
|
||||
|
||||
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.load(get_yaml(f))
|
||||
|
||||
# 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()
|
||||
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())
|
||||
Reference in New Issue
Block a user