Agar tum soch rahe ho ki Rails app mein email setup karna rocket science haiβ¦ toh bhool jaao! Rails mein email integration itna easy hai ki “Doodh mangoge toh kheer denge, email mangoge SMTP settings denge!” π Chalo dikhata hoon step-by-step:
1. Action Mailer: Rails ka Email Wala Dost
Rails ke paas built-in feature hai Action Mailer – jo email ko controller jaisa simple banata hai. Kya karte ho?
- Mailer generate karo
- Email content likho
- SMTP settings daalo
- Done!
No SMTP knowledge? No problem! βοΈ
2. Setup: 10 Minute Ka Tamasha
Step 1: Mailer Banana
Terminal mein bas yeh daalo:
“`bash
rails generate mailer UserMailer
Aise hi jaise controller generate karte ho!
**Step 2: Email Method Define Karo**
`app/mailers/user_mailer.rb` mein:
ruby
class UserMailer < ApplicationMailer
def welcome_email(user)
@user = user
mail(to: @user.email, subject: ‘Swagat hai hamare app mein! π’)
end
end
**Step 3: Views Banana**
HTML email ke liye: `app/views/user_mailer/welcome_email.html.erb`
erb
Namaste <%= @user.name %>!
Aapka account <%= time.now %> pe create hua hai.
Plain text ke liye `.txt.erb` version bhi banana!
---
### **3. Configuration: Gmail/Outlook ke Saat Jugaad**
`config/environments/development.rb` mein dalo:
ruby
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: ‘smtp.gmail.com’,
port: 587,
user_name: ‘apna_email@gmail.com’,
password: ‘app_password’, # Google App Password use karo
authentication: ‘plain’,
enable_starttls_auto: true
}
**Gmail Tip:** Normal password nahi, **App Password** generate karo (Google Account Security se)!
---
### **4. Email Bhejne ka Mantra**
Controller mein bas itna code:
ruby
def create
@user = User.new(user_params)
if @user.save
UserMailer.with(user: @user).welcome_email.deliver_later # Background mein send!
end
end
**Pro Tip:** `.deliver_later` use karo taaki email background job mein send ho (Sidekiq/Active Job ke saath)!
---
### **5. Development Mein Testing? MailCatcher Zindabad!**
Real SMTP setup se bachne ke liye:
1. Gemfile mein `gem 'mailcatcher'` daalo
2. `bundle install`
3. Terminal mein `mailcatcher` chalao
4. Browser kholo `http://localhost:1080`
5. Config mein daaldo:
ruby
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { address: ‘localhost’, port: 1025 }
Ab saare emails is local inbox mein dikhenge! Testing ke liye **no internet, no tension!** π
---
### **6. Production Ke Liye SendGrid (Heroku Friendly)**
1. Heroku pe SendGrid add-on laga lo
2. Ya `config/environments/production.rb` mein:
ruby
config.action_mailer.smtp_settings = {
address: ‘smtp.sendgrid.net’,
port: 587,
user_name: ‘apikey’,
password: Rails.application.credentials.sendgrid_api_key,
# β¦ same as Gmail
}
---
### **7. Kyu Itna Easy Hai?**
- **Convention Magic:** Mailers exactly controllers ki tarah work karte hai
- **Testing Helpers:** Rails automatically provide karta hai `ActionMailer::TestCase`
- **Template Inheritance:** Layouts use kar sakte ho jaise views mein karte ho:
erb
<% # app/views/layouts/mailer.html.erb %>
<%= yield %> Hamara Company Β©
---
### **Advanced Features Bhi Simple**
- **Attachments?** Bas ek line:
ruby
attachments[‘menu.pdf’] = File.read(‘path/to/file.pdf’)
“`
- Email Previews?
test/mailers/previews
folder mein previews banao - Internationalization? Subject aur content ko I18n se customize karo
Conclusion: 3 Step Process
Rails mein email setup:
- Generate Mailer β
rails g mailer UserMailer
- Configure SMTP β Gmail/SendGrid/MailCatcher
- Call in Controller β
UserMailer.welcome_email(@user).deliver_now
Total time? 15 minutes max! β±οΈ
Aur yaad rakho:
“Email setup mein na hone de tension,
Action Mailer hai tera problem solution!“
Abhi jao, apne app mein welcome email implement karoβ¦
Aur jab email aaye toh sochna: “Arey yaar, itna easy tha toh maine pehle kyun nahi kiya?” π
Final Tip:
MailCatcher se testing karo β Gmail/SendGrid for production β Aur party karo! π