diff options
Diffstat (limited to 'src/app/get-started/actions.ts')
-rw-r--r-- | src/app/get-started/actions.ts | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/app/get-started/actions.ts b/src/app/get-started/actions.ts new file mode 100644 index 0000000..1e5f439 --- /dev/null +++ b/src/app/get-started/actions.ts @@ -0,0 +1,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, + }; +} |