If you want to use your own custom generated RFC822 formatted message source, instead of letting Nodemailer to generate it from the message options, use option raw. This can be used both for the entire message or alternatively per-attachment or per-alternative.
This example loads the entire message source from a string value. You don’t have to worry about proper newlines, these are handled by Nodemailer.
let message = {
envelope: {
from: 'sender@example.com',
to: ['recipient@example.com']
},
raw: `From: sender@example.com
To: recipient@example.com
Subject: test message
Hello world!`
};
This example loads the entire message source from a file
let message = {
envelope: {
from: 'sender@example.com',
to: ['recipient@example.com']
},
raw: {
path: '/path/to/message.eml'
}
};
When using raw for attachments then you need to provide all content headers youself, Nodemailer does not process it in any way (besides newline processing), it is inserted into the MIME tree as is.
let message = {
from: 'sender@example.com',
to: 'recipient@example.com',
subject: 'Custom attachment',
attachments: [{
raw: `Content-Type: text/plain
Content-Disposition: attachment
Attached text file`}]
};