GithubHelp home page GithubHelp logo

nodemailer-plugin-file2inline's Introduction

Nodemailer plugin for converting external images to email attachments

This plugin will convert external images in your nodemailer email to be inline ("CID-referenced") attachments within the email. Inline attachments are useful because they embed the image inside the actual email, so it's viewable even if the user is checking their email without an internet connection.

Local files will not be accessible with html alone. This Nodemailer plugin will take images in your email html in the form:

<img src="path/to/file.jpg">

and replace it with a CID-referenced attachment that works in all email clients.

Install

yarn add nodemailer-plugin-file2inline

or

npm install nodemailer-plugin-file2inline --save

Usage

1. Load the nodemailer-plugin-file2inline plugin:

var file2InlinePlugin = require('nodemailer-plugin-file2inline');

2. Attach it as a 'compile' handler for a nodemailer transport object

//Without options
nodemailerTransport.use('compile', file2InlinePlugin())

//With options
nodemailerTransport.use('compile', file2InlinePlugin({
	cidPrefix: "prefix_",
	htmlFilePath: "path/relative/to/the/html/file",
	maxWidth: 600
))

Options are:

  • cidPrefix: allows to set CID prefix1 {cidPrefix: 'somePrefix_'}, then all inline images will have prefix in cid, i.e.: cid:somePrefix_5fe3b631c651bdb1.
  • htmlFilePath: path to the folder where the html file is, relative to the current working folder
  • maxWidth: if this options is set, all images will have the "width" attribute set to the value

Example

var nodemailer = require('nodemailer');
var file2InlinePlugin = require('nodemailer-plugin-file2inline');

let transporter = nodemailer.createTransport({
    sendmail: true,
    newline: 'unix',
});
transporter.use('compile', file2InlinePlugin({cidPrefix: 'somePrefix_'}));
transporter.sendMail({
    from: '[email protected]',
    to: '[email protected]',
    html: '<img src="external/image.png">'
});

Changelog

  • 1.0.0 Initial release

References

1 It might be useful for reply email processing, example with MailParser

mp.on("attachment", function(attachment, mail){
    if (!attachment.contentId.includes('somePrefix')) { // process only images attached by user in reply
        // ...
    }
});

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.