diff --git a/bookwyrm/emailing.py b/bookwyrm/emailing.py index 7069286d..47ac59df 100644 --- a/bookwyrm/emailing.py +++ b/bookwyrm/emailing.py @@ -4,45 +4,61 @@ from django.template.loader import get_template from bookwyrm import models from bookwyrm.tasks import app +from bookwyrm.settings import DOMAIN + + +def email_data(): + """ fields every email needs """ + site = models.SiteSettings.objects.get() + if site.logo_small: + logo_path = "/images/{}".format(site.logo_small.url) + else: + logo_path = "/static/images/logo-small.png" + + return { + "site_name": site.name, + "logo": logo_path, + "domain": DOMAIN, + "user": None, + } def invite_email(invite_request): """ send out an invite code """ - site = models.SiteSettings.objects.get() - data = { - "site_name": site.name, - "invite_link": invite_request.invite.link, - } - send_email.delay(invite_request.email, "invite", data) + data = email_data() + data["invite_link"] = invite_request.invite.link + send_email.delay(invite_request.email, *format_email("invite", data)) def password_reset_email(reset_code): """ generate a password reset email """ - site = models.SiteSettings.objects.get() - data = { - "site_name": site.name, - "reset_link": reset_code.link, - } - send_email.delay(reset_code.user.email, "password_reset", data) + data = email_data() + data["reset_link"] = reset_code.link + data["user"] = reset_code.user.display_name + send_email.delay(reset_code.user.email, *format_email("password_reset", data)) -@app.task -def send_email(recipient, message_name, data): - """ use a task to send the email """ +def format_email(email_name, data): + """ render the email templates """ subject = ( - get_template("email/{}/subject.html".format(message_name)).render(data).strip() + get_template("email/{}/subject.html".format(email_name)).render(data).strip() ) html_content = ( - get_template("email/{}/html_content.html".format(message_name)) + get_template("email/{}/html_content.html".format(email_name)) .render(data) .strip() ) text_content = ( - get_template("email/{}/text_content.html".format(message_name)) + get_template("email/{}/text_content.html".format(email_name)) .render(data) .strip() ) + return (subject, html_content, text_content) + +@app.task +def send_email(recipient, subject, html_content, text_content): + """ use a task to send the email """ email = EmailMultiAlternatives(subject, text_content, None, [recipient]) email.attach_alternative(html_content, "text/html") email.send() diff --git a/bookwyrm/static/images/logo-small.png b/bookwyrm/static/images/logo-small.png index 10ea7a38..72f49ef7 100644 Binary files a/bookwyrm/static/images/logo-small.png and b/bookwyrm/static/images/logo-small.png differ diff --git a/bookwyrm/templates/email/html_layout.html b/bookwyrm/templates/email/html_layout.html new file mode 100644 index 00000000..02527ff5 --- /dev/null +++ b/bookwyrm/templates/email/html_layout.html @@ -0,0 +1,26 @@ +{% load i18n %} +
+ {% if user %}{{ user }},{% else %}{% trans "Hi there," %}{% endif %} +
+ {% block content %}{% endblock %} +{% blocktrans %}BookWyrm hosted on {{ site_name }}{% endblocktrans %}
+ {% if user %} + + {% endif %} ++ {% blocktrans %}You're invited to join {{ site_name }}!{% endblocktrans %} +
+ +{% trans "Join Now" as text %} +{% include 'email/snippets/action.html' with path=invite_link text=text %} + ++ {% url 'code-of-conduct' as coc_path %} + {% url 'about' as about_path %} + {% blocktrans %}Learn more about this instance.{% endblocktrans %} +
+{% endblock %} diff --git a/bookwyrm/templates/email/invite/subject.html b/bookwyrm/templates/email/invite/subject.html index b3c4a141..efb8be5a 100644 --- a/bookwyrm/templates/email/invite/subject.html +++ b/bookwyrm/templates/email/invite/subject.html @@ -1,2 +1,2 @@ {% load i18n %} -{% blocktrans %}You're invited! Join {{ site_name }}{% endblocktrans %} +{% blocktrans %}You're invited to join {{ site_name }}!{% endblocktrans %} diff --git a/bookwyrm/templates/email/invite/text_content.html b/bookwyrm/templates/email/invite/text_content.html index 7add9010..c3fcdc04 100644 --- a/bookwyrm/templates/email/invite/text_content.html +++ b/bookwyrm/templates/email/invite/text_content.html @@ -1,2 +1,10 @@ +{% extends 'email/text_layout.html' %} {% load i18n %} -{% blocktrans %}Join {{ site_name }}: {{ invite_link }}{% endblocktrans %} +{% block content %} +{% blocktrans %}You're invited to join {{ site_name }}! Click the link below to create an account.{% endblocktrans %} + +{{ invite_link }} + +{% trans "Learn more about this instance:" %} https://{{ domain }}{% url 'about' %} + +{% endblock %} diff --git a/bookwyrm/templates/email/password_reset/html_content.html b/bookwyrm/templates/email/password_reset/html_content.html index e3a006b0..eef0e5e5 100644 --- a/bookwyrm/templates/email/password_reset/html_content.html +++ b/bookwyrm/templates/email/password_reset/html_content.html @@ -1,2 +1,15 @@ +{% extends 'email/html_layout.html' %} {% load i18n %} -{% blocktrans %}Your password reset link is: {{ reset_link }}{% endblocktrans %} + +{% block content %} ++ {% blocktrans %}You requested to reset your {{ site_name }} password. Click the link below to set a new password and log in to your account.{% endblocktrans %} +
+ +{% trans "Reset Password" as text %} +{% include 'email/snippets/action.html' with path=reset_link text=text %} + ++ {% trans "If you didn't request to reset your password, you can ignore this email." %} +
+{% endblock %} diff --git a/bookwyrm/templates/email/password_reset/subject.html b/bookwyrm/templates/email/password_reset/subject.html index b216a6c2..88680140 100644 --- a/bookwyrm/templates/email/password_reset/subject.html +++ b/bookwyrm/templates/email/password_reset/subject.html @@ -1,2 +1,2 @@ {% load i18n %} -{% blocktrans %}Reset your password on {{ site_name }}{% endblocktrans %} +{% blocktrans %}Reset your {{ site_name }} password{% endblocktrans %} diff --git a/bookwyrm/templates/email/password_reset/text_content.html b/bookwyrm/templates/email/password_reset/text_content.html index e3a006b0..b5cf754e 100644 --- a/bookwyrm/templates/email/password_reset/text_content.html +++ b/bookwyrm/templates/email/password_reset/text_content.html @@ -1,2 +1,9 @@ +{% extends 'email/text_layout.html' %} {% load i18n %} -{% blocktrans %}Your password reset link is: {{ reset_link }}{% endblocktrans %} +{% block content %} +{% blocktrans %}You requested to reset your {{ site_name }} password. Click the link below to set a new password and log in to your account.{% endblocktrans %} + +{{ reset_link }} + +{% trans "If you didn't request to reset your password, you can ignore this email." %} +{% endblock %} diff --git a/bookwyrm/templates/email/preview.html b/bookwyrm/templates/email/preview.html new file mode 100644 index 00000000..66d856c0 --- /dev/null +++ b/bookwyrm/templates/email/preview.html @@ -0,0 +1,19 @@ + + ++ + {{ text }} + +
diff --git a/bookwyrm/templates/email/text_layout.html b/bookwyrm/templates/email/text_layout.html new file mode 100644 index 00000000..cd0444f1 --- /dev/null +++ b/bookwyrm/templates/email/text_layout.html @@ -0,0 +1,3 @@ +{% load i18n %} +{% if user %}{{ user.display_name }},{% else %}{% trans "Hi there," %}{% endif %} +{% block content %}{% endblock %} diff --git a/bookwyrm/templates/password_reset_request.html b/bookwyrm/templates/password_reset_request.html index 97191afa..5d877442 100644 --- a/bookwyrm/templates/password_reset_request.html +++ b/bookwyrm/templates/password_reset_request.html @@ -8,7 +8,9 @@{{ message }}
{% endif %} + + {% if message %}{{ message }}
{% endif %} +{% trans "A link to reset your password will be sent to your email address" %}