Created
May 8, 2018 06:41
-
-
Save thetutlage/706f4a7e062a1085880a9404efe82f12 to your computer and use it in GitHub Desktop.
Mailgun messages.mime returning 400 with content length header
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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) | |
}) |
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
MIME