build improvements
This commit is contained in:
65
yoga-app/src/actions/contactus-actions.ts
Normal file
65
yoga-app/src/actions/contactus-actions.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
'use server'
|
||||
|
||||
import {sendMail} from "@/actions/mail-actions";
|
||||
import {contactUsFormValidator} from "@/validation/validation";
|
||||
import {copyFormValues, copyValidationErrors, FormControl, formDataToJson} from "@/util/form-util";
|
||||
import localize_hu from 'ajv-i18n/localize/hu';
|
||||
|
||||
|
||||
export type ContactFormState = {
|
||||
firstname: FormControl,
|
||||
lastname: FormControl,
|
||||
phone: FormControl,
|
||||
email: FormControl,
|
||||
comment: FormControl,
|
||||
formState: 'empty' | 'invalid' | 'success'
|
||||
}
|
||||
export type ContactFormData = {
|
||||
firstname: string,
|
||||
lastname: string,
|
||||
phone: string,
|
||||
email: string,
|
||||
comment: string,
|
||||
}
|
||||
|
||||
export async function sendContactUsMail(prevState: ContactFormState, formState: FormData) {
|
||||
const newState: ContactFormState = {
|
||||
comment: {name: 'comment'}, email: {name: 'email'}, firstname: {name: 'firstname'},
|
||||
lastname: {name: 'lastname'},
|
||||
phone: {name: 'phone'},
|
||||
formState: 'invalid'
|
||||
};
|
||||
const contactUsFormData: ContactFormData = formDataToJson(formState) as ContactFormData;
|
||||
const validationResult = contactUsFormValidator(contactUsFormData);
|
||||
localize_hu(contactUsFormValidator.errors)
|
||||
if (!validationResult) {
|
||||
copyFormValues(newState, contactUsFormData);
|
||||
copyValidationErrors(newState, contactUsFormValidator)
|
||||
} else {
|
||||
sendMail(
|
||||
{
|
||||
email: contactUsFormData.email,
|
||||
sendTo: process.env.SITE_MAIL_RECIEVER,
|
||||
subject: 'Kapcsolat',
|
||||
text: `
|
||||
Kapcsolat üzenet érkezett:
|
||||
Név: ${contactUsFormData.lastname} ${contactUsFormData.firstname}
|
||||
Tel: ${contactUsFormData.phone}
|
||||
Email: ${contactUsFormData.email}
|
||||
Üzenet:
|
||||
${contactUsFormData.comment}
|
||||
`,
|
||||
html:`
|
||||
Kapcsolat üzenet érkezett:
|
||||
<br>Név: ${contactUsFormData.lastname} ${contactUsFormData.firstname}
|
||||
<br>Tel: ${contactUsFormData.phone}
|
||||
<br>Email: ${contactUsFormData.email}
|
||||
<br>Üzenet:
|
||||
<br>${contactUsFormData.comment}
|
||||
`,
|
||||
}
|
||||
)
|
||||
newState.formState = 'success';
|
||||
}
|
||||
return newState;
|
||||
}
|
||||
50
yoga-app/src/actions/mail-actions.ts
Normal file
50
yoga-app/src/actions/mail-actions.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
'use server';
|
||||
import nodemailer from 'nodemailer';
|
||||
const SMTP_SERVER_HOST = process.env.SMTP_SERVER_HOST;
|
||||
const SMTP_SERVER_PORT =parseInt( process.env.SMTP_SERVER_PORT!,10);
|
||||
const SMTP_SERVER_SECURE = "true" == process.env.SMTP_SERVER_SECIRE ;
|
||||
const SMTP_SERVER_USERNAME = process.env.SMTP_SERVER_USERNAME;
|
||||
const SMTP_SERVER_PASSWORD = process.env.SMTP_SERVER_PASSWORD;
|
||||
const SITE_MAIL_RECIEVER = process.env.SITE_MAIL_RECIEVER;
|
||||
const transporter = nodemailer.createTransport({
|
||||
// service: 'gmail',
|
||||
host: SMTP_SERVER_HOST,
|
||||
port: SMTP_SERVER_PORT,
|
||||
secure: SMTP_SERVER_SECURE,
|
||||
auth: {
|
||||
user: SMTP_SERVER_USERNAME,
|
||||
pass: SMTP_SERVER_PASSWORD,
|
||||
},
|
||||
});
|
||||
|
||||
export async function sendMail({
|
||||
email,
|
||||
sendTo,
|
||||
subject,
|
||||
text,
|
||||
html,
|
||||
}: {
|
||||
email: string;
|
||||
sendTo?: string;
|
||||
subject: string;
|
||||
text: string;
|
||||
html?: string;
|
||||
}) {
|
||||
try {
|
||||
const isVerified = await transporter.verify();
|
||||
console.log("isVerified",isVerified)
|
||||
} catch (error) {
|
||||
console.error('Something Went Wrong', SMTP_SERVER_USERNAME, SMTP_SERVER_PASSWORD, error);
|
||||
return;
|
||||
}
|
||||
const info = await transporter.sendMail({
|
||||
from: email,
|
||||
to: sendTo || SITE_MAIL_RECIEVER,
|
||||
subject: subject,
|
||||
text: text,
|
||||
html: html ? html : '',
|
||||
});
|
||||
console.log('Message Sent', info.messageId);
|
||||
console.log('Mail sent to',sendTo, SITE_MAIL_RECIEVER);
|
||||
return info;
|
||||
}
|
||||
Reference in New Issue
Block a user