Git Product home page Git Product logo

mail2pdf's Introduction

mail2pdf

Node.js library to render emails stored as .eml files to PDF.

An example email converted using mail2pdf. On the left, the .eml file is opened in Thunderbird, on the right, the result PDF is opened. The test email was created using Microsoft Outlook and contains complex formatting like tables and images. The conversion was successful.

With mail2pdf, you can convert emails stored as .eml files (as exported by Thunderbird, for example) to PDF files from Node.js. The goal is to produce an output that closely matches what an actual email program would export when using the "Print to PDF" feature. This can be useful for archival purposes, for example.

To produce the best possible output, mail2pdf deliberately relies on the rather heavy Puppeteer library which spawns an entire headless Chromium instance, instead of choosing another HTML to PDF solution implemented natively in JS.

The actual implementation is rather simple, it is mostly a thin wrapper around Puppeteer and Nodemailer's Mailparser. The library supports internationalization and using different output templates. Currently, one template is provided that imitates Thunderbird's PDF output.

Installation

You can install mail2pdf using yarn or npm:

yarn add mail2pdf
# or `npm i mail2pdf`

Example usage

To demonstrate how to use mail2pdf, this code shows how to convert a sample .eml file to PDF. The email was sent using Microsoft Outlook and contains complex formatting like tables and images.

import mail2pdf from 'mail2pdf';
import { promises as fs } from 'fs';

(async () => {
  // Simply call the `mail2pdf()` function with the email (or
  // emails as an array) you want to convert. You can also
  // pass additional options, see below for details.
  const pdf = await mail2pdf('sample.eml', { language: 'en' });
  // The resulting PDF is returned as a `Buffer` that you can
  // save to disk for example.
  await fs.writeFile('sample.pdf', pdf);
})();

You can view the conversion result here.

API

Only a single function is provided with the following interface:

mail2pdf(eml, options?)

Parameters:

  • eml - The email(s) in .eml format to process. You can provide a single email or multiple ones as an array. For each one, either provide a file path as a string, which will then be read from the filesystem, or the content of the .eml file as a Buffer or Stream.
  • options - An optional object with the following properties:
    • language - The language to be used for the labels in the generated PDF (optional, defaults to en). Currently, English (en) and German (de) are supported.
    • template_name - The template to use (optional, defaults to thunderbird). Currently, only thunderbird is supported which will generate output similar to Thunderbird’s “Print to PDF” feature.

returns: Promise<Buffer | Buffer[]> - The generated PDF(s) contained in a Buffer. A single Buffer if you provided a single email for the eml parameter, or an array of Buffers if you provided multiple ones.

License

mail2pdf is licensed under the MIT license, see the LICENSE file for details.

Issues and pull requests are welcome! If you find an email that doesn't render correctly, please let me know. To help reproduce the problem, it would be great if you could attach the .eml file in question to the issue.

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.