---
agent:
  instructions: "Use this search API to find relevant content across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://sendcorex.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://sendcorex.com/llms.txt"
---

Source: https://sendcorex.com/vi/blog/send-email-php-laravel

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ứng | Cách sửa |
| --- | --- |
| `SMTP connect() failed` | Kiể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ửi | Xác nhận `php artisan queue:work` đang chạy nếu `QUEUE_CONNECTION` không phải `sync`. |

### Trên trang này

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.

[Bắt đầu miễn phí — 3.000 email/tháng](https://app.sendcorex.com/authentication/register?appid=smtp)

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

## Đọc tiếp

[Kiến thức cơ bản ### SMTP là gì? Hướng dẫn dễ hiểu SMTP là giao thức chuyển gần như mọi email gửi trên internet. Đây là những gì nó thực sự làm, từng bước — không dùng thuật ngữ RFC. Đọc hướng dẫn](https://sendcorex.com/vi/blog/what-is-smtp) [Bài thực hành ### Cấu hình SMTP cho WordPress (plugin và code) Sửa email WordPress không bao giờ tới bằng cách định tuyến wp_mail qua SMTP server có xác thực — bằng plugin hoặc code. Đọc hướng dẫn](https://sendcorex.com/vi/blog/wordpress-smtp-setup) [Khả năng vào hộp thư ### Giải thích SPF, DKIM và DMARC Ba bản ghi DNS đứng giữa email của bạn và hộp thư. Đây là những gì mỗi bản ghi thực sự chứng minh, và điều gì xảy ra khi thiếu một cái. Đọc hướng dẫn](https://sendcorex.com/vi/blog/spf-dkim-dmarc-explained)

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

[Bắt đầu miễn phí — 3.000 email/tháng](https://app.sendcorex.com/authentication/register?appid=smtp) [Xem bảng giá](https://sendcorex.com/vi/pricing)
