SCENE-CE / mirador-multi-user

1 stars 0 forks source link

Add mail support in backend #49

Open geourjoa opened 1 month ago

geourjoa commented 1 month ago

Functional aspect

We want to support basic mail function :

Technical steps

Something like that

import nodemailer from 'nodemailer';

interface MailOptions {
  from: string;
  to: string[]; // Array of strings to support multiple recipients
  cc?: string[]; // Optional array of strings to support multiple CC recipients
  subject: string;
  text?: string;
  html?: string;
}

interface SenderConfig {
  host: string;
  port: number;
  secure: boolean;
  auth: {
    user: string;
    pass: string;
  };
}

async function sendMail(senderConfig: SenderConfig, mailOptions: MailOptions): Promise<void> {
  // Create a transporter object using SMTP transport.
  const transporter = nodemailer.createTransport({
    host: senderConfig.host,
    port: senderConfig.port,
    secure: senderConfig.secure,
    auth: {
      user: senderConfig.auth.user,
      pass: senderConfig.auth.pass,
    },
  });

  // Join multiple recipient addresses into a single string
  const toAddresses = mailOptions.to.join(',');
  const ccAddresses = mailOptions.cc?.join(',');

  // Send mail with defined transport object.
  try {
    const info = await transporter.sendMail({
      ...mailOptions,
      to: toAddresses, // Use the joined string of addresses for "to"
      cc: ccAddresses, // Use the joined string of addresses for "cc"
    });
    console.log('Message sent: %s', info.messageId);
  } catch (error) {
    console.error('Error sending email:', error);
  }
}

// Example sender configuration
const senderConfig: SenderConfig = {
  host: 'smtp.example.com',
  port: 587,
  secure: false,
  auth: {
    user: 'your-email@example.com',
    pass: 'your-email-password',
  },
};

// Example mail options with multiple recipients and CC
const mailOptions: MailOptions = {
  from: '"Sender Name" <sender@example.com>', // Sender address
  to: ['recipient1@example.com', 'recipient2@example.com'], // Multiple recipients
  cc: ['cc1@example.com', 'cc2@example.com'], // Multiple CC recipients
  subject: 'Hello', // Subject line
  text: 'Hello world?', // Plain text body
  html: '<b>Hello world?</b>', // HTML body
};

sendMail(senderConfig, mailOptions).catch(console.error);
interface TemplateParams {
  [key: string]: string;
}

function fillHtmlTemplate(template: string, params: TemplateParams): string {
  let filledTemplate = template;

  for (const [key, value] of Object.entries(params)) {
    const placeholder = `{{${key}}}`;
    filledTemplate = filledTemplate.replace(new RegExp(placeholder, 'g'), value);
  }

  return filledTemplate;
}

// Example HTML template with placeholders
const htmlTemplate = `
  <html>
    <body>
      <h1>{{title}}</h1>
      <p>{{message}}</p>
      <p>Best regards,</p>
      <p>{{senderName}}</p>
    </body>
  </html>
`;

// Example parameters to fill the template
const params: TemplateParams = {
  title: 'Hello World',
  message: 'This is a test message.',
  senderName: 'John Doe',
};

// Fill the template with the parameters
const filledHtml = fillHtmlTemplate(htmlTemplate, params);

console.log(filledHtml);

Ops aspects

(For this part I suggest to ask help of our incredible admin sys guy Elian)