DEV Community

Cover image for Sending Email with Netlify Functions

Sending Email with Netlify Functions

Jenna Pederson on November 04, 2019

Functions, lambdas, serverless. Maybe you've heard of these. Maybe you haven't. These words are being used to describe a different way of connectin...
Collapse
 
marieqg profile image
marieqg

Hello Jenna,
I got stuck at the first step :(
when I do : "netlify functions:invoke send-contact-email --no-identity"
I get the following error :

$ netlify functions:invoke send-contact-email --no-identity
ran into an error invoking your function
{ FetchError: request to http://localhost:8888/.netlify/functions/send-contact-email failed, reason: connect ECONNREFUSED 127.0.0.1:8888
at ClientRequest.<anonymous> (/usr/local/lib/node_modules/netlify-cli/node_modules/node-fetch/lib/index.js:1455:11)
at ClientRequest.emit (events.js:198:13)
at Socket.socketErrorListener (_http_client.js:392:9)
at Socket.emit (events.js:198:13)
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
message:
'request to http://localhost:8888/.netlify/functions/send-contact-email failed, reason: connect ECONNREFUSED 127.0.0.1:8888',
type: 'system',
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED' }

Any tips on how to get ride of this error?

Collapse
 
jennapederson profile image
Jenna Pederson

Hi Marie -

The ECONNREFUSED error indicates that it can't connect to where the function is hosted (in this case localhost:8888). Make sure that you have netlify dev running in another command line window so that the functions are accessible.

Hope that helps!

Collapse
 
quantuminformation profile image
Nikos

When I run netlify dev I get


  Your application is ready~! 🚀

  ➡ Port 5000 is taken; using 58057 instead

  - Local:      http://localhost:58057

────────────────── LOGS ──────────────────

.......

and when running netlify functions:invoke send-contact-email --no-identity



ran into an error invoking your function
FetchError: request to http://localhost:8888/.netlify/functions/send-contact-email failed, reason: connect ECONNREFUSED 127.0.0.1:8888
    at ClientRequest.<anonymous> (/Users/nikos/.nvm/versions/node/v12.13.1/lib/node_modules/netlify-cli/node_modules/node-fetch/lib/index.js:1455:11)
    at ClientRequest.emit (events.js:210:5)
    at Socket.socketErrorListener (_http_client.js:406:9)
    at Socket.emit (events.js:210:5)
    at emitErrorNT (internal/streams/destroy.js:92:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  message: 'request to http://localhost:8888/.netlify/functions/send-contact-email failed, reason: connect ECONNREFUSED 127.0.0.1:8888',
  type: 'system',
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED'```

Thread Thread
 
stevealee profile image
SteveALee • Edited

Did you resolve this? I'm getting exactly the same issue trying to call a simple function using netlify dev. Have spent ages on it.
I used 'netlify.toml' to define functions="functions/"

Is this possibly a WIndows issue?

Thread Thread
 
jennapederson profile image
Jenna Pederson

Hi Steve - Did you get this ironed out? What's the error message you're seeing?

If it's the same as the one that Marie ran into

reason: connect ECONNREFUSED 127.0.0.1:8888

then make sure that you've started up your app with netlify dev first.

If it is started, check to see what port is being used. You should see a message that the server has started. There are 3 that are started - the app, the lambda server where your functions run, and a proxy server (which is a proxy to both of those), so be sure to look for the one that is for the proxy server. The message looks like this:

Proxy server

If that port is NOT 8888, then the netlify functions:invoke call will fail since it's looking for functions on port 8888.

It doesn't look like there is an option in the command line to specify a port here, so there are a couple of options: 1) Figure out what is using port 8888 and stop it so that this will work or 2) Add the following to your [dev] block in your netlify.toml file:

port = 4444 # Port that the proxy server will listen on

This will force it to use the port you tell it to use and netlify functions:invoke will also use that port.

Make sure to change 4444 to an unused port on your system.

Thread Thread
 
stevealee profile image
SteveALee

Actually, it seems the lastest netlify-cli is broken. After a lot of head scratching I found this issue and intalled an old version and the problem went away

github.com/netlify/cli/issues/659

Thanks for answering so quickly

Thread Thread
 
jennapederson profile image
Jenna Pederson

Oh bummer! Glad you got it sorted though.

Collapse
 
marieqg profile image
marieqg

Thanks for your quick reply. I had the netlify dev running in another command line, that's why I don't understand ^

Collapse
 
thetechnaddict profile image
thetechnaddict

My error is: Function invocation failed: [object Object] - without the payload and with it... ummm

Collapse
 
thetechnaddict profile image
thetechnaddict • Edited

agh, yes - install the dependencies, well done genius - now I'm forbidden, but definitely getting there!!

I'm loving the tutorial - it is super helpful, Jenna, thanks

Collapse
 
jennapederson profile image
Jenna Pederson

Thanks! Glad you got it ironed out.

Thread Thread
 
thetechnaddict profile image
thetechnaddict

Sadly not yet - I'm now forbidden - I can connect and send using curl but the js version is rejected.

Here is the function paired down to basics with hard-coded everything just to get mailgun working

const mailgun = require('mailgun-js');

exports.handler = async (event) =>
{
const mg = mailgun({
apiKey: "key-exampleofverylongsecretkey",
domain: "mg.example.com",
url: "api.eu.mailgun.net/v3/mg.example.com"
});

const data = {
from: 'Name mailgun@mg.example.com',
to: 'bored@otherexample.com',
subject: 'Worldish',
text: 'Hello W',
html: 'HTML'
};

return mg.messages().send(data).then(() => ({
statusCode: 200,
body: "Your message was sent successfully! We'll be in touch."
})).catch(error => ({
statusCode: 422,
body: Error: ${error}
}));
}




ps - I've tried it with 'key-exampleofsecretcode' and 'exampleofsecretcode' and with and without the url and with the url with and without /messages - same result for all
Thread Thread
 
thetechnaddict profile image
thetechnaddict

I went back to testing with the sandbox and the email sent - but ended up in spam. -which could be reasonable since it claims to be from one domain and has been sent by another. - I guess I just have to test it on the server

Thread Thread
 
thetechnaddict profile image
thetechnaddict

If I hardcode sandbox secrets into the file it sends happily from the localhost. If I then switch to using dotenv it fails suggesting that require('dotenv').config() isn't loading up process.env

Thread Thread
 
thetechnaddict profile image
thetechnaddict

Reinstalling dotenv has sorted the problem

Collapse
 
timtrautman profile image
Tim Trautman

Great post!

I've loved playing around with Netlify functions -- it makes working with AWS lambda so much more approachable (for me, at least.) And it's neat to see how quickly it lets you build out small apps with backend functionality (like email!) without having to worry about the operations of standing up a backend server.

But, on the flip side, have you run into any difficulties with them? I've found debugging to be a bit more difficult and time-consuming, but that might subside as I become more familiar with them and as netlify dev gets a bit more mature.

Collapse
 
jennapederson profile image
Jenna Pederson

Thanks for the feedback, Tim!

Such a great question too. I have one live project (other than this playground) using a few functions and with not much traffic, so I haven't had a lot of experience with where it falls down. One thing I found (for that particular implementation) was that since there was not much traffic hitting those functions, they would occasionally fail/timeout as they were spinning up. Another use case I've heard they are not well suited for is long-running processes because of this low timeout value.

I started digging into this because I wanted to learn more about that specifically so that I can make better decisions on whether to go down this route for future projects. Stay tuned for a future post on!

And I'm all ears on where you find it tricky or where it doesn't work well, beyond the debugging process.

Collapse
 
jeffwscott profile image
Jeff Scott

netlify functions:invoke send-contact-email --no-identity --payload '{"contactEmail" : "jenna@example.com", "contactName" : "Jenna", "message" : "hello world from a function!"}'

this produces this error:

Error: Sandbox subdomains are for test purposes only. Please add your own domain or add the address to authorized recipients in Account Settings.

Collapse
 
jennapederson profile image
Jenna Pederson

Hey Jeff -

Based on that error, you'll either have to add the email address you're using in the CONTACT_TO_EMAIL_ADDRESS env var to the authorized recipients in your Mailgun Account Settings (see more about that here) or move off the sandbox domain to your very own custom domain.

Hope that helps!

Collapse
 
quantuminformation profile image
Nikos

Is yarn really necessary in 2019?

Collapse
 
thetechnaddict profile image
thetechnaddict

I seem to be getting away with using npm - but I'm coming late to the party :-)