|
1 | 1 | defmodule Algora.Mailer do |
2 | 2 | @moduledoc false |
3 | 3 | use Swoosh.Mailer, otp_app: :algora |
| 4 | + |
| 5 | + require Logger |
| 6 | + |
| 7 | + def deliver_with_logging(mail) do |
| 8 | + case deliver(mail) do |
| 9 | + {:ok, _} -> |
| 10 | + {:ok, mail} |
| 11 | + |
| 12 | + {:error, reason} -> |
| 13 | + Logger.error(""" |
| 14 | + Email delivery failed: |
| 15 | +
|
| 16 | + Subject: #{mail.subject} |
| 17 | + To: #{inspect(mail.to)} |
| 18 | + Reason: #{inspect(reason, pretty: true)} |
| 19 | + """) |
| 20 | + |
| 21 | + {:error, reason} |
| 22 | + end |
| 23 | + end |
| 24 | + |
| 25 | + def html_template(template_params) do |
| 26 | + """ |
| 27 | + <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 28 | + <html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office"> |
| 29 | + <head> |
| 30 | + <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 31 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 32 | + <!--[if gte mso 9]><xml> |
| 33 | + <o:OfficeDocumentSettings> |
| 34 | + <o:AllowPNG/> |
| 35 | + <o:PixelsPerInch>96</o:PixelsPerInch> |
| 36 | + </o:OfficeDocumentSettings> |
| 37 | + </xml><![endif]--> |
| 38 | + </head> |
| 39 | + <body style="margin: 0; padding: 0; min-width: 100%; background-color: #ffffff;"> |
| 40 | + <div style="background-color: #ffffff; box-sizing: border-box; display: block; padding: 0;"> |
| 41 | + <table cellpadding="0" cellspacing="0" width="100%"> |
| 42 | + <tr> |
| 43 | + <td style="font-family:sans-serif; font-size: 16px;"> |
| 44 | + #{html_sections(template_params)} |
| 45 | + </td> |
| 46 | + </tr> |
| 47 | + </table> |
| 48 | + </div> |
| 49 | + </body> |
| 50 | + </html> |
| 51 | + """ |
| 52 | + end |
| 53 | + |
| 54 | + def text_template(template_params) do |
| 55 | + """ |
| 56 | + ============================== |
| 57 | +
|
| 58 | + #{text_sections(template_params)} |
| 59 | +
|
| 60 | + ============================== |
| 61 | + """ |
| 62 | + end |
| 63 | + |
| 64 | + defp text_sections(template_params) do |
| 65 | + template_params |
| 66 | + |> Enum.map(fn {type, value} -> text_section(type, value) end) |
| 67 | + |> Enum.intersperse("\n\n") |
| 68 | + end |
| 69 | + |
| 70 | + defp text_section(:cta, %{href: href, src: src}) do |
| 71 | + ~s|#{href}\n\n#{src}| |
| 72 | + end |
| 73 | + |
| 74 | + defp text_section(_, value) do |
| 75 | + value |
| 76 | + end |
| 77 | + |
| 78 | + defp html_sections(template_params) do |
| 79 | + for {type, value} <- template_params, |
| 80 | + do: html_section(type, value) |
| 81 | + end |
| 82 | + |
| 83 | + defp html_section(:markdown, value) do |
| 84 | + html = Cmark.to_html(value) |
| 85 | + |
| 86 | + ~s""" |
| 87 | + <table cellpadding="0" cellspacing="0" width="100%"> |
| 88 | + <tr> |
| 89 | + <td> |
| 90 | + #{html} |
| 91 | + </td> |
| 92 | + </tr> |
| 93 | + </table> |
| 94 | + """ |
| 95 | + end |
| 96 | + |
| 97 | + defp html_section(type, value) do |
| 98 | + ~s|<p style="font-family: sans-serif; font-size: 16px; line-height: 1.5; padding-top: 0;">| <> |
| 99 | + html_section_by_type(type, value) <> ~s|</p>| |
| 100 | + end |
| 101 | + |
| 102 | + defp html_section_by_type(:cta, %{href: href, src: src}) do |
| 103 | + ~s|<a href="#{href}" style="word-break: break-all; word-wrap: break-word;">| <> |
| 104 | + ~s|<img src="#{src}" style="width: 100%; height: auto;">| <> |
| 105 | + ~s|</a>| |
| 106 | + end |
| 107 | + |
| 108 | + defp html_section_by_type(:url, value) do |
| 109 | + ~s|<a href="#{value}" style="word-break: break-all; word-wrap: break-word;">| <> |
| 110 | + value <> ~s|</a>| |
| 111 | + end |
| 112 | + |
| 113 | + defp html_section_by_type(:img, value) do |
| 114 | + ~s|<img src="#{value}" style="width: 100%; height: auto;">| |
| 115 | + end |
| 116 | + |
| 117 | + defp html_section_by_type(_, text) do |
| 118 | + text |
| 119 | + end |
4 | 120 | end |
0 commit comments