0.0.1 • Published 9 years ago

img2attach v0.0.1

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

img2attach

Replace images urls from html email content to content-id resources links

Send beautiful HTML emails without external images ;)

Install

npm install img2attach --save

Usage

Simple

var img2attach = require('img2attach');

var html = '<img src="http://cs624028.vk.me/v624028538/276f5/2ASEewrS9xk.jpg">';

img2attach(html).then(function (data) {
  console.log('html', data.html);
  console.log('attachments', data.attachments);
  
}).fail(function (err) {
  console.log('error', err);
});

With nodemailer

var img2attach = require('img2attach');
var nodemailer = require('nodemailer');

var html = '<img src="http://cs624028.vk.me/v624028538/276f5/2ASEewrS9xk.jpg">';

var transporter = nodemailer.createTransport({
    service: 'Gmail',
    auth: {
        user: 'user',
        pass: 'pass'
    }
});

img2attach(html)
    .then(function (data) {
        console.log('html', data.html);

        var mailOptions = {
            to: 'serge.dmitriev@gmail.com', 
            subject: 'Hello',
            html: data.html,
            attachments: data.attachments
        };
        
        transporter.sendMail(mailOptions, function (error, info) {      
            console.log('Message sent: ' + info.response);
        });
    })
    .fail(function (err) {
        console.log('fail', err);
    });

API

img2attach(html)

html - html string like

<p><img src="1.jpg"></p>

return promise (used Q)

img2attach(html).then(function(data) {})

data - javascript object

{
  html: '<img src="cid:1">',      //cid - url to content-id attachment
  attachments: [
    { 
      cid: 1,
      src: '1.png',
      encoding: 'base64',
      content: 'encoded_data',   //base64 encoded data
      contentType: 'image/jpg' 
    } 
  ]
};

RFC

https://www.ietf.org/rfc/rfc2392.txt