Handle letter options

This commit is contained in:
cyril 2021-08-10 10:14:35 +02:00
commit 6b391c5222
3 changed files with 68 additions and 33 deletions

View file

@ -1,7 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>Paturlettre</title> <title>{GENEALETTER_NAME}</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" />
@ -93,7 +93,7 @@
padding: 3px 5px 0px; padding: 3px 5px 0px;
font-weight: 400; font-weight: 400;
font-size: 110%; font-size: 110%;
color: #229027; color: {GENEALETTER_MAIN_COLOR};
} }
/* ANDROID CENTER FIX */ /* ANDROID CENTER FIX */
@ -102,16 +102,16 @@
} }
</style> </style>
</head> </head>
<body style="background-color: #eef3ee; margin: 0 !important; padding: 0 !important;"> <body style="background-color: {GENEALETTER_SECOND_COLOR}; margin: 0 !important; padding: 0 !important;">
<!-- HIDDEN PREHEADER TEXT --> <!-- HIDDEN PREHEADER TEXT -->
<div style="display: none; font-size: 1px; color: #fefefe; line-height: 1px; font-family: 'Open Sans', 'OpenSans', Segoe UI, Helvetica, Arial, sans-serif; max-height: 0px; max-width: 0px; opacity: 0; overflow: hidden;"> <div style="display: none; font-size: 1px; color: #fefefe; line-height: 1px; font-family: 'Open Sans', 'OpenSans', Segoe UI, Helvetica, Arial, sans-serif; max-height: 0px; max-width: 0px; opacity: 0; overflow: hidden;">
Paturlettre {GENEALETTER_NAME}
</div> </div>
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="font-family: 'Open Sans', 'OpenSans', Segoe UI, Helvetica, Arial, sans-serif;"> <table border="0" cellpadding="0" cellspacing="0" width="100%" style="font-family: 'Open Sans', 'OpenSans', Segoe UI, Helvetica, Arial, sans-serif;">
<!-- LOGO --> <!-- LOGO -->
<tr> <tr>
<td bgcolor="#34ce3b" align="center"> <td bgcolor="{GENEALETTER_MAIN_COLOR}" align="center">
<!--[if (gte mso 9)|(IE)]> <!--[if (gte mso 9)|(IE)]>
<table align="center" border="0" cellspacing="0" cellpadding="0" width="600"> <table align="center" border="0" cellspacing="0" cellpadding="0" width="600">
<tr> <tr>
@ -133,7 +133,7 @@
</tr> </tr>
<!-- HERO --> <!-- HERO -->
<tr> <tr>
<td bgcolor="#34ce3b" align="center" style="padding: 0px 10px 0px 10px;"> <td bgcolor="{GENEALETTER_MAIN_COLOR}" align="center" style="padding: 0px 10px 0px 10px;">
<!--[if (gte mso 9)|(IE)]> <!--[if (gte mso 9)|(IE)]>
<table align="center" border="0" cellspacing="0" cellpadding="0" width="600"> <table align="center" border="0" cellspacing="0" cellpadding="0" width="600">
<tr> <tr>
@ -156,7 +156,7 @@
</tr> </tr>
<!-- COPY BLOCK --> <!-- COPY BLOCK -->
<tr> <tr>
<td bgcolor="#eef3ee" align="center" style="padding: 0px 10px 0px 10px;"> <td bgcolor="{GENEALETTER_SECOND_COLOR}" align="center" style="padding: 0px 10px 0px 10px;">
<!--[if (gte mso 9)|(IE)]> <!--[if (gte mso 9)|(IE)]>
<table align="center" border="0" cellspacing="0" cellpadding="0" width="600"> <table align="center" border="0" cellspacing="0" cellpadding="0" width="600">
<tr> <tr>
@ -201,7 +201,7 @@
<!-- FOOTER --> <!-- FOOTER -->
<tr> <tr>
<td bgcolor="#eef3ee" align="center" style="padding: 0px 10px 0px 10px;"> <td bgcolor="{GENEALETTER_SECOND_COLOR}" align="center" style="padding: 0px 10px 0px 10px;">
<!--[if (gte mso 9)|(IE)]> <!--[if (gte mso 9)|(IE)]>
<table align="center" border="0" cellspacing="0" cellpadding="0" width="600"> <table align="center" border="0" cellspacing="0" cellpadding="0" width="600">
<tr> <tr>
@ -210,11 +210,11 @@
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width: 600px;"> <table border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width: 600px;">
<!-- NAVIGATION --> <!-- NAVIGATION -->
<tr> <tr>
<td bgcolor="#eef3ee" align="center" style="padding: 30px 30px 30px 30px; color: #9ba5ae; font-size: 14px; font-weight: 400; line-height: 18px;"> <td bgcolor="{GENEALETTER_SECOND_COLOR}" align="center" style="padding: 30px 30px 30px 30px; color: #9ba5ae; font-size: 14px; font-weight: 400; line-height: 18px;">
<p style="margin: 0;"> <p style="margin: 0;">
Cet email vous est envoyé par Cyril NOVEL<br /> Cet email vous est envoyé par Cyril NOVEL<br />
parce que vous êtes inscrit sur la liste de diffusion de la Paturlettre.<br /> parce que vous êtes inscrit sur la liste de diffusion de {GENEALETTER_NAME}.<br />
Des questions ou des commentaires ? Envoyez un email à <a href="mailto:paturlettre@kosmon.fr" style="color: #34ce3b;">Paturlettre</a>. Des questions ou des commentaires ? Envoyez un email à <a href="mailto:{GENEALETTER_RESPONSE}" style="color: {GENEALETTER_MAIN_COLOR};">{GENEALETTER_NAME}</a>.
</p> </p>
</td> </td>
</tr> </tr>

