Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

libmime

nodemailer10.4mMIT5.3.6TypeScript support: definitely-typed

Encode and decode quoted printable and base64 strings

MIME, Base64, Quoted-Printable

readme

libmime

libmime provides useful MIME related functions. For Quoted-Printable and Base64 encoding and decoding see libqp and libbase64.

Installation

npm:

npm install libmime

Usage

var libmime = require('libmime');

Methods

Encoded Words

#encodeWord

Encodes a string into mime encoded word format.

libmime.encodeWord(str [, mimeWordEncoding[, maxLength]]) → String
  • str - String or Buffer to be encoded
  • mimeWordEncoding - Encoding for the mime word, either Q or B (default is 'Q')
  • maxLength - If set, split mime words into several chunks if needed

Example

libmime.encodeWord('See on õhin test', 'Q');

Becomes with UTF-8 and Quoted-printable encoding

=?UTF-8?Q?See_on_=C3=B5hin_test?=

#encodeWords

Encodes non ascii sequences in a string to mime words.

libmime.encodeWords(str[, mimeWordEncoding[, maxLength]) → String
  • str - String or Buffer to be encoded
  • mimeWordEncoding - Encoding for the mime word, either Q or B (default is 'Q')
  • maxLength - If set, split mime words into several chunks if needed

#decodeWords

Decodes a string that might include one or several mime words. If no mime words are found from the string, the original string is returned

libmime.decodeWords(str) → String
  • str - String to be decoded

Folding

#foldLines

Folds a long line according to the RFC 5322. Mostly needed for folding header lines.

libmime.foldLines(str [, lineLength[, afterSpace]]) → String
  • str - String to be folded
  • lineLength - Maximum length of a line (defaults to 76)
  • afterSpace - If true, leave a space in the end of a line

Example

libmime.foldLines('Content-Type: multipart/alternative; boundary="----zzzz----"')

results in

Content-Type: multipart/alternative;
     boundary="----zzzz----"

#encodeFlowed

Adds soft line breaks to content marked with format=flowed options to ensure that no line in the message is never longer than lineLength.

libmime.encodeFlowed(str [, lineLength]) → String
  • str Plaintext string that requires wrapping
  • lineLength (defaults to 76) Maximum length of a line

#decodeFlowed

Unwraps a plaintext string in format=flowed wrapping.

libmime.decodeFlowed(str [, delSp]) → String
  • str Plaintext string with format=flowed to decode
  • delSp If true, delete leading spaces (delsp=yes)

Headers

#decodeHeader

Unfolds a header line and splits it to key and value pair. The return value is in the form of {key: 'subject', value: 'test'}. The value is not mime word decoded, you need to do your own decoding based on the rules for the specific header key.

libmime.decodeHeader(headerLine) → Object
  • headerLine - Single header line, might include linebreaks as well if folded

#decodeHeaders

Parses a block of header lines. Does not decode mime words as every header might have its own rules (eg. formatted email addresses and such).

Return value is an object of headers, where header keys are object keys and values are arrays.

libmime.decodeHeaders(headers) → Object
  • headers - Headers string

#parseHeaderValue

Parses a header value with key=value arguments into a structured object. Useful when dealing with content-type and such. Continuation encoded params are joined into mime encoded words.

parseHeaderValue(valueString) → Object
  • valueString - a header value without the key

Example

parseHeaderValue('content-type: text/plain; CHARSET="UTF-8"');

Outputs

{
    "value": "text/plain",
    "params": {
        "charset": "UTF-8"
    }
}

#buildHeaderValue

Joins structured header value together as 'value; param1=value1; param2=value2'

buildHeaderValue(structuredHeader) → String
  • structuredHeader - a header value formatted with parseHeaderValue

filename argument is encoded with continuation encoding if needed

#buildHeaderParam

Encodes and splits a header param value according to RFC2231 Parameter Value Continuations.

libmime.buildHeaderParam(key, str, maxLength) → Array
  • key - Parameter key (eg. filename)
  • str - String or an Buffer value to encode
  • maxLength - Maximum length of the encoded string part (not line length). Defaults to 50

The method returns an array of encoded parts with the following structure: [{key:'...', value: '...'}]

Example

libmime.buildHeaderParam('filename', 'filename õäöü.txt', 20);
→
[ { key: 'filename*0*', value: 'utf-8\'\'filename%20' },
  { key: 'filename*1*', value: '%C3%B5%C3%A4%C3%B6' },
  { key: 'filename*2*', value: '%C3%BC.txt' } ]

This can be combined into a properly formatted header:

Content-disposition: attachment; filename*0*=utf-8''filename%20
  filename*1*=%C3%B5%C3%A4%C3%B6; filename*2*=%C3%BC.txt

MIME Types

#detectExtension

Returns file extension for a content type string. If no suitable extensions are found, 'bin' is used as the default extension.

libmime.detectExtension(mimeType) → String
  • mimeType - Content type to be checked for

Example

libmime.detectExtension('image/jpeg') // returns 'jpeg'

#detectMimeType

Returns content type for a file extension. If no suitable content types are found, 'application/octet-stream' is used as the default content type

libmime.detectMimeType(extension) → String
  • extension Extension (or filename) to be checked for

Example

libmime.detectExtension('logo.jpg') // returns 'image/jpeg'

License

MIT

changelog

Changelog

5.3.6 (2024-11-29)

Bug Fixes

  • deps: Bumped libqp to fix issue with missing whitespace (1137a9f)

5.3.5 (2024-04-12)

Bug Fixes

5.3.4 (2024-02-23)

Bug Fixes

  • deploy: fixed git repo url (0f3e5f4)
  • deploy: fixed git repo url (4d584ad)

5.3.3 (2024-02-23)

Bug Fixes

  • deploy: fixed git repo url (f8f788f)

5.3.2 (2024-02-23)

Bug Fixes

  • deploy: fixed git repo url (2161a43)

5.3.1 (2024-02-23)

Bug Fixes

  • deploy: include package-lock.json (8363bf4)

5.3.0 (2024-02-23)

Features

  • deploy: added autopublish (3e8109f)

v5.2.1 2023-01-27

  • Fix base64 encoding for emoji bytes in encoded words

v5.2.0 2022-12-08

  • Bumped libqp to get rid of new Buffer warnings

v5.1.0 2022-04-28

  • Bumped deps
  • Removed Travis config
  • Added Github actions file to run tests

v5.0.0 2020-07-22

  • Removed optional node-iconv support
  • Bumped dependencies
  • Updated Travis test matrix, dropped Node 8

v4.2.1 2019-10-28

  • Replace jconv with more recent encoding-japanese

v4.2.0 2019-10-28

  • Use jconv module to parse ISO-2022-JP by default

v4.1.4 2019-10-28

  • decodeWords should also decode empty content part WeiAnAn
  • fix decode base64 ending with = WeiAnAn

v4.1.0 2019-05-01

  • Experimental support for node-iconv

v4.0.1 2018-07-24

  • Maintenance release. Bumped deps

v4.0.0 2018-06-11

  • Refactored decoding of mime encoded words and parameter continuation strings

v3.0.0 2016-12-08

  • Updated encoded-word generation. Previously a minimal value was encoded, so it was possible to have multiple encoded words in a string separated by non encoded-words. This was an issue with some webmail clients that stripped out the non-encoded parts between encoded-words so the updated method uses wide match by encoding from the first word with unicode characters to the last word. "a =?b?= c =?d?= e" -> "a =?bcd?= e"

v2.1.3 2016-12-08

  • Revert dot as a special symbol

v2.1.2 2016-11-21

  • Quote special symbols as defined in RFC (surajwy)

v2.1.1 2016-11-15

  • Fixed issue with special symbols in attachment filenames

v2.1.0 2016-07-24

  • Changed handling of base64 encoded mime words where multiple words are joined together if possible. This fixes issues with multi byte characters getting split into different mime words (against the RFC but occurs)

v2.0.3 2016-02-29

  • Fixed an issue with rfc2231 filenames

v2.0.2 2016-02-11

  • Fixed an issue with base64 mime words encoding

v2.0.1 2016-02-11

  • Fix base64 mime-word encoding. Final string length was calculated invalidly

v2.0.0 2016-01-04

  • Replaced jshint with eslint
  • Refactored file structure

v1.2.1 2015-10-05

Added support for emojis in header params (eg. filenames)

v1.2.0 2015-10-05

Added support for emojis in header params (eg. filenames)

v1.1.0 2015-09-24

Updated encoded word encoding with quoted printable, should be more like required in https://tools.ietf.org/html/rfc2047#section-5

v1.0.0 2015-04-15

Changed versioning scheme to use 1.x instead of 0.x versions. Bumped dependency versions, no actual code changes.

v0.1.7 2015-01-19

Updated unicode filename handling – only revert to parameter continuation if the value actually includes non-ascii characters or is too long. Previously filenames were encoded if they included anything besides letters, numbers, dot or space.

v0.1.6 2014-10-25

Fixed an issue with encodeWords where a trailing space was invalidly included in a word if the word ended with an non-ascii character.

v0.1.5 2014-09-12

Do not use quotes for continuation encoded filename parts. Fixes an issue with Gmail where the Gmail webmail keeps the charset as part of the filename.