Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML Body File Template with Variables #124

Open
MattTimmons opened this issue Dec 27, 2021 · 4 comments
Open

HTML Body File Template with Variables #124

MattTimmons opened this issue Dec 27, 2021 · 4 comments

Comments

@MattTimmons
Copy link

Other SMTP / email libraries for JS or Python have the ability to "render" an html files and pass through variables.

ie.. pseudo code:

var htmlTemplate =  fs.readFileSync('./views/email-welcome.html', 'utf-8')

data = {
	orgName: organizationName,
	orgTitle: orgTitle,
	email: email,
}
var emailHTML = ejs.render(htmlTemplate, data);
_sendMailgunEmail(from, email, 'Welcome to my app!', emailHTML);

I know I can pass html as a string and use variable that way, but that would make a for a lot in one file. Any other suggestions or could something like this be possible as feature request?

@MattTimmons
Copy link
Author

MattTimmons commented Dec 27, 2021

To add a bit more.
I know I can load in an html file as string and use it..

let emailTemplateFile = Bundle.main.path(forResource: "myemail", ofType: "html")!
let contents = try? String(contentsOfFile: emailTemplateFile)
        
let htmlContent = Attachment(htmlContent: contents!)
let mail = Mail(
     from: fromEmail,
     to:emailUsers,
     cc: [],
     subject: "some email test",
     attachments: [htmlContent]
)

And then use a simple stringReplace to handle inline variables. However, one nice thing about the others is you can use bool/conditionals in your email templates as they render. Ie..

<p>
<% if (!userExists) { %>
	<strong>NOTE:</strong> You may need to change your password after you first log in.<br>
	Your temporary password:<br>
	<strong><%- password %></strong><br>
	&nbsp;
<% } %>
</p>

I would appreciate it if anyone has ideas on doing this.
Thanks!

@dannys42
Copy link
Contributor

dannys42 commented Jan 1, 2022

For relatively simple use cases, perhaps you can keep your text/html in a struct. For example, something like this:

struct Message: CustomStringConvertible {
   let username: String
  var description: String {
    "Hello \(username)!  How are you?"
  }
}

When it comes to mailing then you should simply use Message(username: "Charlie").

If you really want to be able to read the contents from a file, you could perhaps use Codable and load from a JSON or have a custom Decoder. Or you could use a templating system like Stencil or Tuxedo.

@MattTimmons
Copy link
Author

I will look more into this. Loops are another good use for this.. like building a list of things in an email, all things I have come across recently. For simple things, like a username and one-time string, are fine, but more complex is obviously more difficult. I just thought I would highlight to desire to have something similar in this framework.

@sbeitzel
Copy link

Kitura already has a template engine in place for the web server component -- is it reasonable to try using Stencil, or does that wind up pulling in other unnecessary stuff?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants