Can I send mail through SMTP from a function?

Is it allowed? What are the ports I can use?

Hi, @iwazaru. Netlify doesn’t offer any email service but I can see no reason that a function could not send email using a third-party email service.

If your function contains code to connect to a third-party email server it should be able to send email via SMTP.

Regarding what ports to use, that would be determined by that third-party email service, not by Netlify so we are unable to answer that question.

There are a couple of examples available here: https://github.com/netlify/functions#community-function-examples.

The Sendmail example looked promising to me but is unfortunately slightly unreliable. Like @luke said, using a third party solution is your best bet.

1 Like

Thanks @luke @tomrutgers for your answers.

To clarify, I’ve already successfully sent mail using a third-party module (mailjet-apiv3-nodejs) that uses their API under the hood but I’m looking for a more universal solution was wondering if netlify allow to send mail by connecting directing to a SMTP server through port 25, 465 or 587, using something like nodemailer.

Some hosts block SMTP ports to avoid being used as spam bots or allow only some ports, so I was wondering if there was some restrictions like this at Netlify.

Thanks!

We don’t block any ports with Netlify Functions so you will be able to make outbound connections on those ports but there is a ten second timeout by default which could become an obstacle.

Note, if the ten second timeout does become an issue, that can be increased to a maximum of 28 seconds if the Functions add-on for the site is upgraded to Level 1. (After upgrading please let us know here as the increase of the timeout is a manual step which our support team must perform.)

1 Like