diff --git a/apps/api/src/services/index.ts b/apps/api/src/services/index.ts index a589bfa9..2e9a7c90 100644 --- a/apps/api/src/services/index.ts +++ b/apps/api/src/services/index.ts @@ -1,3 +1,2 @@ export * from './account'; export * from './onboarding'; -export * from './mail'; diff --git a/apps/api/src/services/mail.ts b/apps/api/src/services/mail.ts deleted file mode 100644 index 3720e4fa..00000000 --- a/apps/api/src/services/mail.ts +++ /dev/null @@ -1,91 +0,0 @@ -import { extractNameAndEmail, nodemailerTransporter, withEmailTemplate, zohoClient } from '@api/utils/mail'; - -import type { TEmailData } from '@cio/utils/validation/mail'; -import type { Transporter } from 'nodemailer'; -import { env } from '@api/config/env'; - -interface EmailResponse { - success: boolean; - error?: string; - details?: unknown; -} - -let transporter: Transporter | null; - -nodemailerTransporter().then((t: Transporter | null) => { - transporter = t; -}); - -export async function sendWithNodemailer(emailData: TEmailData): Promise { - const { from, to, subject, content, replyTo } = emailData; - - if (!transporter) { - return { - success: false, - error: 'Email transporter not initialized' - }; - } - - try { - const result = await transporter.sendMail({ - from: from || env.SMTP_SENDER, - to, - subject, - replyTo, - html: withEmailTemplate(content) - }); - - return { - success: true, - details: result - }; - } catch (error) { - return { - success: false, - error: error instanceof Error ? error.message : 'Unknown error occurred', - details: error - }; - } -} - -export async function sendWithZoho(emailData: TEmailData): Promise { - const { from, to, subject, content } = emailData; - - const fromData = extractNameAndEmail(from || ''); - - if (!fromData) { - return { - success: false, - error: 'Invalid from data' - }; - } - - try { - const result = await zohoClient.sendMail({ - from: { - address: fromData.email, - name: fromData.name - }, - to: [ - { - email_address: { - address: to - } - } - ], - subject, - htmlbody: content - }); - - return { - success: true, - details: result - }; - } catch (error) { - return { - success: false, - error: error instanceof Error ? error.message : 'Unknown error occurred', - details: error - }; - } -} diff --git a/apps/api/src/utils/mail.ts b/apps/api/src/utils/mail.ts deleted file mode 100644 index 2018989f..00000000 --- a/apps/api/src/utils/mail.ts +++ /dev/null @@ -1,249 +0,0 @@ -import { SendMailClient } from 'zeptomail'; -import type { Transporter } from 'nodemailer'; -import { env } from '@api/config/env'; -import nodemailer from 'nodemailer'; - -export const zohoClient = new SendMailClient({ - url: 'api.zeptomail.eu/', - token: env.ZOHO_TOKEN! -}); - -export const nodemailerTransporter = async (): Promise => { - if (!env.SMTP_HOST || !env.SMTP_PORT || !env.SMTP_USER || !env.SMTP_PASSWORD) { - console.error('SMTP configuration missing'); - return null; - } - - try { - const transporter = nodemailer.createTransport({ - host: env.SMTP_HOST, - port: parseInt(env.SMTP_PORT || '465', 10), - secure: true, - auth: { - user: env.SMTP_USER, - pass: env.SMTP_PASSWORD - } - }); - - await transporter.verify(); - - return transporter; - } catch (error) { - console.error('Transporter error:', error); - return null; - } -}; - -interface FromData { - name: string; - email: string; -} - -// format: "ClassroomIO Developers (via ClassroomIO.com)" -export function extractNameAndEmail(str: string): FromData | undefined { - // Use regular expressions to match the name and email - const regex = /"(.*?)"\s+<\s*(.*?)@(.*?)\s*>/; - const match = str.match(regex); - - if (match) { - // Extract the name and email from the match groups - const name = match[1]; - const email = match[2] + '@' + match[3]; - return { name, email }; - } else { - // Return undefined if the format doesn't match - return { name: str, email: str }; - } -} - -export const withEmailTemplate = (content: string): string => - ` - - - - - - - - - - - -
- - ClassroomIO Logo -
-
- ${content} -
- - - - - -`;