Written by toomaime
Last Updated 🗓 a month ago
How to send a magic link with Nodemailer and Namecheap
For my project Growthhacklist I needed a simple and fast way to send new users a “magic link” after leaving their email address on my landing page. To do so, I hacked together a little recipe with Nodemailer and Namecheap.
As you can see on my landing page I have a little input field where the user can submit an email. After clicking the Start Learning button he will be redirected to the “Create Account” page.
Now in the background my little recipe will start working. To get the "magic link" working you need a namecheap account (in my case, can also be another email provider) and the npm package Nodemailer.
First of all install the npm package and require it in your controller (i am using express js) with
const nodemailer = require('nodemailer');
After that you need the following code which will do the magic.
The var transporter part is for your email provider. For security reasons make sure to put the account details in your environment variables.
In the var emailOptions you need to set up the sending email address. You can then add the subject of your email address and after html: set your welcome message for your first email to the user.
You also need to add the post request somewhere. In my case I added it to my index.js file.
You could also do that in one file but it is cleaner to have an index.js in your routes folder. The only thing you need to do now is to add the code for the form to your frontend. I am using Pug JS but of course you could use also use another template engines.
That's it already. I hope it helps, if you have questions please let me know in the comments section.