Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Ruby

ประมวลผล Incoming Email ใน Rack Application ใดๆ ได้ราบรื่น

So your client just called...

เขาอยากให้ app จัดการ incoming email เหมือน Basecamp

ไม่เป็นไร คิดว่ามี services เยอะส่ง email โดย POST ไป app ควรจะง่าย ถูกไหม?

Nope.

แต่ละ service ใช้ proprietary format บางอันส่ง parsed email บางอันส่ง raw บางอันส่ง authentication signature บางอันไม่ส่ง

ไม่ดีไหมถ้าคุณข้ามส่วนนี้ไป และทำงานกับ standard (well-known) ruby objects?

ไม่ดีไหมถ้าคุณเปลี่ยน vendors...โดยไม่ต้อง rewrite code?

ถ้าคุณใช้ Incoming! คุณทำได้

TL;DR

Incoming! รับ Rack::Request ให้คุณ Mail::Message

Supported Services

Incoming รองรับ services ยอดเยี่ยมเหล่านี้:

  1. SendGrid

  2. Mailgun

  3. Postmark

  4. CloudMailin

  5. Actual mail servers ที่คุณรันเอง (woah)

ดีที่สุดคือ extend ง่าย ดังนั้น compatible กับ services ที่ยังไม่มีอยู่! (ไม่จริง แต่แทบจะ)

ทำไมต้องการ inbound email?

ให้ people option ทำบางอย่างโดยส่ง email ทำให้พวกเขา likely ทำมากขึ้น

เราสร้าง Incoming! สำหรับ honeybadger.io ต้องการ streamline discussions เกี่ยวกับ errors ตัวอย่าง คุณ comment บน error ใดๆ ได้โดยแค่ reply notification ส่วนใหญ่ comments ของเรามาแบบนี้ ถือว่า big win

ณ เวลานี้ gem ใช้ internally โดย Honeybadger team ประมาณ 6 เดือนแล้ว

Show me the code!

ตัวอย่างง่ายใช้ CloudMailin

`# ก่อนอื่น define email receiver class
class EmailReceiver < Incoming::Strategies::CloudMailin
  def receive(mail)
    puts %(Got message from #{mail.to.first} with subject "#{mail.subject}")
  end
end

# จากนั้น feed 它 Rack::Request object. และเสร็จแล้ว
req = Rack::Request.new(env)
result = EmailReceiver.receive(req)
# => Got message from whoever@wherever.com with subject "hello world" `

รายละเอียดเพิ่มเติมเช็ค Readme

แต่ใช้ใน Rails ยังไง?

ใช้ Incoming! ใน rails ง่ายเท่าเดิม มาสร้าง sample app

REQUIREMENT: ผมเกลียด texting มันแค่ pet peeve แต่ต้องส่ง love notes ให้ fiancée ขณะเธอใน class

คำตอบชัดเจนคือสร้าง email-to-sms bridge!

ก่อน ตั้งค่า 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 `

สอง เพิ่ม controller pipe requests เข้า 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 `

โอ้ ต้องการ route:

`# config/routes.rb
Rails.application.routes.draw do
  post '/emails' => 'emails#create'
end `

แค่นี้

Incoming! เปรียบเทียบ gems อื่นยังไง?

มี gems อื่นใน space นี้ สำคัญคือ Griddler ของ Thoughtbot

แต่มีจุดที่ Incoming! โดดเด่น มัน:

  1. รองรับ multiple mail services

  2. ทำงานกับ any rack application ไม่แค่ rails

  3. มอบ Mail::Message มาตรฐาน

  4. ไม่ทำ assumptions เกี่ยว use case ของคุณ

มี meme pic ให้ไหม?

มีครับ

ประมวลผล Incoming Email ใน Rack Application ใดๆ ได้ราบรื่น