Skip to content

Instantly share code, notes, and snippets.

@theinhumaneme
Last active December 17, 2024 08:59
Show Gist options
  • Save theinhumaneme/7ef03e49e21efa21d373e48a132fef7e to your computer and use it in GitHub Desktop.
Save theinhumaneme/7ef03e49e21efa21d373e48a132fef7e to your computer and use it in GitHub Desktop.
mailgun template migration to different accounts
import json
import requests
# Path to the JSON file containing the template data
TEMPLATES_FILE = "templates.json"
# Source and destination Mailgun API keys and domains
SOURCE_API_KEY = '...'
SOURCE_DOMAIN = '...'
DEST_API_KEY = '...'
DEST_DOMAIN = '...'
# Dry Run Flag (set to True for dry run, False to push templates)
DRY_RUN = False
# Read the JSON file containing template details (if it's already available)
def read_templates_file():
with open(TEMPLATES_FILE, 'r') as f:
return json.load(f)
# Function to download all templates from the source Mailgun account
def download_templates():
url = f"https://api.mailgun.net/v3/{SOURCE_DOMAIN}/templates"
response = requests.get(url, auth=("api", SOURCE_API_KEY))
if response.status_code == 200:
templates_data = response.json()
templates = templates_data.get("items", [])
# Save the templates to a JSON file
with open(TEMPLATES_FILE, 'w') as f:
json.dump(templates_data, f, indent=2)
print(f"Successfully downloaded {len(templates)} templates from the source account.")
else:
print(f"Error downloading templates: {response.status_code}")
# Function to fetch template content from the source account
def fetch_template_content(template_name):
url = f"https://api.mailgun.net/v3/{SOURCE_DOMAIN}/templates/{template_name}?active=yes"
response = requests.get(url, auth=("api", SOURCE_API_KEY))
print(response.status_code)
if response.status_code == 200:
template_data = response.json()
template_html = template_data.get('template').get('version').get('template', '')
return template_html
else:
print(f"Error fetching template {template_name}: {response.status_code}")
return "", "" # Return empty content in case of an error
# Function to create a template on the destination Mailgun account
def create_template(template_name, template_description, template_html):
url = f"https://api.mailgun.net/v3/{DEST_DOMAIN}/templates"
data = {
'name': template_name,
'description': template_description,
'template': template_html,
'engine': "handlebars"
}
if DRY_RUN:
print(f"Dry Run: Creating template '{template_name}'")
print(f"Description: {template_description}")
print(f"HTML Content: {template_html}")
print("-----------------------------------------------")
else:
response = requests.post(url, auth=("api", DEST_API_KEY), data=data)
if response.status_code == 200:
print(f"Successfully created template '{template_name}' on destination account.")
else:
print(f"Error creating template {template_name}: {response.status_code}")
# If the templates file doesn't exist, download the templates
if not os.path.exists(TEMPLATES_FILE):
print("Templates file not found. Downloading templates...")
download_templates()
# Read the templates
templates = read_templates_file()
# Loop through the templates and create them on the destination account
for template in templates.get('items', []):
template_name = template.get('name')
template_description = template.get('description')
# Debugging: Print extracted template name and description
print(f"Extracted template name: '{template_name}', description: '{template_description}'")
# Fetch the template content from the source account
template_html = fetch_template_content(template_name)
# Create the template on the destination account
create_template(template_name, template_description, template_html)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment