Skip to content

Instantly share code, notes, and snippets.

@thetutlage
Created May 8, 2018 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thetutlage/706f4a7e062a1085880a9404efe82f12 to your computer and use it in GitHub Desktop.
Save thetutlage/706f4a7e062a1085880a9404efe82f12 to your computer and use it in GitHub Desktop.
Mailgun messages.mime returning 400 with content length header
'use strict'
const nodemailer = require('nodemailer')
const got = require('got')
const FormData = require('form-data')
class MailGunTransporter {
send (mail, callback) {
const form = new FormData()
form.append('to', '<TO_EMAIL>')
form.append('message', mail.message.createReadStream(), { filename: 'message.txt' })
const req = got('https://api.mailgun.net/v3/<YOUR_DOMAIN>/messages.mime', {
body: form,
stream: true,
auth: 'api:<API_KEY>'
})
req.on('request', (req) => {
// Uncommenting the following line will send the email through
// req.removeHeader('content-length')
})
req.on('end', () => {
callback(null)
})
req.on('error', (error) => {
callback(error)
})
}
}
const transporter = nodemailer.createTransport(new MailGunTransporter())
transporter.sendMail({
from: '<FROM_EMAIL>',
to: '<TO_EMAIL>',
subject: 'Hi guys',
text: 'hello world'
}, function (error) {
console.log(error)
})
@thetutlage
Copy link
Author

thetutlage commented May 8, 2018

MIME

Content-Type: text/plain
From: <FROM_EMAIL>
To: <TO_EMAIL>
Subject: Hi guys
Message-ID: <522def0e-b93f-5b93-60ee-8a08924af4d0@dimerapp.com>
Content-Transfer-Encoding: 7bit
Date: Tue, 08 May 2018 08:50:54 +0000
MIME-Version: 1.0

hello world

@thetutlage
Copy link
Author

Curl request

 curl --user 'api:<API_KEY>' \
    https://api.mailgun.net/v3/<DOMAIN>/messages.mime \
    -H "Content-length:300" \
    -F to='<TO>' \
    -F message=@email.mime

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment