Inline styling for html mail in golang

go-premailer

Inline styling for HTML mail in golang

Document

Build Status GoDoc GoCover Go Report Card

install

go get github.com/vanng822/go-premailer/premailer

Example

import (
	"fmt"
	"github.com/vanng822/go-premailer/premailer"
	"log"
)

func main() {
	prem, err := premailer.NewPremailerFromFile(inputFile, premailer.NewOptions())
	if err != nil {
		log.Fatal(err)
	}
	
	html, err := prem.Transform()
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(html)
}

Input

<html>
<head>
<title>Title</title>
<style type="text/css">
	h1 { width: 300px; color:red; }
	strong { text-decoration:none; }
</style>
</head>
<body>
	<h1>Hi!</h1>
	<p><strong>Yes!</strong></p>
</body>
</html>

Output

<html>
<head>
<title>Title</title>
</head>
<body>
	<h1 style="color:red;width:300px" width="300">Hi!</h1>
	<p><strong style="text-decoration:none">Yes!</strong></p>
</body>
</html>

Commandline

> go run main.go -i your_email.html
> go run main.go -i your_mail.html -o process_mail.html

Demo

http://premailer.isgoodness.com/

Conversion endpoint

http://premailer.isgoodness.com/convert

request POST:
	html: your mail
	cssToAttributes: true|false
	removeClasses: true|false
response:
	{result: output}
Owner
Nguyen Van Nhu
Team-player and always try to make it easy for people around me. I have been learning so much through the years but prefer working with Python, Golang or Nodejs
Nguyen Van Nhu
Comments
  • Breaks Responsive Layout with Foundation for Emails

    Breaks Responsive Layout with Foundation for Emails

    I looove the idea of having an inliner available in GO. Thank you so much for writing this.

    Unfortunately for me though, it doesn't work for inlining emails written with [Foundation for Emails].

    Before inline:

    Screenshot 2019-04-26 15 12 21

    After inline:

    Screenshot 2019-04-26 15 12 30

    It works fine when using another inliner, inline-css (with node), but somehow the inlining with with go-premailier is breaking it.

    For a full example of the email and broken output:

    HTML with embedded CSS before inlining

    HTML after inlining with inline-css npm package - This works 👍

    HTML after inling with go-premailer - This is broken 😢

    I hope this is helpful... I would looove to be able to inline these in Go without having to do slow calls out to a separate node script.

    Thanks!

  • Why all leftover rules are marked as important?

    Why all leftover rules are marked as important?

    My input HTML:

    <html>
    <head>
    <title>Title</title>
    <style type="text/css">
           a, a:link, a:visited {
             text-decoration: underline;
           }
           .btn a, .btn a:link, .btn a:visited {
           text-decoration: none !important;
           }
    </style>
    </head>
    <body>
           <div class="btn">
             <a href="http://example.com">Link</a>
           </div>
    </body>
    </html>
    

    The actual output:

    <html><head>
    <title>Title</title>
    <style type="text/css"> a:link {
      text-decoration: underline
    } a:visited {
      text-decoration: underline
    } .btn a:link {
      text-decoration: none !important
    } .btn a:visited {
      text-decoration: none !important
    }</style></head>
    <body>
      <div class="btn">
        <a href="http://example.com" style="text-decoration:none">Link</a>
      </div>
    </body></html>
    

    My expected output HTML:

    <html><head>
    <title>Title</title>
    <style type="text/css"> a:link {
      text-decoration: underline !important
    } a:visited {
      text-decoration: underline !important
    } .btn a:link {
      text-decoration: none !important
    } .btn a:visited {
      text-decoration: none !important
    }</style></head>
    <body>
      <div class="btn">
        <a href="http://example.com" style="text-decoration:none">Link</a>
      </div>
    </body></html>
    

    The difference is I expected a:link { text-decoration: underline } rule (no important), but I got a:link { text-decoration: underline !important } rule (important although it was not in the original html file).

    My question why is that done? Is it done intentionally? Is it a bug? How can I avoid this from happening?

  • .content styles applied to <div class=">

    .content styles applied to

    CSS styles I intended only for a <div class="content"> were also getting applied to a <div class="content-wrap">

    My CSS rule was: .content { padding: 0; }

    Workaround: Name no class a prefix of another (a common pattern though).

  • Import css files

    Import css files

    It would be useful if the package would automatically import local css files. This would really help to keep your source html template clean and to reuse the same style in multiple mail templates.

  • Added a KeepBangImportant option to preserve '!important' in final style attribute.

    Added a KeepBangImportant option to preserve '!important' in final style attribute.

    My final output needed to have margin: 0 auto !important in the style attribute to work properly with the template I was using.

    I added this option and tests to achieve that.

  • Fix rule sorting by specificity

    Fix rule sorting by specificity

    The bug appears only on lists with more than 6 elements due to sort.Sort implementation. For more details refer to quickSort() imlementation in https://golang.org/src/sort/sort.go#L185

  • collect rules from style which has media=

    collect rules from style which has media="all"

    mail generate by https://github.com/matcornic/hermes has media="all" for style, this pr make go-premailer can inline styles mail generated by hermes

  • Fix TestNotSupportedSelectors test case

    Fix TestNotSupportedSelectors test case

    As far as I can understand goquery decided to change the behaviour of their API in case of invalid/unsupported selectors: instead of panic-ing, they return an empty selection. For more details please refer to https://github.com/PuerkitoBio/goquery/commit/2ab590df05344e1c8da3929b4a3b2aefe56705c0

  • `/x/net/http` upgrade breaking change

    `/x/net/http` upgrade breaking change

    I added a test case that breaks when you upgrade golang.org/x/net, which broke the Hermes e-mail templating package downstream.

    This requires a downstream update of goquery, and /x/net/http. I believe that /x/net/http was resolved, but Goquery hasn't upgraded yet https://github.com/PuerkitoBio/goquery/issues/391. After it has, the version here should be bumped too.

  • font-family escaping

    font-family escaping

    Hello guys,

    When I check the generated HTML for classes that contain a font-family the output is:

    <p class="font-family:&#39;Arial&#39;, sans-serif"></p>

    Is this the expected behavior? I'm suprised to see HTML entities &#39; in the middle of a CSS attribute...

    Thanks

