4

I am working on app in elixir. It sends email to clients. I am using bamboo library for sending emails.

So far, emails are working fine. But now, I am trying to send emails using templates. Everywhere i see in the bamboo documentation is using bamboo.phoenix .

I am not using phoenix for handling requests. I am using a library called plug. Is there a way to send templates in email without phoenix ??

NeiL
  • 791
  • 8
  • 35
  • 2
    As one way to go and I am not sure if it's a good one or not but you could use this -> https://hexdocs.pm/eex/EEx.html#function_from_file/5 to compile your own templates into functions and use them for rendering. – NoDisplayName Jun 15 '17 at 08:41
  • @JustMichael Interesting. It also supports dynamic variables in templates. But have to check the performance. – NeiL Jun 15 '17 at 08:51
  • I've done this before, but can't remember exactly how at the moment. Look at the phoenix source to see how they load templates. It should be a pretty easy copy pasta. – Mike Quinlan Jun 17 '17 at 15:08
  • Phoenix is quite modular and built on plug, so you should be able to use Phoenix views just for the templating, but not phoenix Endpoint/Router/Controller. – Mike Buhot Jun 18 '17 at 23:54

1 Answers1

2

Adding answer to this post with the help @JustMichael comment.

Directory structure -

/priv
 /static
  /test.html.eex

Function used :

new_email
|> to("vivek29vivek@gmail.com")
|> from(@from_email)
|> subject("test")
|> html_body(EEx.eval_file("priv/static/mail_templates/#test.html.eex",[foo: "bar"]))  //this will render the template.Also can pass variables 

test.html.eex

<h3>Foo: <%= foo %></h3>

But , we cannot add css just by adding <link rel="stylesheet" href="styles.css"> . I guess, There is a need for static server. Do comment if there is another way to add css apart from inline css.

NeiL
  • 791
  • 8
  • 35
  • I think the CSS can only be inlined (apart from using a static sefver) which is a good idea because it makes the email self-contained. – Ludovic Kuty Oct 28 '20 at 11:30