aboutsummaryrefslogtreecommitdiff
path: root/src/app/get-started/actions.ts
blob: 1e5f439c6c1f09f79dddadb1225545977027e352 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"use server";

import { createTransport } from "nodemailer";

const noSend = true; // for testing

if (noSend) {
  console.log("Emails are disabled - no emails will be sent");
}

export default async function logFormData(
  prevState: { message: string; error: boolean },
  formData: FormData
) {
  // wait 15 sec
  await new Promise<void>((resolve) => setTimeout(resolve, 2000));

  // Create a test account or replace with real credentials.
  const transporter = createTransport({
    host: "mail.mfoi.dev",
    port: 465,
    secure: true, // true for 465, false for other ports
    auth: {
      user: "test@mfoi.dev",
      pass: "Fakrum-5hapzo-fivkeb", // TODO: put in env variable
    },
  });

  const email_content = `
<h2>New Consulting Request</h2>
<p><strong>First Name: </strong>${formData.get("firstname")}</p>
<p><strong>Last Name: </strong>${formData.get("lastname")}</p>
<p><strong>Email: </strong>${formData.get("email")}</p>${
    formData.get("phonenumber") &&
    `<p><strong>Phone Number: </strong>${formData.get("phonenumber")}</p>`
  }
<p><strong>Message: </strong><br />${formData.get("message")}</p>
<hr />
<p><strong>Submitted at:</strong> ${new Date().toLocaleString()}</p>
  `;

  const full_name = `${formData.get("firstname")} ${formData.get("lastname")}`;

  if (noSend) {
    console.log("Email sending is disabled. Email content:");
    console.log(email_content);
    return {
      message:
        "Successfully submitted your consultation request - email sending is disabled for testing purposes.",
      error: false,
    };
  }

  try {
    const info = await transporter.sendMail({
      from: '"sensiblescholars.com" <test@mfoi.dev>',
      to: "test@mfoi.dev",
      subject: `New Consultation Request from ${full_name}!`,
      html: email_content,
    });
    console.log("Message sent:", info.messageId);
  } catch (error) {
    console.error("Error sending email:", error); // Handle errors
    return {
      message:
        "Failed to send email. This has been reported. Please try again later, and sorry for any inconvenience.",
      error: true,
    };
  }

  return {
    message:
      "Successfully submitted your consultation request - expect to hear back soon via email!",
    error: false,
  };
}