如何用PHP生成HTML文档适配Mailchimp邮件模板批量修改需求
Hey there! Totally get your frustration with manually updating Mailchimp templates—those repetitive edits eat up way too much time. Let’s walk through two solid solutions, starting with the PHP approach you’re curious about, plus an even simpler built-in Mailchimp trick that might save you extra work.
1. Use PHP to Generate Pure HTML Files
Since Mailchimp only accepts static HTML, we’ll use PHP to dynamically replace your template’s placeholder content and output ready-to-import HTML files. Here’s how to do it step by step:
Step 1: Prepare Your Template with Placeholders
First, modify your mailing-template.html to add clear placeholders for the content you want to swap out. For example:
<!-- mailing-template.html --> <!DOCTYPE html> <html> <head> <title>Product Update</title> </head> <body> <div class="product-card"> <h1>{{PRODUCT_NAME}}</h1> <p>{{PRODUCT_DETAILS}}</p> <a href="{{PRODUCT_LINK}}">Shop Now</a> </div> </body> </html>
Use placeholder names that make sense (like {{PRODUCT_NAME}}) so you can easily map them to your data later.
Step 2: Write a Simple PHP Script to Generate HTML
Create a new file (e.g., generate-mailers.php) in the same folder as your template. This script will read the template, replace placeholders with your product data, and save individual HTML files:
<?php // Read your base template file $template = file_get_contents('mailing-template.html'); // Add your product data here (you can also load this from a CSV or database later!) $productData = [ [ 'name' => 'Wireless Earbuds X', 'details' => 'Waterproof earbuds with 24-hour battery life and touch controls.', 'link' => 'https://yourstore.com/earbuds-x' ], [ 'name' => 'Smart Watch Series 3', 'details' => 'Fitness tracker with heart rate monitor and sleep tracking.', 'link' => 'https://yourstore.com/smartwatch-3' ] ]; // Loop through each product to generate a unique HTML file foreach ($productData as $index => $product) { // Replace placeholders with actual product data $finalHtml = str_replace( ['{{PRODUCT_NAME}}', '{{PRODUCT_DETAILS}}', '{{PRODUCT_LINK}}'], [$product['name'], $product['details'], $product['link']], $template ); // Save the final HTML file $outputFile = 'mailer-product-' . ($index + 1) . '.html'; file_put_contents($outputFile, $finalHtml); echo "Generated file: $outputFile<br>"; } ?>
Step 3: Run the Script and Import to Mailchimp
- If you’re using a local server (like XAMPP), open the script in your browser (e.g.,
http://localhost/generate-mailers.php). - Or run it via command line:
php generate-mailers.php - You’ll get a set of pure HTML files—just upload these to Mailchimp like you normally would!
2. Even Easier: Use Mailchimp’s Built-in Merge Tags
If you don’t want to mess with PHP at all, Mailchimp has a native feature called Merge Tags that lets you batch-populate templates without writing code. This is perfect for beginners:
How to Use Merge Tags:
- Create a Template in Mailchimp: When building your template, replace dynamic content with Mailchimp-style merge tags (e.g.,
*|PRODUCT_NAME|*,*|PRODUCT_DETAILS|*). These tags are case-sensitive, so stick to uppercase. - Prepare a CSV File: Make a CSV with columns matching your merge tags (e.g.,
PRODUCT_NAME,PRODUCT_DETAILS,PRODUCT_LINK). Each row will be a unique product entry. - Import and Merge: When creating a campaign in Mailchimp, select your template, then import your CSV. Mailchimp will automatically replace the merge tags with the corresponding data from your CSV.
Which Solution Should You Choose?
- PHP Generated HTML: Best if you need advanced customization (like conditional content, complex formatting logic) or if you already have product data stored in a database.
- Mailchimp Merge Tags: Best for beginners who want a no-code solution—faster to set up and requires zero coding knowledge.
Either way, you’ll cut down that manual editing time drastically!
内容的提问来源于stack exchange,提问作者Izzy