Mail-alias-resolve-ldap - Resolve mail alias addresses in LDAP

alias-resolve-ldap alias-resolve-ldap was written to be used as a hook for the c

Jan 30, 2022
Golang package that generates clean, responsive HTML e-mails for sending transactional mail
Golang package that generates clean, responsive HTML e-mails for sending transactional mail

Hermes Hermes is the Go port of the great mailgen engine for Node.js. Check their work, it's awesome! It's a package that generates clean, responsive

Dec 28, 2022
VMail - check the markup (HTML, CSS) of HTML email template compatibility with email clients
VMail - check the markup (HTML, CSS) of HTML email template compatibility with email clients

VMail - check the markup (HTML, CSS) of HTML email template compatibility with email clients Email clients use different rendering standards. This is

Dec 17, 2022
📧 Example of sending mail via SendGrid in Golang.

?? go-sendgrid-example Example of sending mail via SendGrid in Golang. Get it started $ make setup # Edit environment variables $ vim ./env/local.env

Jan 11, 2022
:envelope: A streaming Go library for the Internet Message Format and mail messages

go-message A Go library for the Internet Message Format. It implements: RFC 5322: Internet Message Format RFC 2045, RFC 2046 and RFC 2047: Multipurpos

Dec 26, 2022
Go library for sending mail with the Mailgun API.

Mailgun with Go Go library for interacting with the Mailgun API. Usage package main import ( "context" "fmt" "log" "time" "githu

Dec 25, 2022
✉️ Composable all-in-one mail server.

Maddy Mail Server Composable all-in-one mail server. Maddy Mail Server implements all functionality required to run a e-mail server. It can send messa

Dec 27, 2022
MIME mail encoding and decoding package for Go

enmime enmime is a MIME encoding and decoding library for Go, focused on generating and parsing MIME encoded emails. It is being developed in tandem w

Nov 30, 2022
an MDA that sends a webhook on recieval of mail

an MDA that sends a webhook on recieval of mail

Aug 13, 2022
Filtering spam in mail server, protecting both client privacy and server algorithm

HE Spamfilter SNUCSE 2021 "Intelligent Computing System Design Project" Hyesun Kwak Myeonghwan Ahn Dongwon Lee abstract Naïve Bayesian spam filtering

Mar 23, 2022
Mail_sender - This library is for sending emails from your mail

Mail Sender This library is for sending emails from your mail Installation mail_

Dec 30, 2021
Mcopa - A library allows for parsing an email message into a more convenient form than the net/mail provides

Mail content parsing This library allows for parsing an email message into a mor

Jan 1, 2022
Simple tool to test SMTP mail send with various settings including TLS1.1 downgrade

smtptest Simple tool to test SMTP mail send with various settings including TLS1.1 downgrade All settings are configurable in the config.yaml file ser

Sep 19, 2022
Go-mail - Email service using Kafka consumer

?? The Project This project consists in a Kafka consumer that reads messages of

Feb 5, 2022
Send markdown files as MIME-encoded electronic mail.

Send markdown files as MIME-encoded electronic mail.

Aug 9, 2022
Using Mailchain, blockchain users can now send and receive rich-media HTML messages with attachments via a blockchain address.

Mailchain Introduction Mailchain enables blockchain-based email-like messaging with plain or rich text and attachment capabilities. Using blockchain p

Dec 28, 2022
📮 Simple (but useful) email sender written in pure Go v1.17. Support HTML templates and attachments.

?? Go Email Sender Simple (but useful) email sender written in pure Go v1.17. Yes, yet another email package here! ?? Support HTML templates and attac

Dec 31, 2021
DKIM package for golang

go-dkim DKIM package for Golang Getting started Install go get github.com/toorop/go-dkim Warning: you need to use Go 1.4.2-master or 1.4.3 (when it

Dec 10, 2022
Golang package for send email. Support keep alive connection, TLS and SSL. Easy for bulk SMTP.

Go Simple Mail The best way to send emails in Go with SMTP Keep Alive and Timeout for Connect and Send. IMPORTANT Examples in this README are for v2.2

Jan 8, 2023