Mặc định WordPress dùng mail() của PHP — hay vào spam, server mail thường bị block. Giải pháp là dùng SMTP riêng (Gmail / SendGrid / Resend / AWS SES). Đa số người cài plugin "WP Mail SMTP" — nhưng plugin này nặng và có quảng cáo. Code dưới đây làm cùng việc với 15 dòng.
Code paste vào functions.php (child theme)
add_action('phpmailer_init', function ($phpmailer) {
if (!is_object($phpmailer)) $phpmailer = (object) $phpmailer;
$phpmailer->Mailer = 'smtp';
$phpmailer->Host = 'smtp.gmail.com';
$phpmailer->SMTPAuth = 1;
$phpmailer->Port = 587;
$phpmailer->Username = 'your@gmail.com';
$phpmailer->Password = 'app-password-from-google';
$phpmailer->SMTPSecure = 'tls';
$phpmailer->From = 'your@gmail.com';
$phpmailer->FromName = 'Tên Shop';
});
SMTP gợi ý theo nhu cầu
- Gmail — free, 500 mail/ngày, cần "App Password" (Google Account → Security → 2FA → App passwords)
- SendGrid — 100 mail/ngày free, không bị giới hạn provider
- Resend — 100 mail/ngày free, deliverability tốt nhất, recommend cho transactional
- AWS SES — siêu rẻ ($0.10 / 1000 mail), setup hơi phức tạp
Common pitfall
- Gmail "less secure apps" đã bị bỏ — phải dùng App Password
- From email khác Username → Gmail block. Phải set From = Username
- SPF / DKIM chưa setup → mail vẫn vào spam dù đã SMTP. Setup DNS theo hướng dẫn provider
