如何基于Mailgun/Node.js高效批量发送50k+个性化邮件?
Efficiently Sending 50k+ Personalized Emails (Without 50k API Calls)
Great question—sending large volumes of personalized emails without spamming an API thousands of times is a classic scalability problem, and there are straightforward fixes whether you stick with Mailgun or switch providers. Let’s break this down:
Option 1: Switch to a Provider Built for Batch Personalization
Many email services support bulk personalized sending (often called "mail merge" or "batch send with dynamic content") in a single API request. Here are the most reliable options:
- SendGrid Batch Send: Their API lets you include an array of
personalizationsin one request, where each entry can have a unique recipient, subject line, and dynamic content. You’d define a template (or inline content with placeholders) and pass per-recipient data to fill in[name]and other variables. This cuts your API calls from 50k to just a handful (depending on batch size limits—SendGrid allows up to 1k recipients per batch). - Amazon SES Template + Bulk Send: SES supports email templates with variables (like
{{name}}). You can send a single API request with a list of recipients and their corresponding variable data, and SES handles the personalization and delivery. This is cost-effective for large volumes. - Mailchimp API: While it’s primarily a marketing platform, Mailchimp’s API lets you create campaigns with personalized content using merge tags. You can upload your user list (or pass it via API) and trigger a single campaign send that handles all personalization automatically.
Option 2: Optimize Mailgun (No Provider Switch Needed)
You don’t have to abandon Mailgun—they do support bulk personalized sending, just not via the basic single-recipient send API. Here’s how to use their tools:
Use Mailgun Templates + Recipient Variables
- First, create an email template in your Mailgun dashboard:
- Subject:
Hello {{name}} - Body:
How are you {{name}}? [Your other text here]
- Subject:
- Use Mailgun’s batch send endpoint with
recipient-variablesto pass per-user data in one request. Here’s a quick Python example:import requests import json MAILGUN_DOMAIN = "your-domain.com" MAILGUN_API_KEY = "your-api-key" def send_mailgun_batch(): url = f"https://api.mailgun.net/v3/{MAILGUN_DOMAIN}/messages" auth = ("api", MAILGUN_API_KEY) # Prepare recipient variables: map email to personalized data recipient_vars = {} # Replace this with your actual user list (split into batches of ~1k-2k recipients) for user in your_user_list[:2000]: recipient_vars[user["email"]] = {"name": user["name"]} data = { "from": "Your Team <noreply@your-domain.com>", "to": list(recipient_vars.keys()), "template": "your-template-name", # Name of your Mailgun template "recipient-variables": json.dumps(recipient_vars) } response = requests.post(url, auth=auth, data=data) if response.status_code == 200: print("Batch sent successfully!") else: print(f"Error: {response.json()}") - Split your 50k users into batches (Mailgun recommends 1k-2k recipients per batch) to stay within API limits. This reduces your total requests from 50k to 25-50, which is way more efficient.
General Best Practices
- Asynchronous Processing: Don’t send batches synchronously in your main application. Use a task queue (like Celery for Python, Bull for Node.js) to handle sending in the background, so your app doesn’t hang.
- Error Handling: Add retry logic for failed batches, and monitor delivery statuses (all providers offer webhooks to track bounces, opens, and clicks).
- Deliverability: Make sure your domain has proper DKIM, SPF, and DMARC records set up to avoid being marked as spam. Test with a small batch of 10-20 users first to validate personalization and deliverability.
内容的提问来源于stack exchange,提问作者Peter Moses
相关产品推荐
相关产品推荐

