---
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-python-django

Bài thực hành

# Gửi email bằng Python (Django) qua SMTP

smtplib cho script đơn giản, hoặc backend email của Django cho ứng dụng — cả hai qua cùng SMTP relay có xác thực.

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

## Cách 1: Python thuần với smtplib

```
import smtplib, os
from email.mime.text import MIMEText

api_key = os.environ["SENDCOREX_API_KEY"]

msg = MIMEText("You are in.", "html")
msg["Subject"] = "Welcome!"
msg["From"] = "Your App <hello@yourdomain.com>"
msg["To"] = "customer@example.com"

with smtplib.SMTP("smtp.sendcorex.com", 587) as server:
    server.starttls()
    server.login(api_key, api_key)
    server.send_message(msg)
```

## Cách 2: backend email của Django

Trỏ backend có sẵn của Django tới relay trong `settings.py`:

```
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.sendcorex.com"
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = os.environ["SENDCOREX_API_KEY"]
EMAIL_HOST_PASSWORD = os.environ["SENDCOREX_API_KEY"]
DEFAULT_FROM_EMAIL = "Your App <hello@yourdomain.com>"
```

Sau đó gửi từ bất kỳ đâu trong ứng dụng bằng `send_mail()` hoặc, với HTML, `EmailMultiAlternatives`:

```
from django.core.mail import EmailMultiAlternatives

email = EmailMultiAlternatives("Welcome!", "You are in.", to=["customer@example.com"])
email.attach_alternative("<h1>You are in.</h1>", "text/html")
email.send()
```

## Lỗi thường gặp

| Triệu chứng | Cách sửa |
| --- | --- |
| `SMTPAuthenticationError` | Xác nhận biến môi trường API key đã được đặt và dùng cho cả user lẫn password. |
| `SMTPServerDisconnected` | Gọi `starttls()` trước `login()`, không phải sau. |

### 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 ### Gửi email bằng PHP (Laravel) qua SMTP PHPMailer cho script PHP đơn giản, hoặc hệ thống mail của Laravel với lớp Mailable — kèm email HTML, tệp đính kèm và xử lý lỗi. Đọc hướng dẫn](https://sendcorex.com/vi/blog/send-email-php-laravel) [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)
