@@ -7,6 +7,7 @@ defmodule AlgoraWeb.Admin.CampaignLive do
77 import Ecto.Query
88
99 alias Algora.Activities.Jobs.SendCampaignEmail
10+ alias Algora.Admin
1011 alias Algora.Mailer
1112 alias Algora.Repo
1213 alias Algora.Workspace.Repository
@@ -302,20 +303,12 @@ defmodule AlgoraWeb.Admin.CampaignLive do
302303 Repo . transact ( fn _ ->
303304 recipients
304305 |> Enum . map ( fn recipient ->
305- template_params = [
306- markdown: render_preview ( template , recipient ) ,
307- cta: % {
308- href: "#{ AlgoraWeb.Endpoint . url ( ) } /go/#{ recipient [ "repo_owner" ] } /#{ recipient [ "repo_name" ] } " ,
309- src: "cid:#{ recipient [ "repo_owner" ] } .png"
310- }
311- ]
312-
313306 % {
314307 id: "2025-04-oss" ,
315308 subject: subject ,
316309 recipient_email: recipient [ "email" ] ,
317310 recipient: Algora.Util . term_to_base64 ( recipient ) ,
318- template_params: Algora.Util . term_to_base64 ( template_params ) ,
311+ template: template ,
319312 from_name: from_name ,
320313 from_email: from_email ,
321314 preheader: render_preview ( preheader , recipient )
@@ -332,38 +325,44 @@ defmodule AlgoraWeb.Admin.CampaignLive do
332325 end
333326
334327 def deliver_email ( opts ) do
335- recipient = opts [ :recipient ]
336-
337- case :get
338- |> Finch . build ( "https://algora.io/og/go/#{ recipient [ "repo_owner" ] } /#{ recipient [ "repo_name" ] } " )
339- |> Finch . request ( Algora.Finch ) do
340- { :ok , % Finch.Response { status: status , body: body } } when status in 200 .. 299 ->
341- opts
342- |> Keyword . put ( :attachments , [
343- Swoosh.Attachment . new ( { :data , body } ,
344- filename: "#{ recipient [ "repo_owner" ] } .png" ,
345- content_type: "image/png" ,
346- type: :inline
347- )
348- ] )
349- |> deliver ( )
328+ case opts [ :template ] |> render_preview ( opts [ :recipient ] ) |> extract_attachments ( ) do
329+ { :ok , { preview , attachments } } ->
330+ Email . new ( )
331+ |> Email . to ( opts [ :recipient ] [ "email" ] )
332+ |> Email . from ( opts [ :from ] )
333+ |> Email . subject ( opts [ :subject ] )
334+ |> Email . text_body ( Mailer . text_template ( markdown: preview ) )
335+ |> Email . html_body ( Mailer . html_template ( [ markdown: preview ] , preheader: opts [ :preheader ] ) )
336+ |> then ( & Enum . reduce ( attachments , & 1 , fn attachment , acc -> Email . attachment ( acc , attachment ) end ) )
337+ |> Mailer . deliver_with_logging ( )
350338
351339 { :error , reason } ->
352- raise reason
340+ Admin . alert ( "Failed to deliver email: #{ inspect ( reason ) } " )
341+ { :error , reason }
353342 end
354343 end
355344
356- defp deliver ( opts ) do
357- email =
358- Email . new ( )
359- |> Email . to ( opts [ :recipient ] [ "email" ] )
360- |> Email . from ( opts [ :from ] )
361- |> Email . subject ( opts [ :subject ] )
362- |> Email . text_body ( Mailer . text_template ( opts [ :template_params ] ) )
363- |> Email . html_body ( Mailer . html_template ( opts [ :template_params ] , preheader: opts [ :preheader ] ) )
345+ defp extract_attachments ( preview ) do
346+ image_regex = ~r/ !\[ (.*?)\] \( (.*?)\) /
347+
348+ image_regex
349+ |> Regex . scan ( preview )
350+ |> Enum . reduce_while ( { :ok , { preview , [ ] } } , fn [ full_match , alt , src ] , { :ok , { current_preview , current_attachments } } ->
351+ case :get |> Finch . build ( src ) |> Finch . request ( Algora.Finch ) do
352+ { :ok , % Finch.Response { status: status , body: body } } when status in 200 .. 299 ->
353+ attachment =
354+ Swoosh.Attachment . new ( { :data , body } , filename: "#{ alt } .png" , content_type: "image/png" , type: :inline )
364355
365- email = Enum . reduce ( opts [ :attachments ] , email , fn attachment , acc -> Email . attachment ( acc , attachment ) end )
356+ new_preview = String . replace ( current_preview , full_match , "" )
357+ { :cont , { :ok , { new_preview , [ attachment | current_attachments ] } } }
366358
367- Mailer . deliver_with_logging ( email )
359+ { :error , reason } ->
360+ { :halt , { :error , reason } }
361+ end
362+ end )
363+ |> case do
364+ { :ok , { preview , attachments } } -> { :ok , { preview , Enum . reverse ( attachments ) } }
365+ { :error , reason } -> { :error , reason }
366+ end
368367 end
369368end
0 commit comments