mailjet / mailjet-apiv3-nodejs

[API v3] Official Mailjet API v3 NodeJS wrapper
https://dev.mailjet.com
MIT License
236 stars 69 forks source link

Cannot send email from Firebase Functions #23

Closed theolof closed 7 years ago

theolof commented 7 years ago

I've started using Firebase Functions: https://firebase.google.com/features/functions/ It's basically a node.js server.

When I try to send an email, I get the following error response:

Error sending email: { Error: Unsuccessful
at /user_code/node_modules/node-mailjet/mailjet-client.js:202:23
at Request.callback (/user_code/node_modules/node-mailjet/node_modules/superagent/lib/node/index.js:631:3)
at Stream.<anonymous> (/user_code/node_modules/node-mailjet/node_modules/superagent/lib/node/index.js:795:18)
at emitNone (events.js:86:13)
at Stream.emit (events.js:185:7)
at Unzip.<anonymous> (/user_code/node_modules/node-mailjet/node_modules/superagent/lib/node/utils.js:112:12)
at emitNone (events.js:91:20)
at Unzip.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickDomainCallback (internal/process/next_tick.js:122:9)
ErrorMessage: 'Internal Server Error',
statusCode: 500,
response: 
Response {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
res: 
IncomingMessage {
_readableState: [Object],
readable: false,
domain: null,
_events: [Object],
_eventsCount: 3,
_maxListeners: undefined,
socket: [Object],
connection: [Object],
httpVersionMajor: 1,
httpVersionMinor: 1,
httpVersion: '1.1',
complete: true,
headers: [Object],
rawHeaders: [Object],
trailers: {},
rawTrailers: [],
upgrade: false,
url: '',
method: null,
statusCode: 500,
statusMessage: 'Access violation',
client: [Object],
_consuming: true,
_dumped: false,
req: [Object],
setEncoding: [Function],
on: [Function],
text: '',
read: [Function],
body: undefined },
request: 
Request {
domain: null,
_events: [Object],
_eventsCount: 1,
_maxListeners: undefined,
_agent: false,
_formData: null,
method: 'POST',
url: 'https://api.mailjet.com/v3/send',
_header: [Object],
header: [Object],
writable: true,
_redirects: 0,
_maxRedirects: 5,
cookies: '',
qs: {},
qsRaw: [],
_redirectList: [],
_streamRequest: false,
_data: [Object],
req: [Object],
protocol: 'https:',
host: 'api.mailjet.com',
_callback: [Function],
res: [Object],
_timeout: 0,
response: [Circular],
called: true },
req: 
ClientRequest {
domain: null,
_events: [Object],
_eventsCount: 4,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
upgrading: false,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: true,
sendDate: false,
_removedHeader: [Object],
_contentLength: 229,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket: [Object],
connection: [Object],
_header: 'POST /v3/send HTTP/1.1\r\nHost: api.mailjet.com\r\nAccept-Encoding: gzip, deflate\r\nuser-agent: mailjet-api-v3-nodejs/3.0.6\r\nContent-type: application/json\r\nAuthorization: Basic N2EyZWIxOGNmM2I4YjExMGE2MGMyY2IzNjhhMjA2ZjA6NNlYmFhODY1MWUyYmViZjkyOGM0NzJmN2ZkYzA4YjY=\r\nContent-Length: 229\r\nConnection: close\r\n\r\n',
_headers: [Object],
_headerNames: [Object],
_onPendingData: null,
agent: [Object],
socketPath: undefined,
timeout: undefined,
method: 'POST',
path: '/v3/send',
_ended: true,
parser: null,
res: [Object] },
links: {},
text: '',
body: {},
files: undefined,
buffered: true,
headers: 
{ server: 'nginx',
date: 'Wed, 15 Mar 2017 07:22:34 GMT',
'content-type': 'text/html',
'content-length': '20',
connection: 'close',
vary: 'Accept-Encoding',
'content-encoding': 'gzip' },
header: 
{ server: 'nginx',
date: 'Wed, 15 Mar 2017 07:22:34 GMT',
'content-type': 'text/html',
'content-length': '20',
connection: 'close',
vary: 'Accept-Encoding',
'content-encoding': 'gzip' },
statusCode: 500,
status: 500,
statusType: 5,
info: false,
ok: false,
redirect: false,
clientError: false,
serverError: true,
error: 
{ Error: cannot POST /v3/send (500)
at Response.toError (/user_code/node_modules/node-mailjet/node_modules/superagent/lib/node/response.js:106:13)
at Response._setStatusProperties (/user_code/node_modules/node-mailjet/node_modules/superagent/lib/node/response.js:183:12)
at new Response (/user_code/node_modules/node-mailjet/node_modules/superagent/lib/node/res

What can I do to fix this? I'm sending emails from the Java API successfully.

Here's the code I'm using to send the email:

const Mailjet = require('node-mailjet').connect(
    functions.config().mj_apikey.public,
    functions.config().mj_apikey.private
);

Mailjet.post('send').request(options).then(() => {
            console.log('Email sent to: ', email);
    }).catch(handleError);
theolof commented 7 years ago

OK, I found out what the problem was.

I had

options['Recipients'] = [email];

which should be

options['Recipients'] = [{'Email': email}];

The error message could be more helpful than "500 Internal Server Error"...