View file

@ -27,6 +27,30 @@ class DataPaths:
return self._members_path return self._members_path
class LetterOptions:
def __init__(self, name, response_email, logo_url, hex_main_color, hex_second_color):
self._name = name
self._response_email = response_email
self._logo_url = logo_url
self._main_color = hex_main_color
self._second_color = hex_second_color
def name(self):
return self._name
def response_email(self):
return self._response_email
def logo_url(self):
return self._logo_url
def main_color_hex(self):
return self._main_color
def secondary_color_hex(self):
return self._second_color
class Mailer: class Mailer:
def __init__(self, server, user, password): def __init__(self, server, user, password):
self._server = server self._server = server
@ -44,24 +68,25 @@ class Mailer:
class EmailSender: class EmailSender:
def __init__(self, mailer: Mailer, paths: DataPaths, receiver: str): def __init__(self, mailer: Mailer, paths: DataPaths, receiver: str, letter: LetterOptions):
self._mailer = mailer self._mailer = mailer
self._paths = paths self._paths = paths
self._receiver = receiver self._receiver = receiver
self._letter = letter
print("Email Sender is set up") print("Email Sender is set up")
def send_paturlettre(self): def send_genealetter(self):
msg = MIMEMultipart('alternative') msg = MIMEMultipart('alternative')
msg['Subject'] = f'[Paturlettre] Semaine {get_week_number_and_span()[0]}' msg['Subject'] = f'[{self._letter.name()}] Semaine {get_week_number_and_span()[0]}'
msg['From'] = 'Paturlettre <paturlettre@kosmon.fr>' msg['From'] = f'{self._letter.name()} <{self._letter.response_email()}>'
msg['To'] = 'Diffusion Paturlettre <no_reply_paturlettre@kosmon.fr>' msg['To'] = f'Diffusion {self._letter.name()} <no_reply@kosmon.fr>'
msg.preamble = f'La Paturlettre de la semaine {get_week_number_and_span()[0]} !' msg.preamble = f'{self._letter.name()} de la semaine {get_week_number_and_span()[0]} !'
print("Msg Header is set up") print("Msg Header is set up")
events_previous, events_current, events_next = get_events(self._paths) events_previous, events_current, events_next = get_events(self._paths)
msg.attach(MIMEText(create_plain_email(events_previous, events_current, events_next), msg.attach(MIMEText(create_plain_email(events_previous, events_current, events_next, self._letter),
'plain')) 'plain'))
msg.attach(MIMEText(create_html_templated(events_previous, events_current, events_next), msg.attach(MIMEText(create_html_templated(events_previous, events_current, events_next, self._letter),
'html')) 'html'))
print("Msg body is generated") print("Msg body is generated")
@ -72,18 +97,21 @@ class EmailSender:
try: try:
print("Sending to {}".format(self._receiver)) print("Sending to {}".format(self._receiver))
conn.sendmail(msg['From'], self._receiver, msg.as_string()) conn.sendmail(msg['From'], self._receiver, msg.as_string())
print("Paturlettre has been sent!") print(f"{self._letter.name()} has been sent!")
finally: finally:
conn.quit() conn.quit()
def create_html_templated(events_previous, events_current, events_next): def create_html_templated(events_previous, events_current, events_next, letter: LetterOptions):
directory = os.path.dirname(os.path.realpath(__file__)) directory = os.path.dirname(os.path.realpath(__file__))
template_path = os.path.join(directory, '..', 'html_template', 'mail.html') template_path = os.path.join(directory, '..', 'html_template', 'mail.html')
with open(template_path, 'r', encoding='utf-8') as tmp: with open(template_path, 'r', encoding='utf-8') as tmp:
mail = tmp.read() mail = tmp.read()
mail = mail.replace('{LogoAddress}', mail = mail.replace('{LogoAddress}', letter.logo_url())
'http://xerto.free.fr/Paturlettre/LogoLettre.png') mail = mail.replace('{GENEALETTER_MAIN_COLOR}', letter.main_color_hex())
mail = mail.replace('{GENEALETTER_SECOND_COLOR}', letter.secondary_color_hex())
mail = mail.replace('{GENEALETTER_NAME}', letter.name())
mail = mail.replace('{GENEALETTER_RESPONSE}', letter.response_email())
wk_and_span = get_week_number_and_span() wk_and_span = get_week_number_and_span()
mail = mail.replace('{Main_Title}', f'Semaine {wk_and_span[0]}') mail = mail.replace('{Main_Title}', f'Semaine {wk_and_span[0]}')
mail = mail.replace('{Sub_Title}', mail = mail.replace('{Sub_Title}',
@ -97,9 +125,9 @@ def create_html_templated(events_previous, events_current, events_next):
return mail return mail
def create_plain_email(events_previous, events_current, events_next): def create_plain_email(events_previous, events_current, events_next, letter: LetterOptions):
wk_and_span = get_week_number_and_span() wk_and_span = get_week_number_and_span()
mail_plain = f'Paturlettre - Semaine {wk_and_span[0]}\n' mail_plain = f'{letter.name()} - Semaine {wk_and_span[0]}\n'
mail_plain += f'du {wk_and_span[1].day} {months[wk_and_span[1].month - 1]}' \ mail_plain += f'du {wk_and_span[1].day} {months[wk_and_span[1].month - 1]}' \
f' au {wk_and_span[2].day} {months[wk_and_span[2].month - 1]}\n' f' au {wk_and_span[2].day} {months[wk_and_span[2].month - 1]}\n'
mail_plain += '\n\n' mail_plain += '\n\n'
@ -114,8 +142,8 @@ def create_plain_email(events_previous, events_current, events_next):
mail_plain += '\n\n' mail_plain += '\n\n'
mail_plain += 'Cet email vous est envoyé par Cyril NOVEL parce que vous êtes inscrit sur la ' \ mail_plain += 'Cet email vous est envoyé par Cyril NOVEL parce que vous êtes inscrit sur la ' \
'liste de diffusion de la Paturlettre.\n' 'liste de diffusion de {}.\n'.format(letter.name())
mail_plain += 'Des questions ou des commentaires ? Envoyez un email à paturlettre@kosmon.fr\n' mail_plain += 'Des questions ou des commentaires ? Envoyez un email à {}\n'.format(letter.response_email())
return mail_plain return mail_plain
@ -211,27 +239,32 @@ def get_week_number_and_span():
return today.isocalendar()[1], monday, monday + timedelta(days=6) return today.isocalendar()[1], monday, monday + timedelta(days=6)
def load_config(config_file) -> (Mailer, DataPaths, str): def load_config(config_file) -> (LetterOptions, Mailer, DataPaths, str):
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read(config_file, encoding='utf-8') config.read(config_file, encoding='utf-8')
letter_opts = LetterOptions(config['Letter']['name'],
config['Letter']['response_email'],
config['Letter']['logo_url'],
config['Letter']['color_main'],
config['Letter']['color_second'])
mailer = Mailer(config['Mailer']['smtp_server'], mailer = Mailer(config['Mailer']['smtp_server'],
config['Mailer']['user'], config['Mailer']['user'],
config['Mailer']['password']) config['Mailer']['password'])
data_path = DataPaths(config['Data']['mariages'], data_path = DataPaths(config['Data']['mariages'],
config['Data']['members']) config['Data']['members'])
receiver = config['Destination']['receiver'] receiver = config['Destination']['receiver']
return mailer, data_path, receiver return letter_opts, mailer, data_path, receiver
def main(): def main():
parser = ArgumentParser(description='Send Paturlettre mail') parser = ArgumentParser(description='Send Genealetter mail')
parser.add_argument('-c', '--config', dest='config', parser.add_argument('-c', '--config', dest='config',
help='Config file to load') help='Config file to load')
args = parser.parse_args() args = parser.parse_args()
mailer, paths, receiver = load_config(args.config) letter, mailer, paths, receiver = load_config(args.config)
email_sender = EmailSender(mailer, paths, receiver) email_sender = EmailSender(mailer, paths, receiver, letter)
email_sender.send_paturlettre() email_sender.send_genealetter()
print("Exiting script") print("Exiting script")

View file

@ -64,6 +64,8 @@ class Event:
return self._current_date.weekday() return self._current_date.weekday()
def type_str(self): def type_str(self):
if self._type == 'A':
return 'U'
return self._type return self._type