Setting Up SMTP Relay Servers for High-Volume Email Delivery
Setting Up SMTP Relay Servers for High-Volume Email Delivery
When dealing with high-volume email campaigns, a dedicated SMTP relay server becomes essential. I've set up multiple SMTP relay configurations on cloud VPS to bypass provider limits and ensure reliable delivery.
π― Why SMTP Relay?
Common email provider limits:
- Gmail: 500 emails/day (free), 2000/day (Workspace)
- GoDaddy: 250 emails/hour
- Shared hosting: Often 50-100 emails/hour
π Architecture
``` Laravel Application β SMTP Relay Server β Recipient Mail Servers ```
Benefits
- No daily limits (within reason)
- Better deliverability with proper SPF/DKIM
- Full control over sending
- Cost-effective for high volume
π¦ Installation: Postfix on Debian
1. Install Postfix
```bash sudo apt update sudo apt install postfix mailutils
During installation, select:
- Internet Site
- Your domain name (e.g., ameylokare.com)
```
2. Basic Configuration
```bash
/etc/postfix/main.cf
myhostname = mail.ameylokare.com mydomain = ameylokare.com myorigin = $mydomain inet_interfaces = all inet_protocols = ipv4 mydestination = $myhostname, localhost.$mydomain, $mydomain relayhost = ```
3. Configure as Relay
```bash
/etc/postfix/main.cf
Allow relay from your application servers
mynetworks = 127.0.0.0/8, [::ffff:127.0.0.0]/104, [::1]/128, YOUR_APP_SERVER_IP/32
Authentication (if relaying through another server)
smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd smtp_sasl_security_options = noanonymous ```
π Authentication Setup
For Relaying Through Another Server
```bash
/etc/postfix/sasl_passwd
[smtp.gmail.com]:587 your-email@gmail.com:your-app-password
Create hash database
sudo postmap /etc/postfix/sasl_passwd
Secure the file
sudo chmod 600 /etc/postfix/sasl_passwd ```
Update main.cf
```bash
/etc/postfix/main.cf
relayhost = [smtp.gmail.com]:587 smtp_tls_security_level = encrypt smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt ```
π High-Volume Configuration
1. Increase Connection Limits
```bash
/etc/postfix/main.cf
default_process_limit = 100 smtpd_client_connection_count_limit = 10 smtpd_client_connection_rate_limit = 30 smtpd_client_message_rate_limit = 30 ```
2. Queue Management
```bash
/etc/postfix/main.cf
maximal_queue_lifetime = 5d maximal_backoff_time = 4000s minimal_backoff_time = 300s queue_run_delay = 300s ```
3. Performance Tuning
```bash
/etc/postfix/main.cf
Increase memory for large queues
message_size_limit = 10240000 # 10MB mailbox_size_limit = 0 # Unlimited
Connection timeouts
smtp_connect_timeout = 30s smtp_helo_timeout = 300s ```
π§ Laravel Configuration
.env Setup
```env MAIL_MAILER=smtp MAIL_HOST=mail.ameylokare.com MAIL_PORT=587 MAIL_USERNAME=your-email@ameylokare.com MAIL_PASSWORD=your-password MAIL_ENCRYPTION=tls MAIL_FROM_ADDRESS="noreply@ameylokare.com" MAIL_FROM_NAME="${APP_NAME}" ```
config/mail.php
```php 'smtp' => [ 'transport' => 'smtp', 'host' => env('MAIL_HOST', 'mail.ameylokare.com'), 'port' => env('MAIL_PORT', 587), 'encryption' => env('MAIL_ENCRYPTION', 'tls'), 'username' => env('MAIL_USERNAME'), 'password' => env('MAIL_PASSWORD'), 'timeout' => null, 'auth_mode' => null, ], ```
π Security Hardening
1. Firewall Rules
```bash
Allow only necessary ports
sudo ufw allow 25/tcp # SMTP sudo ufw allow 587/tcp # Submission sudo ufw allow 465/tcp # SMTPS ```
2. Restrict Access
```bash
/etc/postfix/main.cf
Only allow connections from your app servers
smtpd_client_restrictions = permit_mynetworks, reject
Prevent open relay
smtpd_recipient_restrictions = permit_mynetworks, reject_unauth_destination ```
3. SPF Record
Add to DNS:
``` TXT @ "v=spf1 ip4:YOUR_SERVER_IP include:_spf.google.com ~all" ```
4. DKIM Setup
```bash
Install OpenDKIM
sudo apt install opendkim opendkim-tools
Generate keys
sudo opendkim-genkey -t -s default -d ameylokare.com
Configure
/etc/opendkim.conf
Domain ameylokare.com KeyFile /etc/opendkim/keys/default.private Selector default ```
Add DNS record:
``` default._domainkey.ameylokare.com TXT "v=DKIM1; k=rsa; p=YOUR_PUBLIC_KEY" ```
π Monitoring & Logs
View Mail Queue
```bash
Check queue status
sudo postqueue -p
View specific message
sudo postcat -q QUEUE_ID
Flush queue (send all pending)
sudo postqueue -f ```
Logs
```bash
View mail logs
sudo tail -f /var/log/mail.log
Filter for errors
sudo grep "error" /var/log/mail.log
Check delivery status
sudo grep "status=sent" /var/log/mail.log ```
π Performance Optimization
1. Connection Pooling
Reuse SMTP connections:
```php // In Laravel Mail config 'stream' => [ 'ssl' => [ 'verify_peer' => false, 'verify_peer_name' => false, ], ], ```
2. Queue Processing
Send emails via queue:
```php // Dispatch to queue Mail::to($user)->queue(new WelcomeEmail($user));
// Run queue worker php artisan queue:work --queue=emails ```
3. Rate Limiting
Limit emails per minute:
```php // In job public function handle() { // Send email Mail::to($this->subscriber)->send(new CampaignEmail());
// Rate limit: 100 emails per minute sleep(0.6); // 60 seconds / 100 = 0.6s per email } ```
π‘ Real-World Example
I set up an SMTP relay for a campaign sending 50,000 emails:
1. Postfix on Contabo VPS (4GB RAM, 2 vCPU) 2. Laravel queues process emails in background 3. Rate limiting at 100 emails/minute 4. SPF/DKIM configured for deliverability 5. Monitoring via mail logs and queue status
Result: 99.5% delivery rate, no provider limits, cost-effective at scale.
π Key Takeaways
- Postfix is reliable for SMTP relay
- Configure SPF/DKIM for better deliverability
- Use queues to avoid timeouts
- Rate limit to prevent blacklisting
- Monitor logs for delivery issues
- Secure with firewall and access restrictions
Conclusion
A dedicated SMTP relay server gives you control and scalability for high-volume email delivery. With proper configuration, security, and monitoring, you can reliably send thousands of emails without hitting provider limits.