- 
                Notifications
    You must be signed in to change notification settings 
- Fork 81
feat: add welcome email to be sent to new registering user #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,108 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| require("dotenv").config(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let sendRaw; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Validate critical env vars | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!process.env.SENDGRID_API_KEY && (!process.env.EMAIL_USER || !process.env.EMAIL_PASS)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.warn( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "Warning: No email provider fully configured. Emails may fail to send. " + | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "Set SENDGRID_API_KEY for SendGrid or EMAIL_USER/EMAIL_PASS for SMTP." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const emailFrom = process.env.EMAIL_FROM || process.env.EMAIL_USER || "no-reply@paisable.com"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Use SendGrid if API key is available | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (process.env.SENDGRID_API_KEY) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const sgMail = require("@sendgrid/mail"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sgMail.setApiKey(process.env.SENDGRID_API_KEY); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sendRaw = async ({ to, from, subject, text, html }) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const msg = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| to, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from: from || emailFrom, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| subject, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| text, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| html, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return await sgMail.send(msg); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (err) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.error("SendGrid email failed", { to, subject, message: err.message }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Fallback to SMTP via Nodemailer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const nodemailer = require("nodemailer"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const transport = nodemailer.createTransport({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| host: process.env.SMTP_HOST || "smtp.gmail.com", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| port: process.env.SMTP_PORT ? Number(process.env.SMTP_PORT) : 587, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| secure: process.env.SMTP_SECURE === "true", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| auth: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| user: process.env.EMAIL_USER, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pass: process.env.EMAIL_PASS, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pool: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| maxConnections: 5, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| maxMessages: 100, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| transport.verify() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .then(() => console.info("SMTP transporter verified")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .catch(err => console.warn("SMTP transporter verification failed", { message: err.message })); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sendRaw = async ({ to, from, subject, text, html }) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 
      Comment on lines
    
      +51
     to 
      +56
    
   
     | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| transport.verify() | |
| .then(() => console.info("SMTP transporter verified")) | |
| .catch(err => console.warn("SMTP transporter verification failed", { message: err.message })); | |
| sendRaw = async ({ to, from, subject, text, html }) => { | |
| try { | |
| // Ensure transporter is verified before sending emails | |
| let smtpVerified = false; | |
| let smtpVerifyPromise = null; | |
| sendRaw = async ({ to, from, subject, text, html }) => { | |
| try { | |
| // Verify transporter once before first send | |
| if (!smtpVerified) { | |
| if (!smtpVerifyPromise) { | |
| smtpVerifyPromise = transport.verify() | |
| .then(() => { | |
| smtpVerified = true; | |
| console.info("SMTP transporter verified"); | |
| }) | |
| .catch(err => { | |
| console.warn("SMTP transporter verification failed", { message: err.message }); | |
| throw err; | |
| }); | |
| } | |
| await smtpVerifyPromise; | |
| } | 
    
      
    
      Copilot
AI
    
    
    
      Oct 4, 2025 
    
  
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The sendRaw function for SMTP catches errors but doesn't re-throw them, causing the function to return undefined instead of propagating the error. This makes error handling inconsistent with the caller's expectations in sendWelcomeEmail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The sendRaw function for SendGrid catches errors but doesn't re-throw them, causing the function to return undefined instead of propagating the error. This makes error handling inconsistent with the caller's expectations in sendWelcomeEmail.