1.0.3 • Published 6 years ago

html-mail-styler v1.0.3

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

HTML Mail Styler

Create CSS code and use it in your HTML mail.

Installation

# Git
git clone https://github.com/ihack2712/html-mail-styler

# npm
npm i -S html-mail-styler

# yarn
yarn add html-mail-styler

Usage

First create a html file that will be used when creating the html code that will have styles in it.

HTML

You can call a CSS object by using {# and }. Everything between those two is the name of the css object. You can call variables using {$ and }. Everything between those two is the name of the variable.

CSS

Every css object must start with a # (ID selector), for example;

#some-style {
  background: red;
}
Example

verification_mail.html

<div style="{#container}">
  <div style="{#block}">
    <div style="{#content}">
      <h3 style="{#title}">Verify</h3>
      <p style="{#text}">Thank you for registering to our service, please let us know that this was an intended action by clicking the verify button below. If you didn't register you can ignore this mail.</p>
      <p style="{#text}; {#button-spacer-inline}"><a style="{#verify-button}" href="https://example.com/verify/{$code}" target="_blank">Verify</a></p>
      <p style="{#text}">If the button above doesn't work, then you can press the link below.<br /><a style="{#link}" href="https://example.com/verify/{$code}" target="_blank">https://example.com/verify/{$code}</a></p>
    </div>
    <div style="{#footer}; {#text}">
      Copyright &copy; 2018 - Example
    </div>
  </div>
</div>

styles.css

#container {
  display: block;
  width: 100%;
  background: #263228;
  color: #CFD8DC;
  padding-top: 50px;
  padding-bottom: 50px;
}

#block {
  display: block;
  width: 400px;
  min-height: 500px;
  border-top: 5px solid #29B6F6;
  margin: 0 auto;
  background: #455A64;
  text-align: center;
}

#title {
  color: #ECEFF1;
  font-family: Arial, Helvetica, sans-serif;
  text-transform: uppercase;
  letter-spacing: 1px;
}

#content {
  padding: 10px;
  min-height: calc(500px - 70px);
}

#text {
  font-family: Verdana, Geneva, Tahoma, sans-serif;
  word-wrap: break-word;
}

#footer {
  display: block;
  width: 100%;
  background: #37474F;
  padding-top: 25px;
  padding-bottom: 25px;
}

#button-spacer-inline {
  margin-top: 20px;
  margin-bottom: 20px;
}

#verify-button {
  background: #1E88E5;
  text-decoration: none;
  color: white;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  display: inline;
  padding: 10px 20px 10px 20px;
  font-size: 18px;
  border-radius: 5px;
}

#link {
  color: #1E88E5;
  text-decoration: none;
}
// Add mail styler.
const MailStyler = require('html-mail-styler')
const ms = new MailStyler()

// Import FileSystem
const fs = require('fs')

// Get CSS and HTML file.
const html = fs.readFileSync(__dirname + '/verification_mail.html').toString()
const css = fs.readFileSync(__dirname + '/styles.css').toString()

// Add css to Mail Styler
ms.addCss(css)

// Add Variables
ms.set('code', 'some-verification-code')

// Get HTML result
const result = ms.html(html)

The above code will look like

Preview

Okay, so how does it work?

MailStyler.addCss(string CSS_Code)

Add CSS Code to the parser, called like {#example}

MailStyler.set(string key, string value)

Set variables, called like {$example}

MailStyler.html(string HTML_Code)

Returns the parsed html code.

License

MIT License

Copyright (c) 2018 Richard Olsen Sandberg ihack@theteamcoders.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.