Sendcorex
Bài thực hành

Gửi email bằng PHP (Laravel) qua SMTP

PHPMailer cho script đơn giản, hoặc Mailable của Laravel — cả hai qua cùng relay.

Bởi đội ngũ Sendcorex·Cập nhật 21 thg 9, 2026·8 phút đọc

Cách 1: PHPMailer

composer require phpmailer/phpmailer
use PHPMailer\PHPMailer\PHPMailer;

$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.sendcorex.com';
$mail->SMTPAuth = true;
$mail->Username = getenv('SENDCOREX_API_KEY');
$mail->Password = getenv('SENDCOREX_API_KEY');
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

$mail->setFrom('hello@yourdomain.com', 'Your App');
$mail->addAddress('customer@example.com');
$mail->isHTML(true);
$mail->Subject = 'Welcome!';
$mail->Body = '<h1>You are in.</h1>';

$mail->send();

Cách 2: Mailable của Laravel

Đặt relay trong .env:

MAIL_MAILER=smtp
MAIL_HOST=smtp.sendcorex.com
MAIL_PORT=587
MAIL_USERNAME=your_api_key_here
MAIL_PASSWORD=your_api_key_here
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=hello@yourdomain.com
MAIL_FROM_NAME="Your App"

Sau đó tạo và gửi một Mailable:

php artisan make:mail WelcomeMail

Mail::to('customer@example.com')->send(new WelcomeMail());

Lỗi thường gặp

Triệu chứngCách sửa
SMTP connect() failedKiểm tra host, port và chắc chắn hosting không chặn port 587 gửi ra.
Thư trong hàng đợi không bao giờ được gửiXác nhận php artisan queue:work đang chạy nếu QUEUE_CONNECTION không phải sync.

Gửi email đầu tiên trong vài phút

Tạo tài khoản miễn phí, xác minh tên miền và dán code ở trên.

Không cần thẻ tín dụng