email.ex 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. defmodule PlausibleWeb.Email do
  2. use Plausible
  3. use Bamboo.Phoenix, view: PlausibleWeb.EmailView
  4. import Bamboo.PostmarkHelper
  5. def mailer_email_from do
  6. Application.get_env(:plausible, :mailer_email)
  7. end
  8. def activation_email(user, code) do
  9. priority_email()
  10. |> to(user)
  11. |> tag("activation-email")
  12. |> subject("#{code} is your Plausible email verification code")
  13. |> render("activation_email.html", user: user, code: code)
  14. end
  15. def welcome_email(user) do
  16. base_email()
  17. |> to(user)
  18. |> tag("welcome-email")
  19. |> subject("Welcome to Plausible")
  20. |> render("welcome_email.html", user: user)
  21. end
  22. def create_site_email(user) do
  23. base_email()
  24. |> to(user)
  25. |> tag("create-site-email")
  26. |> subject("Your Plausible setup: Add your website details")
  27. |> render("create_site_email.html", user: user)
  28. end
  29. def site_setup_help(user, site) do
  30. base_email()
  31. |> to(user)
  32. |> tag("help-email")
  33. |> subject("Your Plausible setup: Waiting for the first page views")
  34. |> render("site_setup_help_email.html",
  35. user: user,
  36. site: site
  37. )
  38. end
  39. def site_setup_success(user, site) do
  40. base_email()
  41. |> to(user)
  42. |> tag("setup-success-email")
  43. |> subject("Plausible is now tracking your website stats")
  44. |> render("site_setup_success_email.html",
  45. user: user,
  46. site: site
  47. )
  48. end
  49. def check_stats_email(user) do
  50. base_email()
  51. |> to(user)
  52. |> tag("check-stats-email")
  53. |> subject("Check your Plausible website stats")
  54. |> render("check_stats_email.html", user: user)
  55. end
  56. def password_reset_email(email, reset_link) do
  57. priority_email(%{layout: nil})
  58. |> to(email)
  59. |> tag("password-reset-email")
  60. |> subject("Plausible password reset")
  61. |> render("password_reset_email.html", reset_link: reset_link)
  62. end
  63. def two_factor_enabled_email(user) do
  64. priority_email()
  65. |> to(user)
  66. |> tag("two-factor-enabled-email")
  67. |> subject("Plausible Two-Factor Authentication enabled")
  68. |> render("two_factor_enabled_email.html", user: user)
  69. end
  70. def two_factor_disabled_email(user) do
  71. priority_email()
  72. |> to(user)
  73. |> tag("two-factor-disabled-email")
  74. |> subject("Plausible Two-Factor Authentication disabled")
  75. |> render("two_factor_disabled_email.html", user: user)
  76. end
  77. def trial_one_week_reminder(user) do
  78. base_email()
  79. |> to(user)
  80. |> tag("trial-one-week-reminder")
  81. |> subject("Your Plausible trial expires next week")
  82. |> render("trial_one_week_reminder.html", user: user)
  83. end
  84. def trial_upgrade_email(user, day, usage) do
  85. suggested_plan = Plausible.Billing.Plans.suggest(user, usage.total)
  86. base_email()
  87. |> to(user)
  88. |> tag("trial-upgrade-email")
  89. |> subject("Your Plausible trial ends #{day}")
  90. |> render("trial_upgrade_email.html",
  91. user: user,
  92. day: day,
  93. custom_events: usage.custom_events,
  94. usage: usage.total,
  95. suggested_plan: suggested_plan
  96. )
  97. end
  98. def trial_over_email(user) do
  99. base_email()
  100. |> to(user)
  101. |> tag("trial-over-email")
  102. |> subject("Your Plausible trial has ended")
  103. |> render("trial_over_email.html",
  104. user: user,
  105. extra_offset: Plausible.Auth.User.trial_accept_traffic_until_offset_days()
  106. )
  107. end
  108. def stats_report(email, assigns) do
  109. base_email(%{layout: nil})
  110. |> to(email)
  111. |> tag("#{assigns.type}-report")
  112. |> subject("#{assigns.name} report for #{assigns.site.domain}")
  113. |> html_body(PlausibleWeb.MJML.StatsReport.render(assigns))
  114. end
  115. def spike_notification(email, site, current_visitors, sources, dashboard_link) do
  116. base_email()
  117. |> to(email)
  118. |> tag("spike-notification")
  119. |> subject("Traffic Spike on #{site.domain}")
  120. |> render("spike_notification.html", %{
  121. site: site,
  122. current_visitors: current_visitors,
  123. sources: sources,
  124. link: dashboard_link
  125. })
  126. end
  127. def over_limit_email(user, usage, suggested_plan) do
  128. priority_email()
  129. |> to(user)
  130. |> tag("over-limit")
  131. |> subject("[Action required] You have outgrown your Plausible subscription tier")
  132. |> render("over_limit.html", %{
  133. user: user,
  134. usage: usage,
  135. suggested_plan: suggested_plan
  136. })
  137. end
  138. def enterprise_over_limit_internal_email(user, pageview_usage, site_usage, site_allowance) do
  139. base_email(%{layout: nil})
  140. |> to("enterprise@plausible.io")
  141. |> tag("enterprise-over-limit")
  142. |> subject("#{user.email} has outgrown their enterprise plan")
  143. |> render("enterprise_over_limit_internal.html", %{
  144. user: user,
  145. pageview_usage: pageview_usage,
  146. site_usage: site_usage,
  147. site_allowance: site_allowance
  148. })
  149. end
  150. def dashboard_locked(user, usage, suggested_plan) do
  151. priority_email()
  152. |> to(user)
  153. |> tag("dashboard-locked")
  154. |> subject("[Action required] Your Plausible dashboard is now locked")
  155. |> render("dashboard_locked.html", %{
  156. user: user,
  157. usage: usage,
  158. suggested_plan: suggested_plan
  159. })
  160. end
  161. def yearly_renewal_notification(user) do
  162. date = Timex.format!(user.subscription.next_bill_date, "{Mfull} {D}, {YYYY}")
  163. priority_email()
  164. |> to(user)
  165. |> tag("yearly-renewal")
  166. |> subject("Your Plausible subscription is up for renewal")
  167. |> render("yearly_renewal_notification.html", %{
  168. user: user,
  169. date: date,
  170. next_bill_amount: user.subscription.next_bill_amount,
  171. currency: user.subscription.currency_code
  172. })
  173. end
  174. def yearly_expiration_notification(user) do
  175. next_bill_date = Timex.format!(user.subscription.next_bill_date, "{Mfull} {D}, {YYYY}")
  176. accept_traffic_until =
  177. user
  178. |> Plausible.Users.accept_traffic_until()
  179. |> Timex.format!("{Mfull} {D}, {YYYY}")
  180. priority_email()
  181. |> to(user)
  182. |> tag("yearly-expiration")
  183. |> subject("Your Plausible subscription is about to expire")
  184. |> render("yearly_expiration_notification.html", %{
  185. user: user,
  186. next_bill_date: next_bill_date,
  187. accept_traffic_until: accept_traffic_until
  188. })
  189. end
  190. def cancellation_email(user) do
  191. base_email()
  192. |> to(user.email)
  193. |> tag("cancelled-email")
  194. |> subject("Mind sharing your thoughts on Plausible?")
  195. |> render("cancellation_email.html", user: user)
  196. end
  197. def new_user_invitation(invitation) do
  198. priority_email()
  199. |> to(invitation.email)
  200. |> tag("new-user-invitation")
  201. |> subject("[Plausible Analytics] You've been invited to #{invitation.site.domain}")
  202. |> render("new_user_invitation.html",
  203. invitation: invitation
  204. )
  205. end
  206. def existing_user_invitation(invitation) do
  207. priority_email()
  208. |> to(invitation.email)
  209. |> tag("existing-user-invitation")
  210. |> subject("[Plausible Analytics] You've been invited to #{invitation.site.domain}")
  211. |> render("existing_user_invitation.html",
  212. invitation: invitation
  213. )
  214. end
  215. def ownership_transfer_request(invitation, new_owner_account) do
  216. priority_email()
  217. |> to(invitation.email)
  218. |> tag("ownership-transfer-request")
  219. |> subject("[Plausible Analytics] Request to transfer ownership of #{invitation.site.domain}")
  220. |> render("ownership_transfer_request.html",
  221. invitation: invitation,
  222. new_owner_account: new_owner_account
  223. )
  224. end
  225. def invitation_accepted(invitation) do
  226. priority_email()
  227. |> to(invitation.inviter.email)
  228. |> tag("invitation-accepted")
  229. |> subject(
  230. "[Plausible Analytics] #{invitation.email} accepted your invitation to #{invitation.site.domain}"
  231. )
  232. |> render("invitation_accepted.html",
  233. user: invitation.inviter,
  234. invitation: invitation
  235. )
  236. end
  237. def invitation_rejected(invitation) do
  238. priority_email()
  239. |> to(invitation.inviter.email)
  240. |> tag("invitation-rejected")
  241. |> subject(
  242. "[Plausible Analytics] #{invitation.email} rejected your invitation to #{invitation.site.domain}"
  243. )
  244. |> render("invitation_rejected.html",
  245. user: invitation.inviter,
  246. invitation: invitation
  247. )
  248. end
  249. def ownership_transfer_accepted(invitation) do
  250. priority_email()
  251. |> to(invitation.inviter.email)
  252. |> tag("ownership-transfer-accepted")
  253. |> subject(
  254. "[Plausible Analytics] #{invitation.email} accepted the ownership transfer of #{invitation.site.domain}"
  255. )
  256. |> render("ownership_transfer_accepted.html",
  257. user: invitation.inviter,
  258. invitation: invitation
  259. )
  260. end
  261. def ownership_transfer_rejected(invitation) do
  262. priority_email()
  263. |> to(invitation.inviter.email)
  264. |> tag("ownership-transfer-rejected")
  265. |> subject(
  266. "[Plausible Analytics] #{invitation.email} rejected the ownership transfer of #{invitation.site.domain}"
  267. )
  268. |> render("ownership_transfer_rejected.html",
  269. user: invitation.inviter,
  270. invitation: invitation
  271. )
  272. end
  273. def site_member_removed(membership) do
  274. priority_email()
  275. |> to(membership.user.email)
  276. |> tag("site-member-removed")
  277. |> subject("[Plausible Analytics] Your access to #{membership.site.domain} has been revoked")
  278. |> render("site_member_removed.html",
  279. user: membership.user,
  280. membership: membership
  281. )
  282. end
  283. def import_success(site_import, user) do
  284. import_api = Plausible.Imported.ImportSources.by_name(site_import.source)
  285. label = import_api.label()
  286. priority_email()
  287. |> to(user)
  288. |> tag("import-success-email")
  289. |> subject("#{label} data imported for #{site_import.site.domain}")
  290. |> render(import_api.email_template(), %{
  291. site_import: site_import,
  292. label: label,
  293. link: PlausibleWeb.Endpoint.url() <> "/" <> URI.encode_www_form(site_import.site.domain),
  294. user: user,
  295. success: true
  296. })
  297. end
  298. def import_failure(site_import, user) do
  299. import_api = Plausible.Imported.ImportSources.by_name(site_import.source)
  300. label = import_api.label()
  301. priority_email()
  302. |> to(user)
  303. |> tag("import-failure-email")
  304. |> subject("#{label} import failed for #{site_import.site.domain}")
  305. |> render(import_api.email_template(), %{
  306. site_import: site_import,
  307. label: label,
  308. user: user,
  309. success: false
  310. })
  311. end
  312. def export_success(user, site, expires_at) do
  313. subject =
  314. on_ee do
  315. "Your Plausible Analytics export is now ready for download"
  316. else
  317. "Your export is now ready for download"
  318. end
  319. expires_in =
  320. if expires_at do
  321. Timex.Format.DateTime.Formatters.Relative.format!(
  322. expires_at,
  323. "{relative}"
  324. )
  325. end
  326. download_url =
  327. PlausibleWeb.Router.Helpers.site_url(
  328. PlausibleWeb.Endpoint,
  329. :download_export,
  330. site.domain
  331. )
  332. priority_email()
  333. |> to(user)
  334. |> tag("export-success")
  335. |> subject(subject)
  336. |> render("export_success.html",
  337. user: user,
  338. site: site,
  339. download_url: download_url,
  340. expires_in: expires_in
  341. )
  342. end
  343. def export_failure(user, site) do
  344. subject =
  345. on_ee do
  346. "Your Plausible Analytics export has failed"
  347. else
  348. "Your export has failed"
  349. end
  350. priority_email()
  351. |> to(user)
  352. |> subject(subject)
  353. |> render("export_failure.html", user: user, site: site)
  354. end
  355. def error_report(reported_by, trace_id, feedback) do
  356. Map.new()
  357. |> Map.put(:layout, nil)
  358. |> base_email()
  359. |> to("bugs@plausible.io")
  360. |> put_param("ReplyTo", reported_by)
  361. |> tag("sentry")
  362. |> subject("Feedback to Sentry Trace #{trace_id}")
  363. |> render("error_report_email.html", %{
  364. reported_by: reported_by,
  365. feedback: feedback,
  366. trace_id: trace_id
  367. })
  368. end
  369. def approaching_accept_traffic_until(notification) do
  370. base_email()
  371. |> to(notification.email)
  372. |> tag("drop-traffic-warning-first")
  373. |> subject("We'll stop counting your stats")
  374. |> render("approaching_accept_traffic_until.html",
  375. time: "next week",
  376. user: %{email: notification.email, name: notification.name}
  377. )
  378. end
  379. def approaching_accept_traffic_until_tomorrow(notification) do
  380. base_email()
  381. |> to(notification.email)
  382. |> tag("drop-traffic-warning-final")
  383. |> subject("A reminder that we'll stop counting your stats tomorrow")
  384. |> render("approaching_accept_traffic_until.html",
  385. time: "tomorrow",
  386. user: %{email: notification.email, name: notification.name}
  387. )
  388. end
  389. @doc """
  390. Unlike the default 'base' emails, priority emails cannot be unsubscribed from. This is achieved
  391. by sending them through a dedicated 'priority' message stream in Postmark.
  392. """
  393. def priority_email(), do: priority_email(%{layout: "priority_email.html"})
  394. def priority_email(%{layout: layout}) do
  395. base_email(%{layout: layout})
  396. |> put_param("MessageStream", "priority")
  397. end
  398. def base_email(), do: base_email(%{layout: "base_email.html"})
  399. def base_email(%{layout: layout}) do
  400. mailer_from = Application.get_env(:plausible, :mailer_email)
  401. new_email()
  402. |> put_param("TrackOpens", false)
  403. |> from(mailer_from)
  404. |> maybe_put_layout(layout)
  405. end
  406. defp maybe_put_layout(email, nil), do: email
  407. defp maybe_put_layout(email, layout) do
  408. put_html_layout(email, {PlausibleWeb.LayoutView, layout})
  409. end
  410. end