Vậy client của bạn vừa gọi...
Anh ấy muốn app handle incoming email. Giống như Basecamp.
No problem, bạn nghĩ, nhiều services send email by POST đến app. Should be easy. Right?
Nope.
Mỗi service dùng proprietary format. Một số send parsed email, một số send raw. Một số send authentication signature. Một số không.
Không nice nếu bạn skip part này, và work với standard (well-known) ruby objects?
Không nice nếu bạn change vendors...without rewriting code?
Nếu dùng Incoming!, bạn có thể.
TL;DR
Incoming! nhận Rack::Request và cho bạn Mail::Message
Supported Services
Incoming hỗ trợ các services tuyệt vời:
SendGrid
Mailgun
Postmark
CloudMailin
Actual mail servers bạn tự chạy (woah)
Best of all, dễ extend. Vì vậy đã compatible với services chưa tồn tại! (không really, nhưng kind of)
Tại sao cần inbound email?
Cho người option làm gì đó bằng gửi email làm họ more likely to do it.
Chúng tôi build Incoming! cho honeybadger.io. Cần cách streamline discussions về errors. Ví dụ, comment trên bất kỳ error bằng reply notification. Hầu hết comments đến cách này, coi là big win.
Tính đến lúc viết, gem đã dùng internally bởi Honeybadger team khoảng sáu tháng.
Show me the code!
Ví dụ đơn giản dùng CloudMailin.
`# First, you define an email receiver class
class EmailReceiver < Incoming::Strategies::CloudMailin
def receive(mail)
puts %(Got message from #{mail.to.first} with subject "#{mail.subject}")
end
end
# Then you feed it a Rack::Request object. And you're done.
req = Rack::Request.new(env)
result = EmailReceiver.receive(req)
# => Got message from whoever@wherever.com with subject "hello world" `
Chi tiết hơn, check Readme.
But how do you use it in Rails?
Dùng Incoming! trong rails cũng dễ. Xây sample app.
REQUIREMENT: Tôi ghét texting. Chỉ pet peeve. Nhưng cần send love notes đến fiancée khi cô ấy ở class.
Obviously, answer là build email-to-sms bridge!
First, setup email receiver:
`# app/email_receivers/incoming.rb
class EmailToSmsReceiver < Incoming::Strategies::Postmark
def receive(mail)
send_sms([mail.subject, mail.body].join(": "))
end
private
def send_sms(message)
# Insert twilio magic here
end
end `
Second, thêm controller pipe requests vào receiver:
`# app/controllers/emails_controller.rb
class EmailsController < ActionController::Base
def create
if EmailToSmsReceiver.receive(request)
render :json => { :status => 'ok' }
else
render :json => { :status => 'rejected' }, :status => 403
end
end
end `
Oh yeah, cần route:
`# config/routes.rb
Rails.application.routes.draw do
post '/emails' => 'emails#create'
end `
That's it.
Incoming! so sánh với gems khác như thế nào?
Có gems khác trong space này. Notably Thoughtbot's Griddler.
Tuy nhiên vài chỗ Incoming! stand out. Nó:
Hỗ trợ multiple mail services
Hoạt động với any rack application, không chỉ rails
Cho bạn standard
Mail::MessageKhông make assumptions về use case của bạn
Do you have a meme pic for me?
Yes, yes we do.
