Send Emails in Node.js | NodeMailer Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome to this tutorial so today what I'm going to be showing you is how you can send emails in node.js using the node mailer package so I'll be showing you how you can send an email to a single recipient multiple recipients and also how you can include attachments and embedded images so at the top of your script you're going to want to import node mailer using require like I am here and you're also going to need to install the package so you can see there's no package.json or node modules folder here so what you need to do is first of all I've already done it cd'd into my project folder and then I'm going to create a new node project here using npm in it black y so that's going to accept all of the defaults or a new project without me having to hit return many times and then I'm going to install node mailer with npm I node mailer so now this is complete I can go back to my project folder and you now see there's a package.json file listing node mailer as a dependency so we can now start working with node mailer but first what I'm going to do is to create the email content itself and this is in HTML format so I would advise using a template literal to do this stats backticks so for this email I'll just say hello world and below it I'll include a paragraph and a bit later on I'll be adding an image to this when we come on to attachments so now that I've created the HTML for the email what I'm going to do is create a function here that's going to send the email and there's going to be some asynchronous processes that turn a promise inside this function so I'm going to use the async keyword before it so that I can use a weight within the function and I'll call this function Main and I'll be calling this function down here to send the email so inside this function I'm going to call the create transport method on the imported mailer package and what we're going to specify here inside an options object that we pass in is information about the mail server that we're going to be sending the email from so I'm going to be setting this up to work with the mail server connected to a shared Hosting account that I have but you can also use settings for Gmail if you want to test this out so I will post the link to this information in the description below this video so you'll want to use the settings for the outgoing mail server here if you don't have Gmail or a shared Hosting account you're going to need to check the documentation with your email account or the SMTP outgoing mail server settings so the first property to set is the mail server so in my case that's mail dot open JavaScript dot info the port associated with handling the sending of the mail is usually 465 or SMTP so this is sort of a default Port that most servers use and usually it's a secure connection so you want to set the secure property to true and the next thing to do is of course authorize yourself with the mail server that you're using so you do that with a user and a pass property in my case I created a test email test at open JavaScript dot info and the password is node mailer one two three okay so I just add a comma here and now we're done defining the transport settings so I'm going to save a reference to these settings under transporter and then what we're going to do is to call send mail on the transporter asking in another object here where we're actually passing in the email itself and this is an asynchronous process that returns a promise with information about the request whether it was successful or not so we're going to await the information and after that log message sent and you can also include an ID that has been generated or this particular mail so that's available on info message ID now if there is an error in sending the mail then transporter dot send mail is going to throw an error and this console message that we just constructed is not going to be logged so how do we catch the error well it's going to throw an error so we can catch that when we are calling the main function and I just log the error to the console in case there is one and now we're ready to start defining the contents of the email itself so the first property to Define here is from so what you want to include here is the name of the sender and after that the email of the sender so in this case it's from the test email account that I created and also who it's going to so I created another test email account so I'll be sending it to that address you can also include a subject here so I'll say testing testing one two three and finally you want to define the content of the email so that I defined that up above under the reference HTML in the template string so that's all there is to it or sending an email with some HTML content to a single email address so what I'll do now is save what we've got so far and run the app and hopefully that is going to send the email to my email address so this is the ID that it is outputting for this particular mail the fact that we can see it means that it was successful and if I check the email account I was sending it to you see that we've got just a few seconds ago the email you can see that it's printing the markup that we created inside body or the email now the next thing I want to show you is how you would send this email to multiple recipients so you don't need to do much in the code other than I just copy this out so what I'm going to do about is create an array that will include the email addresses that I want to send the mail to so just under the markup I'll create that reference and inside emails I'm going to define the email that we just sent it to and I'll also include an email that doesn't yet exist so S3 I didn't create this email before the tutorial now where we have this console log so it's still going to log a message ID if one of the emails is sent successfully so we would expect the first one to be sent successfully and the second one not to work so this message will still be logged and get the information about which emails were successful and which were not I can log to other bits of information to the console so info accepted and infodel projected and each one of these will return an array that contains the emails for which the sending was successful and the emails which it was done so apart from that I don't need to change anything else so I will run the app once again so as I mentioned because one of the emails was sent successfully we get this first message with the ID up to the console and then in the first array with successful emails we get test two at open javascript.info and in the rejected emails we get test three at open javascript.info so exactly as we would expect okay so the final thing I want to talk about is how you can add attachments so I'll pick these two images here and place those in my project folder and then you define attachments as an array of objects inside the object where you are defining the message itself so for each attachment you define three things the first one is the file name so you can name this if you want I'll just name it the same as it is on my computer the second is the path so in this case it's rather straightforward it's image one dot JPEG and finally if you want to embed it in the email in the case of an image then you want to give it a Content ID so this is simply a unique email like value so the unique value comes first and then I'll set it to open javascript.info and then or the second image I'll do the same but I'm not going to define a CID here because I won't use it in the email itself so to embed the first image in the body of the email I take the content ID and then inside the markup create a new image element with an SRC that starts with CID colon and then the ID there I gave it to it and I'll set a width here 400 so the image isn't too big in the email now I'll run the app one more time okay so that successfully sent to test two again if I open that email address and I'll refresh see that that email came through now and it includes the image one embedded in the body and also both images as attachments so that is it for this tutorial on how you can send emails in node.js using node mailer so I hope you found this tutorial useful if you did please consider hitting the like button down below this video it helps with the algorithm and others to find this video and if you'd like to see more content like this from us in the future don't forget you can subscribe to the channel
Info
Channel: OpenJavaScript
Views: 43,682
Rating: undefined out of 5
Keywords:
Id: L46FwfVTRE0
Channel Id: undefined
Length: 11min 36sec (696 seconds)
Published: Wed Jan 11 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.