phonepe #66
@ -21,7 +21,7 @@ interface EmailRequest {
|
|||||||
|
|
||||||
interface Attachment {
|
interface Attachment {
|
||||||
filename: string;
|
filename: string;
|
||||||
content: string | Buffer; // Base64 encoded string or Buffer
|
content: string | Buffer;
|
||||||
contentType?: string;
|
contentType?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,7 +32,7 @@ const stripHtml = (html: string): string => {
|
|||||||
|
|
||||||
async function sendSimpleEmail(data: EmailRequest, recipients: string[]) {
|
async function sendSimpleEmail(data: EmailRequest, recipients: string[]) {
|
||||||
const ses = new SESClient({
|
const ses = new SESClient({
|
||||||
region: 'ap-south-1',
|
region: process.env.AWS_REGION,
|
||||||
credentials: {
|
credentials: {
|
||||||
accessKeyId: process.env.AWS_ACCESS_KEY_ID || '',
|
accessKeyId: process.env.AWS_ACCESS_KEY_ID || '',
|
||||||
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || ''
|
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || ''
|
||||||
@ -58,7 +58,7 @@ async function sendSimpleEmail(data: EmailRequest, recipients: string[]) {
|
|||||||
|
|
||||||
async function sendEmailWithAttachments(data: EmailRequest, recipients: string[]) {
|
async function sendEmailWithAttachments(data: EmailRequest, recipients: string[]) {
|
||||||
const ses = new SESClient({
|
const ses = new SESClient({
|
||||||
region: 'ap-south-1',
|
region: process.env.AWS_REGION,
|
||||||
credentials: {
|
credentials: {
|
||||||
accessKeyId: process.env.AWS_ACCESS_KEY_ID || '',
|
accessKeyId: process.env.AWS_ACCESS_KEY_ID || '',
|
||||||
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || ''
|
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || ''
|
||||||
@ -72,26 +72,21 @@ async function sendEmailWithAttachments(data: EmailRequest, recipients: string[]
|
|||||||
rawMessage += `MIME-Version: 1.0\n`;
|
rawMessage += `MIME-Version: 1.0\n`;
|
||||||
rawMessage += `Content-Type: multipart/mixed; boundary="${boundary}"\n\n`;
|
rawMessage += `Content-Type: multipart/mixed; boundary="${boundary}"\n\n`;
|
||||||
|
|
||||||
// Add email body (multipart/alternative)
|
|
||||||
rawMessage += `--${boundary}\n`;
|
rawMessage += `--${boundary}\n`;
|
||||||
rawMessage += `Content-Type: multipart/alternative; boundary="alt_${boundary}"\n\n`;
|
rawMessage += `Content-Type: multipart/alternative; boundary="alt_${boundary}"\n\n`;
|
||||||
|
|
||||||
// Text part
|
|
||||||
if (data.text) {
|
if (data.text) {
|
||||||
rawMessage += `--alt_${boundary}\n`;
|
rawMessage += `--alt_${boundary}\n`;
|
||||||
rawMessage += `Content-Type: text/plain; charset=UTF-8\n\n`;
|
rawMessage += `Content-Type: text/plain; charset=UTF-8\n\n`;
|
||||||
rawMessage += `${data.text}\n\n`;
|
rawMessage += `${data.text}\n\n`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTML part
|
|
||||||
rawMessage += `--alt_${boundary}\n`;
|
rawMessage += `--alt_${boundary}\n`;
|
||||||
rawMessage += `Content-Type: text/html; charset=UTF-8\n\n`;
|
rawMessage += `Content-Type: text/html; charset=UTF-8\n\n`;
|
||||||
rawMessage += `${data.html}\n\n`;
|
rawMessage += `${data.html}\n\n`;
|
||||||
|
|
||||||
// Close alternative part
|
|
||||||
rawMessage += `--alt_${boundary}--\n\n`;
|
rawMessage += `--alt_${boundary}--\n\n`;
|
||||||
|
|
||||||
// Add attachments
|
|
||||||
for (const attachment of data.attachments || []) {
|
for (const attachment of data.attachments || []) {
|
||||||
const contentType = attachment.contentType ||
|
const contentType = attachment.contentType ||
|
||||||
mime.lookup(attachment.filename) ||
|
mime.lookup(attachment.filename) ||
|
||||||
@ -109,7 +104,6 @@ async function sendEmailWithAttachments(data: EmailRequest, recipients: string[]
|
|||||||
rawMessage += contentBuffer.toString('base64') + '\n\n';
|
rawMessage += contentBuffer.toString('base64') + '\n\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close message
|
|
||||||
rawMessage += `--${boundary}--`;
|
rawMessage += `--${boundary}--`;
|
||||||
|
|
||||||
const command = new SendRawEmailCommand({
|
const command = new SendRawEmailCommand({
|
||||||
@ -140,7 +134,6 @@ export async function sendEmailWithAttachmentUtil(
|
|||||||
try {
|
try {
|
||||||
logger.info(`Sending email with attachment to: ${toAddress}`);
|
logger.info(`Sending email with attachment to: ${toAddress}`);
|
||||||
|
|
||||||
// Initialize data with basic fields
|
|
||||||
const data: EmailRequest = {
|
const data: EmailRequest = {
|
||||||
to: toAddress,
|
to: toAddress,
|
||||||
html: message,
|
html: message,
|
||||||
@ -151,13 +144,11 @@ export async function sendEmailWithAttachmentUtil(
|
|||||||
attachments: []
|
attachments: []
|
||||||
};
|
};
|
||||||
|
|
||||||
// Handle file URL if provided
|
|
||||||
if (fileUrl && fileName) {
|
if (fileUrl && fileName) {
|
||||||
logger.info(`Downloading attachment from URL: ${fileUrl}`);
|
logger.info(`Downloading attachment from URL: ${fileUrl}`);
|
||||||
try {
|
try {
|
||||||
const fileContent = await downloadFileFromUrl(fileUrl);
|
const fileContent = await downloadFileFromUrl(fileUrl);
|
||||||
|
|
||||||
// Add the downloaded file as an attachment
|
|
||||||
data.attachments!.push({
|
data.attachments!.push({
|
||||||
filename: fileName,
|
filename: fileName,
|
||||||
content: fileContent,
|
content: fileContent,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user