ตั้ง SMTP Relay ด้วย Postfix บน VPS
การส่ง email จาก VPS โดยตรงมักตกใน spam เพราะ IP ของ cloud server ถูก blacklist โดย default SMTP Relay คือการใช้ Postfix เป็น Mail Transfer Agent (MTA) แล้วส่งต่อผ่าน trusted SMTP provider เช่น Gmail, AWS SES หรือ SendGrid วิธีนี้ทำให้ email ไปถึง inbox ได้สูงกว่าและไม่ต้องดูแล email deliverability เอง
ติดตั้ง Postfix
sudo apt update
sudo apt install postfix mailutils -y
# ตอน setup wizard ถามให้เลือก "Satellite system"
# ใส่ system mail name = yourvps.yourdomain.com
ตั้ง SMTP Relay ผ่าน Gmail (App Password)
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
smtp_tls_security_level = encrypt
# สร้างไฟล์ credential
sudo nano /etc/postfix/sasl_passwd
[smtp.gmail.com]:587 [email protected]:your_16char_app_password
# Hash และ secure ไฟล์
sudo postmap /etc/postfix/sasl_passwd
sudo chmod 600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
sudo systemctl restart postfix
ตั้ง SMTP Relay ผ่าน AWS SES
sudo nano /etc/postfix/main.cf
relayhost = [email-smtp.ap-southeast-1.amazonaws.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_security_options = noanonymous
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_use_tls = yes
smtp_tls_security_level = encrypt
smtp_tls_note_starttls_offer = yes
sudo nano /etc/postfix/sasl_passwd
[email-smtp.ap-southeast-1.amazonaws.com]:587 AKIAIOSFODNN7EXAMPLE:wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
sudo postmap /etc/postfix/sasl_passwd
sudo chmod 600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
sudo systemctl restart postfix
ส่ง Email ทดสอบ
# ส่ง test email
echo "Test from VPS" | mail -s "SMTP Relay Test" [email protected]
# ดู mail queue
mailq
# ดู delivery log
sudo tail -f /var/log/mail.log
ตั้ง Canonical Sender (ไม่ให้ส่งในชื่อ root)
sudo nano /etc/postfix/main.cf
# เพิ่ม:
sender_canonical_maps = hash:/etc/postfix/sender_canonical
sudo nano /etc/postfix/sender_canonical
root [email protected]
@yourvps yourdomain.com [email protected]
sudo postmap /etc/postfix/sender_canonical
sudo systemctl reload postfix
ตรวจสอบว่า SMTP Relay ทำงานถูกต้อง
# ทดสอบ SMTP connection ไปยัง relay
telnet smtp.gmail.com 587
# ตรวจ Postfix queue
mailq
# Force delivery ที่ค้างอยู่
sudo postqueue -f
# ดู log แบบ real-time
sudo tail -f /var/log/mail.log
# ตรวจ postfix config ไม่มี error
sudo postfix check
status=sent (250 2.0.0 OK) ใน mail.log แสดงว่า SMTP relay ทำงานปกติและ email ถูกรับไปแล้ว
แก้ปัญหาที่พบบ่อย
Error: "SASL authentication failed"
Gmail App Password ผิด หรือ 2FA ยังไม่ได้เปิด:
ควรเช็คด้วยว่า IP ของ VPS ไม่ติด Blacklist ก่อนเริ่มส่งจริง ผ่าน เครื่องมือเช็ค Blacklist ของ dnsxray.com เพราะ IP ของ VPS มือสองจำนวนมากเคยถูกใช้ส่งสแปมมาก่อน ถ้าติดลิสต์อยู่ให้รีบยื่น Delist หรือขอเปลี่ยน IP ตั้งแต่ยังไม่ผูกระบบ
# Gmail ต้องการ App Password (ไม่ใช่ password ปกติ)
# ไปที่: Google Account > Security > 2-Step Verification > App passwords
# สร้าง App Password สำหรับ "Mail" > "Linux computer"
sudo nano /etc/postfix/sasl_passwd
# อัปเดต App Password ใหม่
sudo postmap /etc/postfix/sasl_passwd
sudo systemctl restart postfix
Email ส่งได้แต่ตกใน spam ปลายทาง
ต้องตั้ง SPF record ในโดเมน และตรวจ blacklist:
# ตั้ง SPF ใน DNS TXT record:
# v=spf1 include:_spf.google.com ~all
# (ถ้าใช้ Gmail relay)
# ตรวจ header ของ email ที่ได้รับ
# ดู "Received-SPF" และ "Authentication-Results"
Error: "Connection timed out" ไปยัง relay
ISP หรือ VPS provider บล็อก port 587 ลองใช้ port 465:
sudo nano /etc/postfix/main.cf
relayhost = [smtp.gmail.com]:465
smtp_tls_wrappermode = yes
sudo systemctl restart postfix
# ทดสอบ connection
telnet smtp.gmail.com 465
Postfix queue ค้างไม่ flush
ตรวจว่า relay ยอมรับ connection ได้:
sudo mailq
sudo postqueue -f
sudo tail -50 /var/log/mail.log
# ถ้า error ซ้ำๆ ให้ purge queue และแก้ config ก่อน
sudo postsuper -d ALL
คำถามที่พบบ่อย
SMTP Relay กับ email server เต็มรูปแบบต่างกันอย่างไร?
SMTP Relay เป็น MTA ที่รับ email จาก local application แล้วส่งต่อผ่าน trusted provider ไม่ได้รับ email จากภายนอก Email server เต็มรูปแบบรับ-ส่งเองและมี mailbox สำหรับ user Relay เหมาะสำหรับ application notification email ที่ต้องการส่งเท่านั้น
ควรใช้ Gmail Relay หรือ AWS SES?
Gmail Relay ส่งได้สูงสุด 500 email/วัน เหมาะสำหรับ low-volume application SES เหมาะสำหรับ high-volume เช่น e-commerce notification ราคา SES อยู่ที่ $0.10 ต่อ 1,000 email ถูกมากและ deliverability สูง
ถ้าใช้ VPS ไทยควรเลือก region SES ไหน?
ใช้ ap-southeast-1 (สิงคโปร์) เป็น region ที่ใกล้ที่สุดและ latency ต่ำ ต้อง verify โดเมนและ email address ใน SES console ก่อนใช้งาน และในช่วงแรกต้องขอ production access เพื่อออกจาก sandbox mode
สำหรับผู้ที่กำลังมองหา VPS ไทยที่รองรับ email และ DirectAdmin asiagb.com มีบริการ VPS ในประเทศไทยที่ตั้งค่า email ได้เต็มรูปแบบ พร้อม SLA 99.9% และ support ภาษาไทย