Advertisement
metalni

NodeMailer example

Sep 5th, 2022 (edited)
1,117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const nodemailer = require("nodemailer");
  2.  
  3. const sendEmail = ({ recepient, subject, body, attachments }) => {
  4.   const { EMAIL_ADDRESS,EMAIL_PASSWORD } = process.env;
  5.  
  6.   let transportOptions = {
  7.     host: 'smtp.example.com',
  8.     port: 587,
  9.     secure: false,
  10.     requireTLS: true,
  11.     tls: {
  12.       rejectUnauthorized: false
  13.     },
  14.     auth: {
  15.       user: EMAIL_ADDRESS,
  16.       pass: EMAIL_PASSWORD
  17.     }
  18.   }
  19.  
  20.   let transporter = nodemailer.createTransport(transportOptions);
  21.  
  22.   const mailOptions = {
  23.     from: EMAIL_ADDRESS,
  24.     to: recepient,
  25.     subject,
  26.     html: body,
  27.     attachments: attachments || null
  28.   };
  29.  
  30.   return transporter.sendMail(mailOptions);
  31. };
  32.  
  33. module.exports = sendEmail;
  34.  
  35.  
  36. // #######
  37. // CLASS IMPLEMENTATION
  38.  
  39. class EmailService {
  40.   constructor(emailOpts = { recepient: '', attachments: [] }) {
  41.     this.emailOpts = emailOpts
  42.   }
  43.  
  44.   sendRegisterConfirmationLink(opts = { confirmationLink: '', recepient: '', body: '' }) {
  45.     return sendEmail({
  46.       ...this.emailOpts,
  47.       recepient: opts.recepient,
  48.       subject: 'Confirm your account',
  49.       body: opts.body || `<p>Click on the link below to confirm your account: </p> <a href="${opts.confirmationLink}">Confirm account</a>`
  50.     })
  51.   }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement