SendGrid
Switching from Resend to SendGrid for sending emails.
If you prefer to use SendGrid instead of the default Resend provider for sending emails, follow these steps. Note that @resend/react is still used for rendering email templates regardless of the sending provider.
1. Package Management
First, uninstall the resend sending package (if installed) and install the SendGrid package. Keep @resend/react for template rendering.
2. Configuration Update (lib/app-config.ts)
Add a sendgrid configuration object to your lib/app-config.ts file. You can remove or comment out the resend object if you won't be using it for sending.
Critical Setup
Update fromNoReply, fromAdmin, and supportEmail with your actual domain
and desired email addresses. The optional subdomain should match the
domain/subdomain you verified in Sendgrid and configured in your DNS.
3. Replace Email Sending Helper (lib/sendgrid.ts)
If you have a lib/resend.ts specifically for sending emails, delete it:
Create a new file named lib/sendgrid.ts and add the following code to handle sending via SendGrid:
4. Update Environment Variables
Update your .env.local file. Remove or comment out AUTH_RESEND_KEY and add your AUTH_SENDGRID_KEY:
5. Update Authentication Provider (lib/auth/auth-config.ts)
Modify your NextAuth configuration in lib/auth/auth-config.ts to use the SendGrid provider for Magic Links:
6. SendGrid Account Setup
Follow these steps to set up your SendGrid account for sending:
- Create Account: Sign up at SendGrid.
- Sender Authentication: Set up Domain Authentication (recommended) or Single Sender Verification. Follow SendGrid's guides to add the necessary DNS records (CNAMEs) for your domain or subdomain (e.g.,
mail.yourdomain.com). - Create API Key: Go to Settings > API Keys > Create API Key. Name it (e.g.,
YourAppName Production) and assignMail Sendpermissions (Full Access also works). Copy the key immediately. - Store API Key: Add the key to your
.env.localasAUTH_SENDGRID_KEY(as done in step 4).
Sending & Receiving
- Sending: The
sendEmailfunction inlib/sendgrid.tsnow handles sending via the SendGrid API. - Receiving: To handle replies, ensure the
reply_tooption is set correctly when sending emails. Our centralizedsendEmailfunction usually handles this by settingreply_toto thesupportEmaildefined in yourapp-config.ts.
Checklist to Avoid Spam Folder (SendGrid)
- Verified Sender: Ensure your sending domain/subdomain or single sender is fully authenticated in SendGrid.
- Proper DNS: Correct CNAME/MX records for SendGrid domain authentication are essential. Implement SPF, DKIM, and DMARC.
- IP Reputation: Monitor your sender reputation using SendGrid's tools. Consider a dedicated IP for high-volume sending.
- Reputation: Maintain a good sender reputation by:
- Sending relevant, expected content.
- Managing bounces, blocks, and spam reports promptly through SendGrid's suppression lists or via webhooks.
- Warming up new domains/IPs gradually.
- Content: Avoid spammy content, excessive links, misleading subject lines, and large attachments.
- List Hygiene: Regularly clean your email lists to remove inactive or invalid addresses.
- Unsubscribe Link: Always include a clear and functional unsubscribe link, ideally using SendGrid's subscription tracking.