Custom source
When you already have a fully‑formatted RFC 822/EML message—perhaps because it was composed elsewhere or fetched from storage—you can hand it to Nodemailer verbatim with the raw option. Nodemailer will bypass its normal MIME generation and deliver exactly what you supply.
You can use raw at three levels:
- Whole message — supply a single RFC 822 document.
- Per alternative — supply a
text/plain
,text/html
, or other MIME alternative that you built yourself. - Per attachment — supply an attachment body, complete with its own headers.
Always set an envelope
When you use raw for the entire message, you must also set envelope.from
and envelope.to
so that the SMTP transaction has the correct sender and recipients. These values are not extracted from the raw source.
Examples
1. String as the entire message
const message = {
envelope: {
from: "sender@example.com",
to: ["recipient@example.com"],
},
raw: `From: sender@example.com
To: recipient@example.com
Subject: Hello world
Hello world!`,
};
Nodemailer will normalise new‑lines for you, so plain
\n
is fine.
2. EML file as the entire message
const message = {
envelope: {
from: "sender@example.com",
to: ["recipient@example.com"],
},
raw: {
path: "/path/to/message.eml", // absolute or relative to process.cwd()
},
};
3. String as an attachment
When raw is used inside attachments[]
, include all of the MIME headers yourself. Nodemailer does not add Content‑Type
, Content‑Disposition
, or any other headers.
const message = {
from: "sender@example.com",
to: "recipient@example.com",
subject: "Custom attachment",
attachments: [
{
raw: `Content-Type: text/plain
Content-Disposition: attachment; filename="notes.txt"
Attached text file`,
},
],
};