|
| 1 | +--- |
| 2 | +title: "Send email using Mailchimp Transactional" |
| 3 | +sidebarTitle: "Mailchimp Transactional" |
| 4 | +description: "Learn how to send an email using React Email and the Mailchimp Transactional Node.js SDK." |
| 5 | +"og:image": "https://react.email/static/covers/react-email.png" |
| 6 | +--- |
| 7 | + |
| 8 | +## 1. Install dependencies |
| 9 | + |
| 10 | +Get the [react-email](https://www.npmjs.com/package/react-email) package and the [Mailchimp Transactional Node.js SDK](https://www.npmjs.com/package/@mailchimp/mailchimp_transactional). |
| 11 | + |
| 12 | +<CodeGroup> |
| 13 | + |
| 14 | +```sh npm |
| 15 | +npm install @mailchimp/mailchimp_transactional react-email |
| 16 | +``` |
| 17 | + |
| 18 | +```sh yarn |
| 19 | +yarn add @mailchimp/mailchimp_transactional react-email |
| 20 | +``` |
| 21 | + |
| 22 | +```sh pnpm |
| 23 | +pnpm add @mailchimp/mailchimp_transactional react-email |
| 24 | +``` |
| 25 | + |
| 26 | +```sh bun |
| 27 | +bun add @mailchimp/mailchimp_transactional react-email |
| 28 | +``` |
| 29 | + |
| 30 | +</CodeGroup> |
| 31 | + |
| 32 | +## 2. Create an email using React |
| 33 | + |
| 34 | +Start by building your email template in a `.jsx` or `.tsx` file. |
| 35 | + |
| 36 | +```tsx email.tsx |
| 37 | +import { Button, Html } from "react-email"; |
| 38 | + |
| 39 | +interface EmailProps { |
| 40 | + url: string; |
| 41 | +} |
| 42 | + |
| 43 | +export function Email({ url }: EmailProps) { |
| 44 | + return ( |
| 45 | + <Html lang="en"> |
| 46 | + <Button href={url}>Click me</Button> |
| 47 | + </Html> |
| 48 | + ); |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +## 3. Convert to HTML and send email |
| 53 | + |
| 54 | +Import the email template you just built, convert into an HTML string, and use the Mailchimp Transactional SDK to send it. |
| 55 | + |
| 56 | +```tsx |
| 57 | +import mailchimpTransactional from "@mailchimp/mailchimp_transactional"; |
| 58 | +import { render } from "react-email"; |
| 59 | +import { Email } from "./email"; |
| 60 | + |
| 61 | +const mailchimp = mailchimpTransactional(process.env.MAILCHIMP_API_KEY || ""); |
| 62 | + |
| 63 | +const emailHtml = await render(<Email url="https://example.com" />); |
| 64 | + |
| 65 | +await mailchimp.messages.send({ |
| 66 | + message: { |
| 67 | + subject: "Hello world", |
| 68 | + from_email: "hello@example.com", |
| 69 | + to: [{ email: "hello@example.com", type: "to" }], |
| 70 | + html: emailHtml, |
| 71 | + }, |
| 72 | +}); |
| 73 | +``` |
| 74 | + |
| 75 | +## Try it yourself |
| 76 | + |
| 77 | +<Card |
| 78 | + title="Mailchimp Transactional example" |
| 79 | + icon="arrow-up-right-from-square" |
| 80 | + iconType="duotone" |
| 81 | + href="https://github.com/resend/react-email/tree/main/examples/mailchimp-transactional" |
| 82 | +> |
| 83 | + See the full source code. |
| 84 | +</Card> |
0 commit comments