add phone & company name

This commit is contained in:
2025-03-19 12:56:34 +01:00
parent e7cadb6df8
commit fcdd98c9ca
4 changed files with 83 additions and 17 deletions

View File

@@ -10,7 +10,13 @@ export class AppController {
return { message: 'API is alive' };
}
@Post()
async sendEMail(@Body() mailInfo: {name:string,email:string,message:string}): Promise<void> {
async sendEMail(@Body() mailInfo: {
name: string,
email: string,
phone?: string,
company?: string,
message: string
}): Promise<void> {
return await this.appService.sendMail(mailInfo);
}
}
}

View File

@@ -1,18 +1,38 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import { MailerService } from '@nestjs-modules/mailer';
import { z,ZodError } from 'zod';
import { z, ZodError } from 'zod';
export const SenderSchema = z.object({
name: z.string().min(6, { message: 'Name must be at least 6 characters long' }),
email: z.string().email({ message: 'Invalid email address' }),
phone: z.string()
.optional()
.refine(
(val) => {
if (!val) return true; // If no phone number provided, validation passes
// Validate US phone number format (various formats supported)
const usPhoneRegex = /^(\+?1\s?)?(\(\d{3}\)|\d{3})[\s.-]?\d{3}[\s.-]?\d{4}$/;
return usPhoneRegex.test(val);
},
{ message: 'Please enter a valid US phone number' }
),
company: z.string().optional(), // Company is optional
message: z.string().min(10, { message: 'Comments must be at least 10 characters long' }),
});
@Injectable()
export class AppService {
constructor(
private mailerService: MailerService,
) {}
async sendMail(mailInfo: {name:string,email:string,message:string}): Promise<void> {
async sendMail(mailInfo: {
name: string,
email: string,
phone?: string, // Mark as optional with ?
company?: string, // Mark as optional with ?
message: string
}): Promise<void> {
try {
SenderSchema.parse(mailInfo);
} catch (error) {
@@ -25,6 +45,7 @@ export class AppService {
}
throw error;
}
await this.mailerService.sendMail({
to: 'andreas.knuth@gmail.com',
from: `"Bay Area Affiliates, Inc." <bayarea@bizmatch.net>`,
@@ -33,8 +54,10 @@ export class AppService {
context: {
name: mailInfo.name,
email: mailInfo.email,
phone: mailInfo.phone || 'Not provided',
company: mailInfo.company || 'Not provided',
message: mailInfo.message
},
});
}
}
}

View File

@@ -2,7 +2,7 @@
<html>
<head>
<meta charset="UTF-8">
<title>Neue Nachricht von {{name}}</title>
<title>New message from {{name}}</title>
<style>
body {
font-family: Arial, sans-serif;
@@ -41,6 +41,8 @@
<div class="content">
<p><strong>Name:</strong> {{name}}</p>
<p><strong>Email:</strong> {{email}}</p>
<p><strong>Phone:</strong> {{phone}}</p>
<p><strong>Company:</strong> {{company}}</p>
<p><strong>Message:</strong></p>
<p>{{message}}</p>
</div>
@@ -49,4 +51,4 @@
</div>
</div>
</body>
</html>
</html>