Email Integration with SendGrid
This boilerplate project includes SendGrid pre-installed, allowing you to easily send emails to your customers. Follow these steps to set up and use SendGrid in your application.
Getting Started
Step 1: Create a SendGrid Account
First, create an account on SendGrid (opens in a new tab).
Step 2: Add Your SendGrid API Key
Set up your SendGrid API key by adding it to your environment variables:
SENDGRID_API_KEY=your-sendgrid-api-key
Step 3: Create an Email Template on SendGrid
Create an email template on SendGrid to use for sending emails. Here is an example screenshot of a template creation process:
Step 4: Use the sendEmail
Function to Send Emails
You can now use the sendEmail
function to send emails anywhere in your server. Below is an example of how to send an email when someone likes a post.
import { sendEmail } from "../service/mailing-service";
{
...
likePost: protectedProcedure
.input(
z.object({
post_id: z.string(),
})
)
.mutation(async (opts) => {
const postCreator = await getPostCreator(opts.input.post_id); // Assuming you have a function to get post creator details
sendEmail({
to: postCreator.email,
from: "noreply@yourdomain.com",
subject: "Someone liked your post",
templateId: "your-sendgrid-template-id",
templateData: {
post_id: opts.input.post_id,
},
});
return {};
}),
...
}
Summary
By following these steps, you can easily integrate SendGrid into your project to send emails. Create an account, set up your API key, design email templates, and use the sendEmail
function to communicate with your users effectively.