import nodemailer from "nodemailer" import { logger } from "@/lib/logger" import { getBaseUrl } from "@/lib/utils" /** Returns the absolute URL for the JUNO logo (for use in outbound HTML emails) */ export function getEmailLogoUrl(): string { return `${getBaseUrl()}/icon.png` } // Create reusable transporter (only if SMTP is configured) function getTransporter() { if (!process.env.SMTP_USER || !process.env.SMTP_PASSWORD) { return null } return nodemailer.createTransport({ host: process.env.SMTP_HOST || "smtp.gmail.com", port: parseInt(process.env.SMTP_PORT || "587"), secure: process.env.SMTP_SECURE === "true", // true for 465, false for other ports auth: { user: process.env.SMTP_USER, pass: process.env.SMTP_PASSWORD, }, }) } export interface EmailOptions { to: string subject: string html: string } export async function sendEmail({ to, subject, html }: EmailOptions) { try { // Skip sending in development if SMTP is not configured if (!process.env.SMTP_USER || !process.env.SMTP_PASSWORD) { logger.info("Email not configured. Would send", { to, subject }) return { success: true, message: "Email skipped (not configured)" } } const transporter = getTransporter() if (!transporter) { logger.warn("Email transporter not available. Would send", { to, subject }) return { success: true, message: "Email skipped (transporter not available)" } } const info = await transporter.sendMail({ from: `"JUNO" <${process.env.SMTP_USER}>`, to, subject, html, }) return { success: true, messageId: info.messageId } } catch (error) { logger.error("Error sending email", error) // Don't throw - let the caller handle it return { success: false, error: error instanceof Error ? error.message : "Unknown email error" } } } export function generateInvitationEmailHtml( supplierName: string, vendorCompanyName: string, invitationToken: string, invitationLink: string ): string { return ` Supplier Invitation - JUNO
JUNO

JUNO

You've Been Invited!

Hello ${supplierName || "Supplier"},

${vendorCompanyName} has invited you to connect on JUNO. They're interested in partnering with you and would like to explore your product catalog.

Your Invitation Token

${invitationToken}

Use this token in your supplier portal to accept the invitation

Accept Invitation

Or copy the invitation token above and paste it in the Vendors section of your supplier portal.

This invitation will expire in 7 days.

© 2026 JUNO Systems. All rights reserved.

` }