+ This email was sent from your website's career application form.
+
+
+ `,
+ attachments: attachments,
+ };
+
+ const result = await transporter.sendMail(mailOptions);
+ console.log('Career email sent successfully:', result.messageId);
+ return result;
+}
+
+// POST handler for all form submissions
+export async function POST(request) {
+ console.log('\nš === EMAIL API ROUTE STARTED ===');
+
+ try {
+ // Check environment variables first
+ const requiredEnvVars = {
+ ZOHO_EMAIL: process.env.ZOHO_EMAIL,
+ ZOHO_PASSWORD: process.env.ZOHO_PASSWORD,
+ RECIPIENT_EMAIL: process.env.RECIPIENT_EMAIL,
+ };
+
+ console.log('Environment variables check:');
+ Object.entries(requiredEnvVars).forEach(([key, value]) => {
+ console.log(`${key}: ${value ? 'ā Set' : 'ā Missing'}`);
+ });
+ console.log(
+ 'Loaded password (first 3 chars):',
+ process.env.ZOHO_PASSWORD?.slice(0, 3)
+ );
+
+ if (!requiredEnvVars.ZOHO_EMAIL || !requiredEnvVars.ZOHO_PASSWORD || !requiredEnvVars.RECIPIENT_EMAIL) {
+ console.error('ā Missing required environment variables');
+ return NextResponse.json(
+ { error: 'Email configuration missing' },
+ { status: 500 }
+ );
+ }
+
+ // Parse request body
+ let body;
+ try {
+ body = await request.json();
+ console.log('ā Request body parsed successfully');
+ } catch (parseError) {
+ console.error('ā Failed to parse request body:', parseError);
+ return NextResponse.json(
+ { error: 'Invalid JSON in request body' },
+ { status: 400 }
+ );
+ }
+
+ const { type, ...data } = body;
+ console.log(`š Form type: ${type}`);
+ console.log(`š Form data keys: ${Object.keys(data).join(', ')}`);
+
+ if (!type) {
+ console.error('ā No form type specified');
+ return NextResponse.json(
+ { error: 'Form type is required' },
+ { status: 400 }
+ );
+ }
+
+ // Route to appropriate handler based on form type
+ let result;
+ switch (type) {
+ case 'newsletter':
+ if (!data.email) {
+ console.error('ā Newsletter: Email is required');
+ return NextResponse.json({ error: 'Email is required' }, { status: 400 });
+ }
+ result = await handleNewsletter(data);
+ break;
+
+ case 'contact':
+ if (!data.fullName || !data.email) {
+ console.error('ā Contact: Full name and email are required');
+ return NextResponse.json({ error: 'Full name and email are required' }, { status: 400 });
+ }
+ result = await handleContact(data);
+ break;
+
+ case 'career':
+ if (!data.fullName || !data.email || !data.phone) {
+ console.error('ā Career: Full name, email, and phone are required');
+ return NextResponse.json({ error: 'Full name, email, and phone are required' }, { status: 400 });
+ }
+ result = await handleCareer(data);
+ break;
+
+ default:
+ console.error('ā Invalid form type:', type);
+ return NextResponse.json({ error: 'Invalid form type' }, { status: 400 });
+ }
+
+ console.log('ā Email sent successfully!');
+ console.log('š§ Message ID:', result.messageId);
+ console.log('š === EMAIL API ROUTE SUCCESS ===\n');
+
+ return NextResponse.json(
+ {
+ message: 'Email sent successfully',
+ messageId: result.messageId,
+ type: type
+ },
+ { status: 200 }
+ );
+
+ } catch (error) {
+ console.error('\nš„ === EMAIL API ROUTE ERROR ===');
+ console.error('Error name:', error.name);
+ console.error('Error message:', error.message);
+ console.error('Error code:', error.code);
+ console.error('Error stack:', error.stack);
+
+ // Provide more specific error messages
+ let errorMessage = 'Failed to send email';
+ let statusCode = 500;
+
+ if (error.code === 'EAUTH') {
+ errorMessage = 'Authentication failed. Please check your Zoho email and app password.';
+ console.error('š Authentication issue - check ZOHO_EMAIL and ZOHO_PASSWORD');
+ } else if (error.code === 'ECONNECTION' || error.code === 'ETIMEDOUT') {
+ errorMessage = 'Connection to email server failed. Please try again.';
+ console.error('š Network connection issue');
+ } else if (error.code === 'EMESSAGE') {
+ errorMessage = 'Invalid email message format.';
+ statusCode = 400;
+ console.error('š§ Email format issue');
+ }
+
+ console.error('š„ === END ERROR DETAILS ===\n');
+
+ return NextResponse.json(
+ {
+ error: errorMessage,
+ details: error.message,
+ code: error.code
+ },
+ { status: statusCode }
+ );
+ }
+}
\ No newline at end of file
diff --git a/components/common/Careerform.jsx b/components/common/Careerform.jsx
index 82fded4..90e2ba4 100644
--- a/components/common/Careerform.jsx
+++ b/components/common/Careerform.jsx
@@ -12,6 +12,8 @@ export default function Careerform() {
resume: null
});
const [uploadStatus, setUploadStatus] = useState('');
+ const [isSubmitting, setIsSubmitting] = useState(false);
+ const [submitStatus, setSubmitStatus] = useState(null);
const handleInputChange = (e) => {
const { id, value } = e.target;
@@ -39,15 +41,25 @@ export default function Careerform() {
return;
}
- setFormData(prev => ({
- ...prev,
- resume: file
- }));
- setUploadStatus(`${file.name} uploaded successfully`);
+ // Convert file to base64
+ const reader = new FileReader();
+ reader.onload = () => {
+ const base64 = reader.result.split(',')[1]; // Remove data:mime;base64, prefix
+ setFormData(prev => ({
+ ...prev,
+ resume: {
+ name: file.name,
+ data: base64,
+ type: file.type
+ }
+ }));
+ setUploadStatus(`${file.name} uploaded successfully`);
+ };
+ reader.readAsDataURL(file);
}
};
- const handleSubmit = (e) => {
+ const handleSubmit = async (e) => {
e.preventDefault();
// Basic form validation
@@ -61,40 +73,47 @@ export default function Careerform() {
return;
}
- // Here you would typically send the form data to your backend
- console.log('Form submitted:', formData);
-
- // Example of how you might handle the form submission
- const submitData = new FormData();
- submitData.append('fullName', formData.fullName);
- submitData.append('email', formData.email);
- submitData.append('phone', formData.phone);
- submitData.append('roles', formData.roles);
- submitData.append('resume', formData.resume);
+ setIsSubmitting(true);
+ setSubmitStatus(null);
- // Replace with your actual API endpoint
- // fetch('/api/submit-application', {
- // method: 'POST',
- // body: submitData
- // }).then(response => {
- // // Handle response
- // });
+ try {
+ const response = await fetch('/api/contact', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify({
+ type: 'career',
+ ...formData,
+ }),
+ });
- alert('Application submitted successfully!');
-
- // Reset form
- setFormData({
- fullName: '',
- email: '',
- phone: '',
- roles: '',
- resume: null
- });
- setUploadStatus('');
-
- // Reset file input
- const fileInput = document.getElementById('resume-upload');
- if (fileInput) fileInput.value = '';
+ if (response.ok) {
+ setSubmitStatus('success');
+
+ // Reset form
+ setFormData({
+ fullName: '',
+ email: '',
+ phone: '',
+ roles: '',
+ resume: null
+ });
+ setUploadStatus('');
+
+ // Reset file input
+ const fileInput = document.getElementById('resume-upload');
+ if (fileInput) fileInput.value = '';
+ } else {
+ setSubmitStatus('error');
+ }
+ } catch (error) {
+ console.error('Error:', error);
+ setSubmitStatus('error');
+ } finally {
+ setIsSubmitting(false);
+ setTimeout(() => setSubmitStatus(null), 5000);
+ }
};
return (
@@ -120,6 +139,20 @@ export default function Careerform() {
+ {submitStatus && (
+
+ {submitStatus === 'success'
+ ? 'Application submitted successfully! We will review your application and get back to you soon.'
+ : 'Failed to submit application. Please try again.'}
+