Fix sender template

This commit is contained in:
Antonio J. Delgado 2024-08-03 15:42:58 +03:00
parent f3c2d0acf6
commit d75cc3e904
2 changed files with 8 additions and 7 deletions

View file

@ -167,13 +167,15 @@ class MastodonEmailBridge:
def send_mail(self, data): def send_mail(self, data):
'''Send an email with the post composed''' '''Send an email with the post composed'''
sender = self._str_template(self.config['sender'], data)
recipient = self._str_template(self.config['recipient'], data)
msg = MIMEMultipart('alternative') msg = MIMEMultipart('alternative')
if data['in_reply_to_id']: if data['in_reply_to_id']:
msg['Subject'] = f"FediReply from {data['account']['display_name']} ({data['account']['username']})" msg['Subject'] = f"FediReply from {data['account']['display_name']} ({data['account']['username']})"
else: else:
msg['Subject'] = f"FediPost from {data['account']['display_name']} ({data['account']['username']})" msg['Subject'] = f"FediPost from {data['account']['display_name']} ({data['account']['username']})"
msg['From'] = self.config['sender'] msg['From'] = sender
msg['To'] = self.config['recipient'] msg['To'] = recipient
html_template = self.j2env.get_template("new_post.html.j2") html_template = self.j2env.get_template("new_post.html.j2")
html_source = html_template.render( html_source = html_template.render(
data=data, data=data,
@ -207,8 +209,6 @@ class MastodonEmailBridge:
if self.config['mail_user'] is not None: if self.config['mail_user'] is not None:
conn.login(self.config['mail_user'], self.config['mail_pass']) conn.login(self.config['mail_user'], self.config['mail_pass'])
self._log.debug("Sending email for post with id '%s'...", data['id']) self._log.debug("Sending email for post with id '%s'...", data['id'])
sender = self._str_replace_template(self.config['sender'], data)
recipient = self._str_replace_template(self.config['recipient'], data)
conn.sendmail(sender, recipient, msg.as_string()) conn.sendmail(sender, recipient, msg.as_string())
conn.quit() conn.quit()
self._log.debug("Adding entry to database...") self._log.debug("Adding entry to database...")
@ -218,9 +218,10 @@ class MastodonEmailBridge:
self.sent_items.append(data['id']) self.sent_items.append(data['id'])
return True return True
def _str_replace_template(self, template_string, data): def _str_template(self, template_string, data):
template = self.j2env.from_string(template_string) template = self.j2env.from_string(template_string)
return template.render(data=data) result = template.render(data=data)
return result
def _init_log(self): def _init_log(self):
''' Initialize log object ''' ''' Initialize log object '''

View file

@ -58,7 +58,7 @@
<DIV STYLE="margin:2%;"> <DIV STYLE="margin:2%;">
{% if media['type'] == 'image' %} {% if media['type'] == 'image' %}
<IMG SRC="{{ media['preview_url'] }}" ALT="{{ media['description'] }}"> <IMG SRC="{{ media['preview_url'] }}" ALT="{{ media['description'] }}">
{% elif media['type'] == 'video' %} {% elif media['type'] == 'video' or media['type'] == 'gifv' %}
<video controls width="100%"> <video controls width="100%">
<source src="{{ media['url'] }}" type="video/webm" /> <source src="{{ media['url'] }}" type="video/webm" />
<A HREF="{{ media['url'] }}">Download video</A> <A HREF="{{ media['url'] }}">Download video</A>