| 1 | import { eq } from "drizzle-orm"; |
| 2 | import nodemailer from "nodemailer |
| 3 | import type { Transporter |
| 4 | import { db, settings |
| 5 | import { safeDecrypt } |
| 6 | |
| 7 | export interface SmtpConfig |
| 8 | host: string; |
| 9 | port: number; |
| 10 | user: string; |
| 11 | password: string; |
| 12 | fromEmail: string; |
| 13 | fromName: string; |
| 14 | secure: boolean; |
| 15 | } |
| 16 | |
| 17 | export interface EmailService |
| 18 | transporter: Transporter |
| 19 | fromEmail: string; |
| 20 | fromName: string; |
| 21 | } |
| 22 | |
| 23 | export function buildSmtpTransporter(config |
| 24 | // Port 465 = implicit TLS (secure: true). All other ports use STARTTLS: |
| 25 | // if the user wants TLS, set requireTLS: true so the upgrade is mandatory. |
| 26 | const implicitTls = |
| 27 | return nodemailer.createTransport |
| 28 | host: config.host |
| 29 | port: config.port |
| 30 | secure: implicitTls, |
| 31 | requireTLS: !implicitTls && |
| 32 | auth: config.user |
| 33 | }); |
| 34 | } |
| 35 | |
| 36 | export async function buildEmailServiceFromSettings |
| 37 | const rows = |
| 38 | const s = |
| 39 | |
| 40 | if (s.smtp_enabled |
| 41 | if (!s.smtp_host |
| 42 | |
| 43 | const password = |
| 44 | const transporter = |
| 45 | host: s.smtp_host |
| 46 | port: parseInt(s |
| 47 | user: s.smtp_user |
| 48 | password, |
| 49 | fromEmail: s.smtp_from_email |
| 50 | fromName: s.smtp_from_name |
| 51 | secure: s.smtp_secure |
| 52 | }); |
| 53 | |
| 54 | return { |
| 55 | transporter, |
| 56 | fromEmail: s.smtp_from_email |
| 57 | fromName: s.smtp_from_name |
| 58 | }; |
| 59 | } |
| 60 | |
| 61 | export async function getEmailTemplate |
| 62 | const row = |
| 63 | where: eq(settings |
| 64 | }); |
| 65 | return row?.value |
| 66 | } |
| 67 | |
| 68 | export function escapeHtml(value |
| 69 | return value |
| 70 | .replace(/&/g |
| 71 | .replace(/</g |
| 72 | .replace(/>/g |
| 73 | .replace(/"/g |
| 74 | .replace(/'/g |
| 75 | } |
| 76 | |
| 77 | export function substituteVars(html |
| 78 | return html.replace |
| 79 | vars[key] === |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | export async function sendEmail |
| 84 | service: EmailService |
| 85 | to: string, |
| 86 | subject: string, |
| 87 | html: string, |
| 88 | ): Promise<void |
| 89 | const from = |
| 90 | ? `"${service. |
| 91 | : service.fromEmail; |
| 92 | await service.transporter |
| 93 | } |
| 94 | |
| 95 | const BASE_STYLE = ` |
| 96 | body { margin: 0; padding: 0; background: #f4f4f5; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; } |
| 97 | .wrapper { padding: 40px 16px; } |
| 98 | .card { background: #ffffff; border-radius: 12px; max-width: 560px; margin: 0 auto; overflow: hidden; box-shadow: 0 1px 4px rgba(0,0,0,0.08); } |
| 99 | .header { background: #18181b; padding: 24px 32px; } |
| 100 | .header h1 { margin: 0; color: #ffffff; font-size: 18px; font-weight: 600; letter-spacing: -0.3px; } |
| 101 | .body { padding: 32px; } |
| 102 | .body h2 { margin: 0 0 8px; color: #18181b; font-size: 20px; font-weight: 600; } |
| 103 | .body p { margin: 0 0 16px; color: #52525b; font-size: 14px; line-height: 1.6; } |
| 104 | .btn { display: inline-block; background: #18181b; color: #ffffff !important; text-decoration: none; padding: 10px 24px; border-radius: 8px; font-size: 14px; font-weight: 500; } |
| 105 | .footer { padding: 0 32px 24px; } |
| 106 | .footer p { margin: 0; color: #a1a1aa; font-size: 12px; line-height: 1.5; } |
| 107 | .divider { border: none; border-top: 1px solid #f4f4f5; margin: 0 32px 24px; } |
| 108 | `.trim(); |
| 109 | |
| 110 | function baseTemplate(appName: |
| 111 | return `<!DOCTYPE html> |
| 112 | <html lang="en"> |
| 113 | <head> |
| 114 | <meta charset="UTF-8" /> |
| 115 | <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 116 | <title>${header}</title> |
| 117 | <style>${BASE_STYLE}</style> |
| 118 | </head> |
| 119 | <body> |
| 120 | <div class="wrapper"> |
| 121 | <div class="card"> |
| 122 | <div class="header"><h1>${escapeHtml(appName)} |
| 123 | <div class="body">${body}</div> |
| 124 | <hr class="divider" /> |
| 125 | <div class="footer"><p>This email was sent by ${escapeHtml(appName)} |
| 126 | </div> |
| 127 | </div> |
| 128 | </body> |
| 129 | </html>`; |
| 130 | } |
| 131 | |
| 132 | export const TEMPLATE_NAMES = |
| 133 | export type TemplateName |
| 134 | |
| 135 | export const DEFAULT_TEMPLATES: |
| 136 | verification: (vars) |
| 137 | baseTemplate( |
| 138 | vars.appName ?? |
| 139 | "Verify your email", |
| 140 | `<h2>Verify your email address</h2> |
| 141 | <p>Hi ${escapeHtml(vars.userName |
| 142 | <p>Thanks for signing up. Click the button below to verify your email address and activate your account.</p> |
| 143 | <p><a class="btn" href="${escapeHtml(vars.verificationUrl |
| 144 | <p style="font-size:12px;color:#a1a1aa;">Button not working? Copy and paste this link into your browser:<br><a href="${escapeHtml(vars.verificationUrl |
| 145 | ), |
| 146 | |
| 147 | "password-reset": (vars |
| 148 | baseTemplate( |
| 149 | vars.appName ?? |
| 150 | "Reset your password", |
| 151 | `<h2>Reset your password</h2> |
| 152 | <p>Hi ${escapeHtml(vars.userName |
| 153 | <p>We received a request to reset the password for your account. Click the button below to choose a new password. This link expires in 1 hour.</p> |
| 154 | <p><a class="btn" href="${escapeHtml(vars.resetUrl |
| 155 | <p style="font-size:12px;color:#a1a1aa;">If you didn't request a password reset, you can safely ignore this email. Your password won't change.<br><br>Button not working? Copy and paste this link:<br><a href="${escapeHtml(vars. |
| 156 | ), |
| 157 | |
| 158 | welcome: (vars) |
| 159 | baseTemplate( |
| 160 | vars.appName ?? |
| 161 | `Welcome to ${escapeHtml(vars |
| 162 | `<h2>Welcome to ${escapeHtml(vars |
| 163 | <p>Hi ${escapeHtml(vars.userName |
| 164 | <p>Your account has been created and is ready to use. You can now log in and start managing your servers.</p> |
| 165 | <p>If you have any questions, reach out to your panel administrator.</p>`, |
| 166 | ), |
| 167 | |
| 168 | "server-install": (vars |
| 169 | baseTemplate( |
| 170 | vars.appName ?? |
| 171 | "Server installation complete", |
| 172 | `<h2>Your server is ready</h2> |
| 173 | <p>Hi ${escapeHtml(vars.userName |
| 174 | <p>Your server <strong>${escapeHtml(vars.serverName |
| 175 | <p>Log in to your panel to start and manage your server.</p>`, |
| 176 | ), |
| 177 | |
| 178 | "change-email": (vars |
| 179 | baseTemplate( |
| 180 | vars.appName ?? |
| 181 | "Confirm your new email", |
| 182 | `<h2>Confirm your new email address</h2> |
| 183 | <p>Hi ${escapeHtml(vars.userName |
| 184 | <p>We received a request to change the email address on your account. Click the button below to confirm your new address. Your email will only be changed once you confirm it.</p> |
| 185 | <p><a class="btn" href="${escapeHtml(vars.verificationUrl |
| 186 | <p style="font-size:12px;color:#a1a1aa;">If you didn't request this change, you can safely ignore this email.<br><br>Button not working? Copy and paste this link into your browser:<br><a href="${escapeHtml(vars. |
| 187 | ), |
| 188 | }; |
| 189 | |
| 190 | export const TEMPLATE_VARIABLES: |
| 191 | verification: ["appName", |
| 192 | "password-reset": ["appName" |
| 193 | welcome: ["appName", |
| 194 | "server-install": ["appName" |
| 195 | "change-email": ["appName" |
| 196 | }; |