Problem: AWS Lightsail Email Issues – "Why Can’t I Send Mail from Lightsail?"
Problem: AWS Lightsail Email Issues – "Why Can’t I Send Mail from Lightsail?"
Common Symptoms:
# SMTP Connection Error:
$ telnet smtp.gmail.com 25
# Connection times out, indicating port 25 is blocked.
# PHP Mail Not Working:
mail() failed to execute: No SMTP connection available.
# Postfix/Sendmail Logs:
status=deferred (connect to smtp.example.com[203.0.113.10]:25:
Connection timed out)
Issue:
AWS blocks outbound SMTP traffic on port 25 to prevent spam, affecting mail servers on Lightsail. Common reasons email fails:
- Port 25 is blocked – AWS restricts direct mail sending over SMTP.
- Mail server misconfiguration – Postfix, Exim, or Sendmail may need adjustments.
- No SPF/DKIM records – Email providers may reject mail from unverified senders.
- Hosting email from Lightsail – Not recommended; Amazon SES or third-party services work better.
Fix: Sending Email from AWS Lightsail
# Step 1: Verify If Port 25 Is Blocked
$ telnet smtp.gmail.com 25
# If connection times out, port 25 is blocked.
# Step 2: Use a Different SMTP Port (587 or 465)
# Configure Postfix to use port 587 with authentication:
$ sudo nano /etc/postfix/main.cf
# Update or add:
relayhost = [smtp-relay.gmail.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_security_options = noanonymous
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
# Step 3: Authenticate SMTP Relay (Gmail Example)
$ echo "[smtp-relay.gmail.com]:587 \
user@gmail.com:password" | sudo tee \
/etc/postfix/sasl_passwd
$ sudo postmap /etc/postfix/sasl_passwd
$ sudo chmod 600 /etc/postfix/sasl_passwd \
/etc/postfix/sasl_passwd.db
$ sudo systemctl restart postfix
# Step 4: Use Amazon SES as a Relay Instead
$ sudo nano /etc/postfix/main.cf
# Replace relayhost:
relayhost = [email-smtp.us-east-1.amazonaws.com]:587
# Set up authentication:
$ echo "[email-smtp.us-east-1.amazonaws.com]:587 \
SMTP_USERNAME:SMTP_PASSWORD" | sudo tee \
/etc/postfix/sasl_passwd
$ sudo postmap /etc/postfix/sasl_passwd
$ sudo systemctl restart postfix
# Step 5: Configure SPF, DKIM, and DMARC in DNS
# Add SPF record to Lightsail DNS:
$ aws lightsail create-domain-entry \
--domain-name "example.com" \
--domain-entry name="_spf",type="TXT",\
target="v=spf1 include:amazonses.com -all"
# Enable DKIM for SES:
$ aws ses verify-domain-dkim \
--domain-name "example.com"
# Step 6: Test Email Sending
$ echo "Test email from Lightsail" | mail -s \
"Lightsail SMTP Test" recipient@example.com
# If successful, email should arrive in inbox or spam folder.
# Step 7: Restart Mail Services if Needed
$ sudo systemctl restart postfix
$ sudo systemctl restart sendmail
Need AWS Expertise?
If you're looking for guidance on Amazon Lightsail or any cloud challenges, feel free to reach out! We'd love to help you tackle your Lightsail projects. 🚀
Email us at: info@pacificw.com
Image: Gemini
Comments
Post a Comment