feat: allow text message

This commit is contained in:
mrloyal 2024-06-07 08:15:09 +02:00
parent 5b66cde5bc
commit 7ff96fabe1
4 changed files with 67 additions and 16 deletions

View File

@ -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
```

View File

@ -36,8 +36,6 @@ 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: []
@ -45,9 +43,34 @@ defmodule Swoosh.Adapters.Mailinator do
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",

View File

@ -3,7 +3,7 @@ defmodule SwooshMailinator.MixProject do
def project do
[
version: "0.1.0",
version: "0.2.0",
deps: [
{:swoosh, "~> 1.7"},
]

View File

@ -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("<h1>Hello World</h1><p>"<>mess<>"</p>")
|> 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()