Mailchimp Setup

Learn how to set up Mailchimp for HTML email delivery

Setting Up Mailchimp ๐Ÿ“ง

This guide will help you set up Mailchimp for sending HTML emails in your application.

Prerequisites ๐Ÿ“‹

  1. A Mailchimp account
  2. Access to your domain's DNS settings
  3. Domain verified in Mailchimp
  4. API key with send permissions

Domain Verification ๐Ÿ”

  1. Go to Mailchimp Dashboard
  2. Navigate to Account > Domains
  3. Add and verify your domain
  4. Add the provided DNS records to your domain settings

Implementation ๐Ÿ’ป

Install required dependencies:

pnpm add @mailchimp/mailchimp_transactional

Create src/lib/email/sendMail.ts:

import mailchimp from "@mailchimp/mailchimp_transactional";
import { appConfig } from "../config";

const sendMail = async (to: string, subject: string, html: string) => {
  if (process.env.NODE_ENV !== "production") {
    console.log(
      "Sending email to",
      to,
      "with subject",
      subject,
      "and html",
      html
    );
    return;
  }

  const client = mailchimp(process.env.MAILCHIMP_API_KEY);

  const message = {
    html: html,
    subject: subject,
    from_email: appConfig.email.senderEmail,
    from_name: appConfig.email.senderName,
    to: [
      {
        email: to,
        type: "to"
      }
    ],
    headers: {
      "Reply-To": appConfig.email.senderEmail
    }
  };

  const response = await client.messages.send({ message });
  console.log("Email sent successfully", response);
};

export default sendMail;

Environment Variables ๐Ÿ”

Add these to your .env.local:

MAILCHIMP_API_KEY=your_api_key

Testing Email Setup ๐Ÿงช

  1. Send a test email using Mailchimp dashboard
  2. Monitor delivery in Mailchimp Activity Feed
  3. Check email headers for proper authentication

Important Notes โš ๏ธ

  1. DNS propagation takes time (24-48 hours)
  2. Monitor email reputation in Mailchimp dashboard
  3. Keep bounce rate below recommended threshold
  4. Use production API key in production
  5. Only HTML emails are supported

Remember to wait for DNS propagation before testing your email setup. Rushing this process can lead to delivery issues! ๐Ÿš€