sendEmail.js 5.56 KB
import nodemailer from "nodemailer";

export default function (req, res) {
    const transporter = nodemailer.createTransport({
        host: "smtp.gmail.com",
        port: 587,
        secure: false,
        auth: {
            user: process.env.EMAIL_USER,
            pass: process.env.EMAIL_PASS,
        },
    });

    // var maillist = ["gaurav.logicloop@gmail.com"];
    var maillist = req.body.maillist || ["gaurav.logicloop@gmail.com"];

    const generateFieldRow = (label, value) => {
        return value ? `<tr><td style="width: 30%;"><b>${label}:</b></td><td style="width: 70%;">${value}</td></tr>` : '';
    };

    const mailData = {
        from: "gaurav.logicloop@gmail.com",
        to: maillist,
        subject: `Akruti ${req.body.FormSource} Leads`,
        html: `
      <table border="0" cellpadding="0" cellspacing="0" style="padding-top:35px; background-color:#f1f1f1; font-family:Verdana,Arial,sans-serif; color:#454748; width:100%; border-collapse:separate;">
          <tbody>
          <tr>
              <td align="center">
                  <table border="0" cellpadding="0" cellspacing="0" width="590" style="padding:16px; background-color:white; color:#454748; border-collapse:separate;">
                      <tbody>
                      <tr>
                          <td align="center" style="min-width:590px;">
                              <table border="0" cellpadding="0" cellspacing="0" width="590" style="min-width:590px; background-color:white; padding:0px 8px 0px 8px; border-collapse:separate;">
                                  <tbody>
                                  <tr>
                                      <td valign="middle">
                                          <strong style="font-size:16px; margin: 0;">Welcome to Akruti</strong>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td colspan="2" style="text-align:center;">
                                          <hr width="100%" style="border-top-color:rgba(0,0,0,0.1); border-top-style:solid; border-top-width:1px;">
                                      </td>
                                  </tr>
                                  </tbody>
                              </table>
                          </td>
                      </tr>
                      <tr>
                          <td align="center" style="min-width:590px;">
                              <table border="0" cellpadding="0" cellspacing="0" width="590" style="min-width:590px; background-color:white; padding:0px 8px 0px 8px; border-collapse:separate;">
                                  <tbody>
                                  <tr>
                                      <td colspan="2" valign="top" style="font-size:13px;">
                                          <div style="font-size:13px; font-family:'Lucida Grande',Helvetica,Verdana,Arial,sans-serif;">
                                              Details below are enquiry from website!<br><br>
                                          </div>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td valign="top" style="font-size:13px;">
                                          <div style="font-size:13px; font-family:'Lucida Grande',Helvetica,Verdana,Arial,sans-serif;">
                                              <table>
                                                  ${generateFieldRow('Name', req.body.name)}
                                                  ${generateFieldRow('Email', req.body.email)}
                                                  ${generateFieldRow('Mobile', req.body.mobile)}
                                                  ${generateFieldRow('Company', req.body.Company)}
                                                  ${generateFieldRow('Organization', req.body.organization)}
                                                  ${generateFieldRow('Message', req.body.Message)}
                                              </table>
                                              <br><br>
                                          </div>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td colspan="2" style="text-align:center;">
                                          <hr width="100%" style="border-top-color:rgba(0,0,0,0.1); border-top-style:solid; border-top-width:1px;">
                                      </td>
                                  </tr>
                                  </tbody>
                              </table>
                          </td>
                      </tr>
                      </tbody>
                  </table>
              </td>
          </tr>
          <tr>
              <td align="center" style="min-width:590px;">
                  <table border="0" cellpadding="0" cellspacing="0" width="590" style="min-width:590px; background-color:#f1f1f1; color:#454748; padding:8px; border-collapse:separate;">
                      <tbody>
                      <tr>
                          <td style="text-align:center; font-size:13px;"><br></td>
                      </tr>
                      </tbody>
                  </table>
              </td>
          </tr>
          </tbody>
      </table>`,
    };

    transporter.sendMail(mailData, function (err, info) {
        if (err) console.log(err);
        else console.log(info);
    });
    console.log(req.body);
    res.send("success");
}