---
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-ruby-rails

Bài thực hành

# Gửi email bằng Ruby on Rails qua SMTP

Action Mailer, cấu hình một lần trong initializer, gửi từ bất kỳ đâu trong ứng dụng.

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

## Cấu hình Action Mailer

Trong `config/environments/production.rb` (hoặc một initializer):

```
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  address: 'smtp.sendcorex.com',
  port: 587,
  user_name: ENV['SENDCOREX_API_KEY'],
  password: ENV['SENDCOREX_API_KEY'],
  authentication: 'plain',
  enable_starttls_auto: true,
}
config.action_mailer.default_url_options = { host: 'yourdomain.com' }
```

## Tạo và gửi mailer

```
rails generate mailer Welcome
```

```
class WelcomeMailer < ApplicationMailer
  default from: 'Your App <hello@yourdomain.com>'

  def welcome_email(user)
    @user = user
    mail(to: @user.email, subject: 'Welcome!')
  end
end

# from anywhere in your app
WelcomeMailer.welcome_email(user).deliver_later
```

Dùng `deliver_later` với queue adapter (Sidekiq, Solid Queue) thay cho `deliver_now` để kết nối SMTP chậm không bao giờ chặn request web.

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

| Triệu chứng | Cách sửa |
| --- | --- |
| `Net::SMTPAuthenticationError` | Xác nhận biến môi trường đã được nạp và dùng cho cả `user_name` lẫn `password`. |
| Thư không bao giờ rời hàng đợi | Kiểm tra queue adapter (ví dụ Sidekiq) có thực sự chạy trong môi trường đó. |

### 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 Go (net/smtp) qua SMTP Dùng net/smtp trong thư viện chuẩn, hoặc gọi REST API bằng net/http — kèm header MIME, email HTML và tệp đính kèm. Đọc hướng dẫn](https://sendcorex.com/vi/blog/send-email-go) [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)
