📧 Complete Guide: Integrating SendGrid with Open-source MediaWiki for Reliable Email Delivery
MediaWiki’s default email functionality often faces deliverability issues, especially in production environments where emails may end up in spam folders or fail to send entirely.
SendGrid, a cloud-based email delivery service, provides a robust solution with advanced features like analytics, bounce handling, and reputation management.
This guide walks you through integrating SendGrid with MediaWiki, covering common challenges and their solutions.
🔹 Why SendGrid for MediaWiki?
❌ Common MediaWiki Email Problems
✅ SendGrid Benefits
🔹 Prerequisites
Before starting, ensure you have:
🔹 Step 1: SendGrid Setup
1.1 Create SendGrid Account
1.2 Domain Authentication
Add these DNS records:
CNAME: s1._domainkey.yourdomain.com → s1.domainkey.u12345.wl.sendgrid.net
CNAME: s2._domainkey.yourdomain.com → s2.domainkey.u12345.wl.sendgrid.net
CNAME: em1234.yourdomain.com → u12345.wl.sendgrid.net
1.3 Create API Key
🔹 Step 2: Build a SendGrid Extension
Create directory structure:
extensions/SendGrid/
├── extension.json
├── includes/
│ ├── SendGridHooks.php
│ └── SendGridMailer.php
├── i18n/
│ └── en.json
├── test-email.php
└── README.md
Example: SendGridMailer.php
class SendGridMailer {
public function send($to, $from, $subject, $body, $headers = []) {
global $wgSendGridAPIKey, $wgSendGridFromEmail, $wgSendGridFromName;
$toEmail = is_array($to) ? $to[0]->address : $to->address;
$fromEmail = $wgSendGridFromEmail ?: $from->address;
$data = [
'personalizations' => [[
'to' => [['email' => $toEmail]]
]],
'from' => ['email' => $fromEmail, 'name' => $wgSendGridFromName],
'subject' => $subject,
'content' => [[
'type' => 'text/html',
'value' => $body
]]
];
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://api.sendgrid.com/v3/mail/send',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $wgSendGridAPIKey,
'Content-Type: application/json'
],
CURLOPT_RETURNTRANSFER => true
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $httpCode == 202;
}
}
🔹 Step 3: Configure MediaWiki In LocalSettings.php:
# Enable email
$wgEnableEmail = true;
$wgEnableUserEmail = true;
# Load SendGrid extension
wfLoadExtension('SendGrid');
# SendGrid Config
$wgSendGridAPIKey = 'YOUR_SENDGRID_API_KEY';
$wgSendGridFromEmail = 'noreply@yourdomain.com';
$wgSendGridFromName = 'Your Wiki';
👉 Use environment variables instead of hardcoding API keys:
Recommended by LinkedIn
$wgSendGridAPIKey = getenv('SENDGRID_API_KEY');
$wgSendGridFromEmail = getenv('SENDGRID_FROM_EMAIL');
🔹 Step 4: Testing Run test script:
php extensions/SendGrid/test-email.php --to=test@example.com
Test password reset via:
Special:PasswordReset
🔹 Step 5: Common Issues
❌ API Key Not Working
❌ Emails Going to Spam
❌ Rate Limiting
Implement small delays between sends
🔹 Step 6: Best Practices
🔹 Step 7: Advanced Features
✅ Conclusion
By integrating SendGrid with MediaWiki you unlock:
This solution has been tested in production on large MediaWiki deployments, handling thousands of daily emails with excellent reliability.
📚 Resources
🔔 If you found this useful, consider sharing it with others working on MediaWiki deployments!
Twilio SendGrid Partner - Caddycode MediaWiki #SendGrid #MediaWiki