ตั้ง Postfix SMTP Relay ส่ง Email จาก VPS
Web application ส่วนใหญ่ต้องส่ง email เช่น password reset, order confirmation หรือ notification Postfix เป็น MTA (Mail Transfer Agent) ยอดนิยมที่ตั้งเป็น SMTP relay ได้ง่าย ส่งต่อ email ออกผ่าน SMTP service ภายนอกเพื่อให้ email ไม่ตกใน spam
ติดตั้ง Postfix
sudo apt update
sudo apt install postfix mailutils -y
# เมื่อถามประเภท เลือก "Internet Site" และใส่ hostname ของ server
ตั้งค่า Relay ผ่าน Gmail SMTP
sudo nano /etc/postfix/main.cf
# เพิ่มหรือแก้บรรทัดเหล่านี้
relayhost = [smtp.gmail.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
# สร้างไฟล์ password
sudo nano /etc/postfix/sasl_passwd
# ใส่:
[smtp.gmail.com]:587 [email protected]:YOUR_APP_PASSWORD
# แปลงเป็น hash และตั้ง permission
sudo postmap /etc/postfix/sasl_passwd
sudo chmod 600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
# Restart postfix
sudo systemctl restart postfix
ทดสอบส่ง Email
# ส่ง test email
echo "Test from VPS $(hostname)" | mail -s "Postfix Test" [email protected]
# ดู log ว่าส่งสำเร็จหรือไม่
sudo tail -f /var/log/mail.log
ตั้งค่า PHP mail() ให้ใช้ Postfix
sudo nano /etc/php/8.3/fpm/php.ini
# ตั้ง sendmail_path
sendmail_path = /usr/sbin/sendmail -t -i
# หรือใช้ SMTP โดยตรง (ถ้าใช้ PHPMailer)
# MAIL_HOST=localhost
# MAIL_PORT=25
ตรวจสอบว่า Postfix ทำงานถูกต้อง
# ตรวจสถานะ service
systemctl status postfix
# ดู mail queue
mailq
# ดู log real-time
sudo tail -f /var/log/mail.log
# ทดสอบ SMTP connection
telnet smtp.gmail.com 587
แก้ปัญหาที่พบบ่อย
Error: "SASL authentication failed" ใน mail.log
Password ผิดหรือ App Password ไม่ถูกต้อง ต้องสร้าง App Password ใหม่และอัพเดทไฟล์:
ทดสอบจากมุมมองภายนอกด้วย SMTP Diagnostic ของ dnsxray.com ที่ต่อ SMTP จริงไปยัง MX ของโดเมน เช็ค Banner, EHLO, STARTTLS, PTR และทดสอบว่าเผลอเปิด Open Relay หรือไม่ — จุดที่ทดสอบเองจากในเครื่องไม่เห็น
# อัพเดท password
sudo nano /etc/postfix/sasl_passwd
# แก้ password ใหม่
sudo postmap /etc/postfix/sasl_passwd
sudo systemctl reload postfix
Email ส่งได้แต่ตก spam ทุกครั้ง
IP ของ VPS อาจอยู่ใน blacklist หรือ domain ไม่มี SPF/DKIM record ตรวจ IP reputation ก่อน:
# ตรวจว่า IP อยู่ใน blacklist ไหม
# ใช้ mxtoolbox.com/blacklists หรือ
curl -s "https://www.stopforumspam.com/api?ip=YOUR_VPS_IP&f=json"
Postfix ส่ง email ไม่ออก port 25 ถูก block
VPS provider ส่วนใหญ่ block port 25 ออก เพราะ spam ต้องใช้ relay ผ่าน port 587 (STARTTLS) หรือ 465 (SSL) แทน:
# ตรวจว่า port 25 ออกได้ไหม
nc -zv smtp.gmail.com 25 2>&1
nc -zv smtp.gmail.com 587 2>&1
# ถ้า port 25 fail แต่ 587 pass = ต้องใช้ relay เท่านั้น
PHP mail() ส่งแล้ว return true แต่ไม่ถึงปลายทาง
mail() return true แค่ว่าส่งถึง MTA ไม่ใช่ว่า deliver สำเร็จ ดู mail.log และตรวจ queue:
# ดู mail queue
mailq
# Force flush queue
sudo postfix flush
# ดูรายละเอียดของ message ที่ค้างใน queue
sudo postcat -q MESSAGE_ID
คำถามที่พบบ่อย
Postfix relay กับ full mail server ต่างกันอย่างไร?
Full mail server (Postfix + Dovecot + SpamAssassin) รับและส่ง email ของตัวเอง ซับซ้อนมากและต้องดูแล deliverability เอง SMTP relay แค่รับ email จาก application แล้วส่งต่อผ่าน service อื่น (Gmail, SendGrid) ง่ายกว่าและ deliverability ดีกว่าสำหรับ transactional email
ควรใช้ SendGrid หรือ Gmail สำหรับ production?
SendGrid/Mailgun ดีกว่าสำหรับ production เพราะมี dashboard ดู delivery rate, bounce, spam complaint ได้ มี dedicated IP ให้ใช้ และ limit สูงกว่ามาก Gmail limit 500 email/วัน (free) เหมาะแค่ testing หรือ internal alerts
SPF record ตั้งอย่างไรให้ email ไม่ตก spam?
SPF ระบุว่า server ไหนมีสิทธิ์ส่ง email ในนาม domain ถ้าใช้ Gmail relay: v=spf1 include:_spf.google.com ~all ถ้าใช้ SendGrid: v=spf1 include:sendgrid.net ~all ตั้งเป็น TXT record ที่ DNS provider