---
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/th/blog/send-email-ruby-rails

บทเรียน

# ส่งอีเมลด้วย Ruby on Rails ผ่าน SMTP

Action Mailer ตั้งค่าครั้งเดียวใน initializer แล้วส่งได้จากทุกที่ในแอป

โดยทีม Sendcorex·อัปเดต 21 ก.ย. 2026·อ่าน 7 นาที

## ตั้งค่า Action Mailer

ใน `config/environments/production.rb` (หรือ 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' }
```

## สร้างและส่ง 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
```

ใช้ `deliver_later` กับ queue adapter (Sidekiq, Solid Queue) แทน `deliver_now` เพื่อไม่ให้การเชื่อมต่อ SMTP ที่ช้าบล็อกคำขอเว็บ

## ข้อผิดพลาดที่พบบ่อย

| อาการ | วิธีแก้ |
| --- | --- |
| `Net::SMTPAuthenticationError` | ตรวจว่าตัวแปรสภาพแวดล้อมถูกโหลดและใช้ทั้ง `user_name` และ `password` |
| อีเมลไม่ออกจากคิว | ตรวจว่า queue adapter (เช่น Sidekiq) ทำงานจริงในสภาพแวดล้อมนั้น |

### ในหน้านี้

ส่งอีเมลแรกในไม่กี่นาที

สร้างบัญชีฟรี ยืนยันโดเมน แล้ววางโค้ดด้านบน

[เริ่มฟรี — 3,000 อีเมล/เดือน](https://app.sendcorex.com/authentication/register?appid=smtp)

ไม่ต้องใช้บัตรเครดิต

## อ่านต่อ

[พื้นฐาน ### SMTP คืออะไร? คู่มือภาษาง่ายๆ SMTP คือโปรโตคอลที่ส่งอีเมลเกือบทุกฉบับบนอินเทอร์เน็ต นี่คือสิ่งที่มันทำจริงๆ ทีละขั้นตอน โดยไม่ใช้ศัพท์เทคนิคของ RFC อ่านคู่มือ](https://sendcorex.com/th/blog/what-is-smtp) [บทเรียน ### ส่งอีเมลด้วย Go (net/smtp) ผ่าน SMTP ใช้ net/smtp จากไลบรารีมาตรฐาน หรือเรียก REST API ด้วย net/http พร้อม header MIME อีเมล HTML และไฟล์แนบ อ่านคู่มือ](https://sendcorex.com/th/blog/send-email-go) [การส่งถึง ### อธิบาย SPF, DKIM และ DMARC ระเบียน DNS สามตัวคั่นระหว่างอีเมลของคุณกับกล่องจดหมาย นี่คือสิ่งที่แต่ละตัวพิสูจน์จริงๆ และสิ่งที่เกิดขึ้นเมื่อขาดตัวใดตัวหนึ่ง อ่านคู่มือ](https://sendcorex.com/th/blog/spf-dkim-dmarc-explained)

ส่งอีเมลแรกในไม่กี่นาที

สร้างบัญชีฟรี ยืนยันโดเมน แล้ววางโค้ดด้านบน

ไม่ต้องใช้บัตรเครดิต

[เริ่มฟรี — 3,000 อีเมล/เดือน](https://app.sendcorex.com/authentication/register?appid=smtp) [ดูราคา](https://sendcorex.com/th/pricing)
