Welcome to the Comprehensive Guide to Mailin API
Mailin offers one of the most efficient ways to manage your email campaigns, transactional emails, and more, through their extensive API. Below are dozens of useful API explanations complete with code snippets to help you get started.
Getting Started with Mailin API
Before you start, make sure to sign up for a Mailin account and obtain your API key.
1. Sending a Simple Email
import requests
url = "https://api.mailin.com/v2.0/email"
data = {
"to": "example@example.com",
"subject": "Test Email",
"content": "This is a test email.",
"from": "you@example.com"
}
headers = {
"api-key": "your_api_key_here",
"Content-Type": "application/json"
}
response = requests.post(url, json=data, headers=headers)
print(response.json())
2. Retrieving Campaign Statistics
import requests
url = "https://api.mailin.com/v2.0/statistics/campaign"
headers = {
"api-key": "your_api_key_here",
"Content-Type": "application/json"
}
response = requests.get(url, headers=headers)
print(response.json())
3. Managing Contacts
import requests
def add_contact(email, attributes):
url = "https://api.mailin.com/v2.0/contact"
data = {
"email": email,
"attributes": attributes
}
headers = {
"api-key": "your_api_key_here",
"Content-Type": "application/json"
}
response = requests.post(url, json=data, headers=headers)
return response.json()
contact = add_contact("newuser@example.com", {"FIRSTNAME": "John", "LASTNAME": "Doe"})
print(contact)
4. Sending Bulk Emails
import requests
url = "https://api.mailin.com/v2.0/email/bulk"
data = {
"to": ["user1@example.com", "user2@example.com"],
"subject": "Bulk Email Test",
"htmlContent": "This is a bulk email
",
"from": "you@example.com"
}
headers = {
"api-key": "your_api_key_here",
"Content-Type": "application/json"
}
response = requests.post(url, json=data, headers=headers)
print(response.json())
5. Creating and Scheduling Campaigns
import requests
url = "https://api.mailin.com/v2.0/campaign"
data = {
"name": "New Campaign",
"subject": "Campaign Subject",
"htmlContent": "This is the content of the campaign
",
"from_email": "you@example.com",
"list_id": "YOUR_LIST_ID",
"scheduled_date": "2023-12-01 10:00:00"
}
headers = {
"api-key": "your_api_key_here",
"Content-Type": "application/json"
}
response = requests.post(url, json=data, headers=headers)
print(response.json())
Building an App with Mailin API
Here’s a basic example of an application that sends a welcome email to new users and retrieves campaign statistics:
import requests
API_KEY = "your_api_key_here"
def send_welcome_email(email):
url = "https://api.mailin.com/v2.0/email"
data = {
"to": email,
"subject": "Welcome!",
"htmlContent": "Welcome to our service!
",
"from": "support@yourdomain.com"
}
headers = {
"api-key": API_KEY,
"Content-Type": "application/json"
}
response = requests.post(url, json=data, headers=headers)
return response.json()
def get_campaign_stats(campaign_id):
url = f"https://api.mailin.com/v2.0/statistics/campaign/{campaign_id}"
headers = {
"api-key": API_KEY,
"Content-Type": "application/json"
}
response = requests.get(url, headers=headers)
return response.json()
new_user_email = "newuser@example.com"
welcome_email_response = send_welcome_email(new_user_email)
print("Welcome Email Response:", welcome_email_response)
campaign_id = "YOUR_CAMPAIGN_ID"
campaign_stats_response = get_campaign_stats(campaign_id)
print("Campaign Stats Response:", campaign_stats_response)
Hash: 8751a09b8b924e71fd2fca7bcdcdfb8b372cb086c49a1f7e0aa72d821aeaa6f4