{"id":5035,"date":"2023-04-22T14:33:00","date_gmt":"2023-04-22T19:33:00","guid":{"rendered":"https:\/\/www.incredigeek.com\/home\/?p=5035"},"modified":"2023-05-13T01:00:17","modified_gmt":"2023-05-13T06:00:17","slug":"send-an-email-with-node-js","status":"publish","type":"post","link":"https:\/\/www.incredigeek.com\/home\/send-an-email-with-node-js\/","title":{"rendered":"Send an Email with Node.JS"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2023\/04\/image-10.png\"><img loading=\"lazy\" decoding=\"async\" width=\"991\" height=\"766\" src=\"https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2023\/04\/image-10.png\" alt=\"\" class=\"wp-image-5036\" srcset=\"https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2023\/04\/image-10.png 991w, https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2023\/04\/image-10-300x232.png 300w, https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2023\/04\/image-10-768x594.png 768w, https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2023\/04\/image-10-388x300.png 388w\" sizes=\"auto, (max-width: 991px) 100vw, 991px\" \/><\/a><\/figure>\n\n\n\n<p>In this post, we will be using Node.JS and the nodemailer library to send email.  We need to have an email account with an email provider to send email.  Gmail or some other email provider should work.  <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites <\/h2>\n\n\n\n<p>First lets install some tools<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt install nodejs npm<\/pre>\n\n\n\n<p>Now lets install nodemailer<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">npm install nodemailer<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Writing the Code to Send Email<\/h2>\n\n\n\n<p>Now that we have nodemailer installed, we can write or copy our code.  Create a file called maill.js and make it look similar to the following.<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color\"><code lang=\"javascript\" class=\"language-javascript line-numbers\">\/\/ We can pass in the email text as an argument\nconst emailText = process.argv.slice(2);\n\/\/ Or we can just have it as a variable\n\/\/ const emailText = \"NodeJS test email message.\"\nconsole.log(\"args \" + args)\n\nconst nodemailer = require(\"nodemailer\");\n\nconst transporter = nodemailer.createTransport({\n  host: \"mail.emailserver.com\",\n  port: 465,    \/\/  If your email server does not support TLS, change to 587\n  secure: true, \/\/ If you are using port 587, change to false.  Upgrade later with STARTTLS\n  auth: {\n    user: \"smtpuser@emailserver.com\",\n    pass: \"notpassword)\",\n  },\n});\n\nconst mailOptions = {\n  from: 'user@emailserver.com',\n  to: \"touser@email.com\",\n  subject: 'Test Email using NodeJS',\n  text: `${emailText}`\n};\n\ntransporter.sendMail(mailOptions, function(error, info){\n  if (error) {\n    console.log(error);\n  } else {\n    console.log('Email sent: ' + info.response);\n  }\n});<\/code><\/pre>\n\n\n\n<p>Update the following variables<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>host: to your host email server<\/li>\n\n\n\n<li>user: to the email user that is sending email.  It should have an account on the email server<\/li>\n\n\n\n<li>pass: password for your email user that is sending the email<\/li>\n\n\n\n<li>from: email address that is sending the email<\/li>\n\n\n\n<li>to: email account(s) you are sending email to<\/li>\n\n\n\n<li>subject: subject of your email <\/li>\n<\/ul>\n\n\n\n<p>Now we can proceed to send email<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Sending Email<\/h2>\n\n\n\n<p>We can now run the code by saving our file and running it directly with NodeJS<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">nodejs .\/mail.js \"This is the body text for the email\"<\/pre>\n\n\n\n<p>Hit Return and look for the email.  If something went wrong, it should throw an error.<\/p>\n\n\n\n<p>You can change the emailText variable if you would rather have the message body inside the code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Code Explanation and Notes<\/h2>\n\n\n\n<p>A little explanation on the code. <\/p>\n\n\n\n<p>The second line &#8220;const emailText = process.argv.slice(2);&#8221; is used to pass in a command line argument to use as the text for the body of the email.  You can delete the line and uncomment line 4 if you would rather use a variable inside the code.<\/p>\n\n\n\n<p>Your email server should support using SSL\/TLS on port 465.  If it does not, you may need to use STARTTLS which uses port 587, and then set secure to false.  STARTTLS should upgrade the connection to be encrypted.  But it&#8217;s opportunistic.  You can read more about STARTTLS, SSL\/TLS here <a href=\"https:\/\/mailtrap.io\/blog\/starttls-ssl-tls\/\">https:\/\/mailtrap.io\/blog\/starttls-ssl-tls\/<\/a><\/p>\n\n\n\n<p>You can change the &#8220;to: &#8221; in the mailOptions object to an array of email addresses to send the email to multiple people at once.  <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">to: [\"email1@email.com\", \"email2@email.com\", \"etc\"],<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post, we will be using Node.JS and the nodemailer library to send email. We need to have an email account with an email provider to send email. Gmail or some other email provider should work. Prerequisites First lets &hellip; <a href=\"https:\/\/www.incredigeek.com\/home\/send-an-email-with-node-js\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1460,792],"tags":[644,223,1437,1463,1470,1471,606,1472],"class_list":["post-5035","post","type-post","status-publish","format-standard","hentry","category-javascript","category-programming","tag-code","tag-email","tag-javascript","tag-js","tag-nnodejs","tag-node","tag-programming","tag-smtp"],"_links":{"self":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5035","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/comments?post=5035"}],"version-history":[{"count":3,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5035\/revisions"}],"predecessor-version":[{"id":5132,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5035\/revisions\/5132"}],"wp:attachment":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/media?parent=5035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/categories?post=5035"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/tags?post=5035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}