From 7ff96fabe1aa61fa68b170e9bddffa241479be29 Mon Sep 17 00:00:00 2001 From: mrloyal Date: Fri, 7 Jun 2024 08:15:09 +0200 Subject: [PATCH] feat: allow text message --- README.md | 10 ++++++---- lib/swoosh_mailinator.ex | 31 +++++++++++++++++++++++++++---- mix.exs | 2 +- test/swoosh_mlntr.exs | 40 +++++++++++++++++++++++++++++++++------- 4 files changed, 67 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index cc961e0..8106b5f 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,13 @@ This project is an adapter for swoosh that relais any email to the Mailinator.com SMTP server, no mater the internet domain of the recipient. -This service provide no encryption in transit (SSL/TLS) nor authentication. +Mailinator does not support multipart messages (they are processed as attachment which are ignored). So the adapter ensure that there is only htlm or text part and not both. -This service provide public access to mailboxes. +The service provides no encryption in transit (SSL/TLS) nor authentication. -Don't use this adapter for confidential information. +The service provides public access to mailboxes. + +Don't use this adapter for private or confidential information. Use for tests only. @@ -17,7 +19,7 @@ This module can be installed by adding `swoosh_mailinator` to your list of depen ```elixir def deps do [ - {:swoosh_mailinator, "~> 0.1.0", git: "https://git.zoocoop.com/setop/swoosh_mailinator"} + {:swoosh_mailinator, "~> 0.2.0", git: "https://git.zoocoop.com/setop/swoosh_mailinator"} ] end ``` diff --git a/lib/swoosh_mailinator.ex b/lib/swoosh_mailinator.ex index fcf49db..14a5991 100644 --- a/lib/swoosh_mailinator.ex +++ b/lib/swoosh_mailinator.ex @@ -36,18 +36,41 @@ defmodule Swoosh.Adapters.Mailinator do Don't use this adapter for confidential information. Use for tests only. - - TODO: allow to choose from Text or HTML :prefer_html, :prefer_text - """ +""" use Swoosh.Adapter, required_config: [] alias Swoosh.Email alias Swoosh.Mailer - def deliver(%Email{} = email, _) do + def deliver(%Email{html_body: html, text_body: text} = email, _) when is_nil(html) do + email |> delegate + end + + def deliver(%Email{html_body: html, text_body: text} = email, _) when is_nil(text) do + email |> delegate + end + + def deliver(%Email{html_body: _, text_body: _} = email, _) do email |> Map.put(:text_body, nil) + |> delegate + end + + def deliver(%Email{text_body: _} = email, _) do + email + |> Map.put(:html_body, nil) + |> delegate + end + + def deliver(%Email{html_body: _} = email, _) do + email + |> Map.put(:text_body, nil) + |> delegate + end + + defp delegate(%Email{} = email) do + email |> Mailer.deliver([ adapter: Swoosh.Adapters.SMTP, relay: "mail.mailinator.com", diff --git a/mix.exs b/mix.exs index 88d7439..d74a3e5 100644 --- a/mix.exs +++ b/mix.exs @@ -3,7 +3,7 @@ defmodule SwooshMailinator.MixProject do def project do [ - version: "0.1.0", + version: "0.2.0", deps: [ {:swoosh, "~> 1.7"}, ] diff --git a/test/swoosh_mlntr.exs b/test/swoosh_mlntr.exs index e2a6546..f9f4353 100644 --- a/test/swoosh_mlntr.exs +++ b/test/swoosh_mlntr.exs @@ -11,20 +11,42 @@ defmodule Main do import Swoosh.Email import Swoosh.Mailer - def main do - mess = """ -Compose, deliver and test your emails easily in Elixir. + def test_both() do + prep() + end -Swoosh comes with many adapters, including SendGrid, Mandrill, Mailgun, Postmark and SMTP. See the full list of adapters below. + def test_text_only() do + prep() + |> Map.delete(:html_body) + end -The complete documentation for Swoosh is available online at HexDocs. -""" + def test_text_nil() do + prep() + |> text_body(nil) + end + + def test_html_only() do + prep() + |> Map.delete(:text_body) + end + + def test_html_nil() do + prep() + |> html_body(nil) + end + + def prep do + mess = "mess" new() |> to("mlkjhg@kiujygtrfedc.com") |> from({"Who Knows", "who.knows@nonexisting.tld"}) |> subject("Hello, Swoosh!") |> html_body("

Hello World

"<>mess<>"

") |> text_body(mess) + end + + def send(email) do + email |> IO.inspect() |> deliver([ adapter: Swoosh.Adapters.Mailinator @@ -32,4 +54,8 @@ The complete documentation for Swoosh is available online at HexDocs. end end -Main.main() |> IO.inspect() +Main.test_both() |> Main.send() |> IO.inspect() +Main.test_text_only() |> Main.send() |> IO.inspect() +Main.test_html_only() |> Main.send() |> IO.inspect() +Main.test_text_nil() |> Main.send() |> IO.inspect() +Main.test_html_nil() |> Main.send() |> IO.inspect()