Skip to content

Commit 16901a9

Browse files
authored
Adjustment to allow sending Base64 attachments, not just plain text and fix html preview on message list (#28)
* Adjustment to allow sending Base64 attachments, not just plain text and Added a few parameters to README.md * A bug that prevented the preview of email bodies in HTML from being displayed in the message list has been fixed
1 parent 696444f commit 16901a9

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,11 @@ The request body has to be json with these parameters
123123
|subject|string|no|The email`s subject field|`{"subject":"Some Subject"}`|
124124
|from|string|yes|The email address from which the email is sent.<br>Must be something@yourdomain.com.<br> The the domain has to be added to SES and be verified.|`{"from":"noreply@yourdomain.com"}`|
125125
|to|string/string[]|yes|The emails recipient(s)|`{"to":"joe@somedomain.com"}` or `{"to": ["joe@somedomain.com", "jane@somedomain.com"]}`|
126+
|cc|string|no|"The email address to which a copy of the email will be sent.".|`{"cc":"cc_address@yourdomain.com"}`|
127+
|reply_to|string|no|The email address from which the reply will be sent.|`{"reply_to":"reply@yourdomain.com"}`|
126128
|headers|object|no|An object containing key/value pairs of headers|`{"headers": {"X-Entity-Ref-ID": "00"}}`|
127129
|provider_options|object|no|An object containing key/value pairs of provider options|`{"provider_options": {"tags": [{"name": "tag-name", "value": "some-value"}]}}`|
128-
|attachments|array|no|An array containing attachments|`{"attachments": [{"filename": "invoice.txt", "content": "pewp"}]}`|
130+
|attachments|array|no|An array containing attachments|`{"attachments": [{"filename": "invoice.txt", "content": "pewp", "content_type": "text/plain"}]}`|
129131

130132
### Example
131133

lib/phoenix_00/mailer.ex

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,38 @@ defmodule Phoenix00.Mailer do
6868
{:error, "Missing content type on attachment."}
6969

7070
content_type ->
71+
# Validate if the content is a Base64 string and decode if necessary
72+
content =
73+
case is_base64?(attachment["content"]) do
74+
true ->
75+
Base.decode64!(attachment["content"]) # Decode Base64
76+
false ->
77+
attachment["content"] # It keeps the content as is.
78+
end
79+
7180
swoosh
7281
|> Swoosh.Email.attachment(
73-
Swoosh.Attachment.new({:data, attachment["content"]},
82+
Swoosh.Attachment.new({:data, content},
7483
filename: attachment["filename"],
7584
content_type: content_type
7685
)
7786
)
7887
end
7988
end
8089

90+
# Function to validate string Base64
91+
defp is_base64?(string) when is_bitstring(string) do
92+
case Base.decode64(string, ignore: :whitespace) do
93+
{:ok, decoded} ->
94+
# If encoding it again gives us the original string, it is valid Base64
95+
Base.encode64(decoded) == String.replace(string, ~r/\s+/, "")
96+
:error ->
97+
false
98+
end
99+
end
100+
101+
defp is_base64?(_), do: false
102+
81103
defp map_to_contact(info) when is_list(info) do
82104
Enum.map(info, &map_to_contact/1)
83105
end

lib/phoenix_00_web/live/message_live/show.html.heex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
title="Inline Frame Example"
4242
width="100%"
4343
style="min-height: 600px;"
44-
srcdoc={raw(@message.email.body)}
44+
srcdoc={@message.email.body}
4545
>
4646
</iframe>
4747
</div>

0 commit comments

Comments
 (0)