import * as QRCode from "qrcode";
import * as Nodemailer from "nodemailer";

export const sendQRToMail = async (dataString: string) => {
  //created dataURl QR string
  let dataUrl = await QRCode.toDataURL(`${dataString}`);

  // separated dataURL string into format and base64 string
  let img = dataUrl.split(",");

  try {
    let transporter = await Nodemailer.createTransport({
      host: "smtp.gmail.com",
      port: 587,
      secure: false,
      auth: {
        user: "ponechal.test@gmail.com",
        pass: "inQool_2096",
      },
    });

    await transporter.sendMail({
      from: '"Mikuláš Test" <ponechal.mikulas@gmail.com',
      to: "ponechal.mikulas@gmail.com",
      subject: "hello",
      attachments: [
        {
          filename: "qrcode.png",
          // sending base64 format string
          content: img[1],
          encoding: "base64",
        },
      ],
    });
  } catch (e) {
    console.log("An error has occured:", e);
  }
};
