In this article, we are going to implement NestJS sending emails with file attachments.
Part-1 NestJS Send Plain Text Email
Part-2 NestJS Send Dynamic Generated HTML Template As Email
Part-1 NestJS Send Plain Text Email
Part-2 NestJS Send Dynamic Generated HTML Template As Email
Install @types/nodemailer Package:
Let's install the typescript nodemailer package.
npm install --save-dev @types/nodemailer
Endpoint To Send Email With File Attachment:
Let's add a sample image file into the 'mail' folder that we are going to attach to the email.
Let's create an endpoint to send the email with a file attachment.
src/email.controller.cs:
import { join } from 'path'; @Get('file-attachment') async fileAttachement(@Query('toemail') toemail){ var response = await this.mailService.sendMail({ to: toemail, from: 'nani.bommidi93@gmail.com', subject: 'File Attachment', html: "<h1>File Attachment</h1>", attachments:[{ path: join(__dirname,'mails','bike-1.webp'), filename:'1.webp', contentDisposition:"attachment" }] }); return 'success'; }
- (Line: 10-14) File attachment array where we have to provide the path of the file, name, and content disposition.
Support Me!
Buy Me A Coffee
PayPal Me
Video Session:
Wrapping Up:
Hopefully, I think this article delivered some useful demo on sending simple plain email from the NestJS application. using I love to have your feedback, suggestions, and better techniques in the comment section below.
Refer:
Part-1 Email Sending In NestJS Application
Part-2 Generate Dynamic Email Template And Send Email In The NestJS Application
Source Code
Part-2 Generate Dynamic Email Template And Send Email In The NestJS Application
Source Code
Comments
Post a Comment