To send an email with Gmail in JavaScript, you can use the nodemailer library which provides an easy way to send emails using various email services.
Before proceeding, you need to set up an app-specific password in your Gmail account. This is required because Gmail considers the use of a password by third-party apps as less secure than using your main account password. You can find instructions on how to set up an app-specific password in the Gmail Help Center.
Once you have set up the app-specific password, you can use the following code to send an email:
index.tsx815 chars29 lines
In the above code, we are creating a transporter object using nodemailer with the service set to Gmail and adding the Gmail credentials (email address and app-specific password) to the auth
field.
We then setup the email data including from
, to
, subject
and text
(plain text message) and html
(HTML message), which is optional.
Finally, we call the sendMail
function with the mailOptions
object and a callback function to handle errors and success.
gistlibby LogSnag