merge_type

code Python verified Free Download devices Cross-platform

code Code Preview

Python
#!/usr/bin/env python3
"""
Mail Merge Tool
Merge CSV data into email templates for personalized bulk emails
"""
import csv
from string import Template

def mail_merge(template_file, data_file, output_file):
    """Merge template with CSV data"""
    # Load template
    with open(template_file, 'r') as f:
        template = Template(f.read())

    # Load data
    with open(data_file, 'r') as f:
        reader = csv.DictReader(f)
        data = list(reader)

    # Generate personalized emails
    results = []
    for row in data:
        try:
            personalized = template.safe_substitute(row)
            results.append({
                'email': row.get('email', ''),
                'content': personalized,
                'status': 'success'
            })
        except Exception as e:
            results.append({
                'email': row.get('email', ''),
                'content': '',
                'status': f'error: {e}'
            })

    # Save results
    with open(output_file, 'w', newline='') as f:
        writer = csv.DictWriter(f, fieldnames=['email', 'content', 'status'])
        writer.writeheader()
        writer.writerows(results)

    return len([r for r in results if r['status'] == 'success'])

def preview_merge(template_str, sample_data):
    """Preview merge with sample data"""
    template = Template(template_str)
    return template.safe_substitute(sample_data)

if __name__ == '__main__':
    # Example usage
    sample = {'name': 'John', 'company': 'Acme Inc', 'role': 'CEO'}
    template = "Hi $name,\n\nAs $role at $company..."
    print(preview_merge(template, sample))

info About This Tool

The Mail Merge Tool combines your email templates with CSV data to create personalized messages at scale. Perfect for cold outreach and bulk email campaigns.

Key Features

  • Template Variables - Use $variable or ${variable} syntax
  • CSV Integration - Direct merge with spreadsheet data
  • Error Handling - Reports missing variables per row
  • Preview Mode - Test merge before full run
  • Batch Output - Export all personalized emails to CSV

Template Syntax

  • $name - Simple variable replacement
  • ${company} - Variable with braces
  • $first_name $last_name - Multiple variables

Requirements

  • Python 3.7+
  • No external dependencies

Tip: Always preview with sample data before running full merge to catch variable mismatches.

download Download Script

Need Full Automation?

Try Postigo for automated email campaigns with AI personalization

rocket_launch Start Free Trial