Hello,
Im trying to send email with netlify function and Mailgun, i create a function :
require('dotenv').config()
const { MAILGUN_API_KEY, MAILGUN_DOMAIN, MAILGUN_URL, FROM_EMAIL_ADDRESS, CONTACT_TO_EMAIL_ADDRESS } = process.env
const mailgun = require('mailgun-js')({ apiKey: MAILGUN_API_KEY, domain: MAILGUN_DOMAIN, url: MAILGUN_URL })
exports.handler = async(event) => {
if (event.httpMethod !== 'POST') {
return { statusCode: 405, body: 'Method Not Allowed', headers: { 'Allow': 'POST' } }
}
const data = JSON.parse(event.body)
console.log(data)
if (!data.message || !data.contactName || !data.contactEmail) {
return { statusCode: 422, body: 'Name, email, and message are required.' }
}
const mailgunData = {
from: FROM_EMAIL_ADDRESS,
to: CONTACT_TO_EMAIL_ADDRESS,
'h:Reply-To': data.contactEmail,
subject: `New contact from ${data.contactName}`,
text: `Name: ${data.contactName}\nEmail: ${data.contactEmail}\nMessage: ${data.message}`
}
return mailgun.messages().send(mailgunData).then(() => ({
statusCode: 200,
body: "Your message was sent successfully! We'll be in touch."
})).catch(error => ({
statusCode: 422,
body: `Error: ${error}`
}))
}
When i invoke it with :
netlify functions:invoke send-contact-email --port 8888 --no-identity --payload '{"foo":1}'
I always get an empty body from my console.log(data)
:
Request from ::ffff:127.0.0.1: POST /.netlify/functions/send-contact-email
{}
Anyone can try to help me ?
Hey @Tonyweb
I have a function called splat
in which I added the line
console.log(event.body)
I then ran
ntl functions:invoke splat --payload '{"foo":1}'
And saw
Request from ::ffff:127.0.0.1: POST /.netlify/functions/splat
{"foo":1}
Thanks for your reply but i can’t get the same results, more informations below :
I’m using nuxt3
netlify.toml :
[dev]
command = "npm run dev"
[build]
functions = "functions"
console.log(event)
in my function :
{
path: '/.netlify/functions/send-contact-email',
httpMethod: 'POST',
queryStringParameters: {},
multiValueQueryStringParameters: {},
headers: {
'x-forwarded-for': '::ffff:127.0.0.1',
host: 'localhost:8888',
connection: 'close',
'accept-encoding': 'gzip,deflate',
'user-agent': 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)',
'content-length': '2',
accept: '*/*',
'content-type': 'text/plain;charset=UTF-8',
'client-ip': '127.0.0.1'
},
multiValueHeaders: {
'x-forwarded-for': [ '::ffff:127.0.0.1' ],
host: [ 'localhost:8888' ],
connection: [ 'close' ],
'accept-encoding': [ 'gzip,deflate' ],
'user-agent': [ 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)' ],
'content-length': [ '2' ],
accept: [ '*/*' ],
'content-type': [ 'text/plain;charset=UTF-8' ],
'client-ip': [ '127.0.0.1' ]
},
body: '{}',
isBase64Encoded: false,
rawUrl: 'http://localhost:8888/.netlify/functions/send-contact-email',
rawQuery: ''
}
Maybe this can help ?
Tonyweb:
I’m using nuxt3
I don’t believe that should matter. As long as the function is available.
Tonyweb:
Node version : 16.13
This is what I am using.
When I console.log(event)
I see the body data as well. Given the console.log
is showing something, it means the function is invoked.
Did you try without the extra flags as per the command I used?
Have you tried another (basic function) to test?
Have you tried an alternate method such as cURL?
This is the function I used:
exports.handler = async(event) => {
console.log(event)
return {
statusCode: 200,
body: "OK"
}
}
1 Like
tik9
August 31, 2022, 4:48pm
5
@coelmay you put me on the right track. my error was in the payload, I had {foo:‘bar’}, change it to {“fo”:‘ba’} worked.
2 Likes
Thanks for coming back and sharing, @tik9 !