import nodemailer from 'nodemailer'
export default defineEventHandler(async (event) => {
const body = await readBody(event)
if (!body.name || !body.email || !body.message) {
throw createError({ statusCode: 400, statusMessage: 'Name, email and message are required.' })
}
const transporter = nodemailer.createTransport({
host: 'localhost',
port: 25,
secure: false,
tls: {
rejectUnauthorized: false
}
})
const htmlContent = `
New Contact Form Submission
| Name: | ${body.name} |
| Email: | ${body.email} |
${body.company ? `| Company: | ${body.company} |
` : ''}
${body.phone ? `| Phone: | ${body.phone} |
` : ''}
Message:
${body.message.replace(/\n/g, '
')}
`
try {
await transporter.sendMail({
from: 'no-reply@logyou.de',
to: 'info@logyou.de',
replyTo: body.email,
subject: `Contact Form: ${body.name}${body.company ? ' (' + body.company + ')' : ''}`,
text: `Name: ${body.name}\nEmail: ${body.email}\nCompany: ${body.company || '-'}\nPhone: ${body.phone || '-'}\n\nMessage:\n${body.message}`,
html: htmlContent
})
return { success: true }
} catch (error: any) {
throw createError({ statusCode: 500, statusMessage: 'Failed to send message. Please try again later.' })
}
})