How to Send Emails With Attachments Using Nodemailer | NodeJS | Step By Step Guide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys welcome to octocoder in this video lecture we are going to learn how to send email with multiple images documents and PDF files or any kind of files using node mailer so let's get started so let's initialize the project you know the command and PM in it of the font is good email with attach main we can't write the capital letters here so that's why all small letters keyword author is OCTA coder these are the all normals we already learned that how to create the initialize the project so let's install the node Miller and PM install node Mather and it will add the dependencies in the package.json file C 6.9.2 current version it is the mid 2023 version so now let's create a new file index.js minimize this one so now you know how to import the node Miller module inside the index.js files so I'm gonna importing the node mailer so now let's create the transporter object I'm gonna do the little faster because in the previous video I already explained all the simple email basic code so I'm gonna do the little faster node mailer dot create transport this is the method inside here we provide the service Gmail and then authentication your Gmail ID and the 16 character password if you don't know how to generate this password please go and watch my previous video in that video I already explained every single line of code how to generate 16 character password I already explained in that video so please watch that video and now we are going to create the setup of the email data for that this is the variable name email options you can write anything so now let's create the mail options object basically in this object we just Define from whom you are going to send mail and recipient email address to subject email with attachments testing and then message what you want to send the message let's write welcome to OCTA coder Please Subscribe that's it and now we need to send this email so transporter send mail this is the method here it takes the parameter mail options and the Callback functions basically error or information about this if error occurs we're going to print else if it is successful then we're going to print console.log email sent successfully to so let's take mail options dot 2 and that's it save this file let's run this file now let's see if the code is working fine or not this is very simple you already learned this so that's why I did the a little faster so the command is node index dot Js and see email sent successfully let's check Gmail account we got the email or not and here is the mail we just got it email with attachment testing so this is the normal email which is called it so this simple email you know how to send but you don't know how to send the images PDF files or any kind of files if you want to attach through the email and you want to send the to the other person those files so how to send that files so I'm going to show you now and first here you are just sending the text file you can't edit here so if you want to send the welcome to October in a color format so you can write it as a HTML format you can write H1 tag welcome to October H1 tag is close and then this is a paragraph Please Subscribe coder YouTube channel and let's give the link anchor tag we got the error this error is came because of the blank white spaces so how to remove those space see here we have given the inverted comma so we remove that and give the back tick so you can write it on multiple lines that's why the error games always h r e f and then the link this is OCTA color link and here inside the image tag you can also send the image file like image SRC but here problem is we need to take the online image so let's take the online one of the image online path of the image let's take the OCTA color image any kind of image we have the lot of images so let's take this one copy image address not the link address that's the confusion lot of people get okay let's try the copy link address first and paste it here and now let's run this file we don't need to type the command again and again just press the up here the command will appear and see emails and successfully this is the HTML format so let's check the Gmail we got but image is not came here because the we have copied the link address we didn't copy the image address so now let's copy this image address so now remove the link address first and then paste here and save this file and send the mail again email sends successfully let's check it and see we got the image now images become very large now it's okay but now you understand how to send the image but here problem is only we can take the online images we can take the from your folder or from your laptop images those images not allowed to send through the HTML so we are going to learn how to send the attachment now so here is the another option we have attachments so here we need to create the array object and inside the array we can create multiple objects like this so first file name these are the just names you can write anything file name or just a name so why file name we write it I will tell you why so write for now file name image and the path path I am going to take some of the images from my folder so here let's create a new folder and inside the give the folder name files and inside this folder I am going to add some image file PDF file document file so I'm going to add now foreign folder these are the images and this is the PDF file and this is the document file so how to attach them here so let's give the path of the file image so here you can give absolute part and the relative part but I recommended you to give the relative part one dot because the index file is here so one folder back that is the inside the file and the folder name files and then the image file name is thumbnail and give the extension of the image file PNG and another here you can add the document file give the file name document and the path here you need to give the comma and the path document file path when you're writing the path be careful document is HTML doc dot doc X is the extension of the document and another we can write the PDF file path let's add it we have the HTML all tag PDF so file name so file name let's give the HTML PDF and the path files what is the path of this is the same name you have to write it HTML all tag dot PDF files so that's it guys save this file run this file again let's see email is going to send with the attachment now we have added one image one document and then one PDF file so let's send it now and see email got sent successfully so let's check our Gmail we got it email with attachment let's see yeah we got it the all three files we got it but let's try to open oh we need to download what there is no preview available see no preview available is saying we got the PDF file we got the document and we also got the image file but there is no preview available see why this happen because I will show you now how to make the preview available so here when you're writing the file name you also need to write the extension if it is image then give the extension of the PNG or jpg whatever you want to give and the file is document then give the extension Dock and the file is video give the extension PDF nothing else that's the only we need to change this is the important if you want to see the preview of this file what is this file about so you can write the dot PDF file so now let's run the command again and see emails and successfully let's go back and now we got the preview see we can see the preview now image file and this is the document file so yeah the preview all document also we can see now and this is the PDF file so that's why we need to write the file name with DOT extension don't forget that to give the dot extension about the file name whatever the file is whatever the name you have written here see HTML PDF the name is written on there this is the document so that's why we have written the document so that's it guys so now you know how to send files with attachment guys if you want to give the color for this welcome to October so we can give the color here just need to write a style let's give the color as aqua and now we have two images see we just added one image so let's add another image file name node.js and just don't forget to give the extension of the image if it is image then give the extension PNG and then path files node.js.png that's the correct path and save this file CLS is the command to remove all the commands so we have given the color I want to add some other thing here so I'm going to add H4 tag the our octocoder tagline is learn the way create your own way that is the OCTA color tagline guys you can add the color also let's give the red color save this file and run the file again now we have added two images and see email is same successfully let's go back we just got it see we can see the preview also here plus one is another one file is there available so see we got the heading one color tagline color also got it so this is the anchor tag and see image image preference is image first then document then PDF file so that's it guys that's how to attach the any kind of file PDF file document file images you can send multiple images so that's how to attach the files and if you want to send the multiple people same email then you can write here as a array like example like this you can create here array and comma and then you can write another person email I am going to write it my own email for now is for the demo so you can write here another person email so like that we have written three if you want to send the manager HR then you can write it CC also here and you can give the manager email address whatever so that's how to write the multiple email address and the CC so let's run this file again and see emails and successfully first email address second email address and third email address and here we got the email we got the files and if you click on this down arrow show details and you will get I send myself three times that's why it is not showing because the same address we have sending and see the CC also added here before in the first Mill it wasn't added here CC see you can see here was not CC and here we just added CC let me add the one of my other email address so you will understand I am going to add my other email address Peter dot this is for the temporary address let's check it so you will understand it is sending the multiple people see comma and we got it and let's check the Gmail I will show you yeah see we got another name here now you can see now you understand it is sending the multiple people also we have just seen the two people so that's it guys that's how to send the email with the attachment guys this is just a 30-35 lines code so now you learn and don't worry about this index.js file I'm going to upload this file on the my OCTA coder GitHub Channel or I will give the link of the GitHub in the description so you can download this file I am going to attach this only index.js file other files you know how to create the package.json file and that Etc if you are learning the node.js you know how to do that so that's it guys thank you for watching if you like this video please give the thumbs up and please don't don't forget to subscribe October YouTube channel thank you guys foreign
Info
Channel: Octacoder
Views: 2,094
Rating: undefined out of 5
Keywords:
Id: ytmW68W55yw
Channel Id: undefined
Length: 15min 52sec (952 seconds)
Published: Sun May 28 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.