send from gmail

This commit is contained in:
Ken Yasue
2025-04-01 16:19:00 +02:00
parent 45164faa9d
commit 597f7ddc55
6 changed files with 129 additions and 10 deletions

87
src/lib/email.ts Normal file
View File

@ -0,0 +1,87 @@
import nodemailer from 'nodemailer';
import { ContactRecord } from './database/entities/ContactRecord';
import { Customer } from './database/entities/Customer';
import { getDataSource } from './database';
interface SendEmailResult {
success: boolean;
error?: string;
}
export async function sendEmail(
customerId: string,
subject: string,
body: string,
htmlBody?: string
): Promise<SendEmailResult> {
try {
// Get data source
const dataSource = await getDataSource();
// Get customer details
const customer = await dataSource.getRepository(Customer).findOne({
where: { id: customerId }
});
if (!customer) {
throw new Error(`Customer with ID ${customerId} not found`);
}
// Create Gmail transporter
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.GMAIL_USER,
pass: process.env.GMAIL_APP_PASSWORD // Using app-specific password
}
});
// Send email
const info = await transporter.sendMail({
from: process.env.GMAIL_USER,
to: customer.email,
subject: subject,
text: body,
html: htmlBody || body
});
// Create contact record
const contactRecord = new ContactRecord();
contactRecord.customer = customer;
contactRecord.customerId = customer.id;
contactRecord.contactType = 'EMAIL';
contactRecord.notes = `Email sent successfully. Subject: ${subject}. Message ID: ${info.messageId}`;
// Save contact record
await dataSource.getRepository(ContactRecord).save(contactRecord);
return { success: true };
} catch (error) {
// Create contact record for failed attempt
if (error instanceof Error) {
try {
const dataSource = await getDataSource();
const customer = await dataSource.getRepository(Customer).findOne({
where: { id: customerId }
});
if (customer) {
const contactRecord = new ContactRecord();
contactRecord.customer = customer;
contactRecord.customerId = customer.id;
contactRecord.contactType = 'EMAIL';
contactRecord.notes = `Failed to send email: ${error.message}`;
await dataSource.getRepository(ContactRecord).save(contactRecord);
}
} catch (dbError) {
console.error('Failed to save error record:', dbError);
}
}
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error occurred'
};
}
}

View File

@ -8,7 +8,7 @@ import { Customer } from '../lib/database/entities/Customer';
interface CustomerCSVRow {
City: string;
Name: string;
'Website URL': string;
URL: string;
Email: string;
}
@ -66,14 +66,20 @@ async function importCustomers(csvFilePath: string): Promise<void> {
// Create new customer
const customer = new Customer();
customer.name = name;
customer.url = record['Website URL'] === 'null' ? '' : record['Website URL'];
customer.url = record['URL'] === 'null' ? '' : record['URL'];
customer.email = email;
// Save to database
await customerRepository.save(customer);
importedCount++;
try {
// Save to database
await customerRepository.save(customer);
importedCount++;
console.log(`Imported customer: ${name}`);
} catch (e) {
console.log(`Skipped: ${name}`);
}
console.log(`Imported customer: ${name}`);
}
console.log('Import summary:');