หากคุณเคยดู Request for Comments (RFCs) รอบๆ การส่งและรับอีเมล คุณจะเห็น technical complications ที่เกี่ยวข้องเมื่อกด send ใน email inbox
ขอบคุณหลาย existing tools ที่ provide Simple Mail Transfer Protocol (SMTP) service ให้เรา developers — จาก Postfix server ที่คุณ manage ไป fully scalable sending service เช่น SendGrid, Amazon SES หรือ Postmark อย่างไรก็ตาม การ move between providers สำหรับ deliverability หรือ pricing reasons หมายถึง rewriting หรือ refactoring apps ของเราเพื่อ meet peculiarities ของแต่ละ service
Rails ช่วยเราที่นี่โดย provide Action Mailbox ในโพสต์นี้ เราจะดำดิ่งว่าคุณสามารถใช้ Action Mailbox เพื่อ integrate และ troubleshoot inbound emails อย่างไร
แตก่อน มา quick define ว่า Action Mailbox คืออะไร
Action Mailbox สำหรับ Rails คืออะไร?
Action Mailbox ใช้ conceptual compression สำหรับ receiving email ใน Ruby on Rails Conceptual compression หมายถึงมัน encapsulate all the small differences ระหว่างทุก SMTP service และเขียน inbound processing code ของคุณเพียงครั้งเดียว คุณแม้แต่สามารถเขียน provider สำหรับ service ใหม่ได้
Key concept ของ ActionMailbox คือ routing based on email recipients โดยการ setup inbound email provider mail ที่ไปยัง domain จะ routed เข้าแอปของคุณ คุณสามารถดู recipient address เพื่อ determine ว่าแต่ละ mail message ควร processed อย่างไร
หากคุณผ่าน rails conductor action เพื่อส่ง test email เหมือนที่ผมทำที่นี่:

Inbound email ของคุณจะมี recipients จาก fields To, CC, BCC, และ X-Original-To
Each address ถูก test เพื่อ determine ไปที่ไหนแต่ mail message ถูก routed เพียงครั้งเดียว
One key aspect ของ development คือ actually testing email จาก system Rails มี set ของ development pages ภายใต้ routes /rails/conductor/ ที่ allow คุณ input emails locally เข้า development setup
คุณสามารถ enter email manually เหมือนที่ผมทำในตัวอย่างข้างต้น หรือ upload email 完成 עם headers ทั้งหมด
A great way เพื่อได้ complete email (พร้อม headers, message body และ attachments) คือใช้ email client เช่น Thunderbird Save individual email ใน .eml เปิดไฟล์ด้วย text editor และ copy complete contents เข้า conductor page
ตอนนี้คุณสามารถ test more complicated email processing
Posting และ Commenting Demo สำหรับ Rails App
มา put together small demo เพื่อแสดงว่าทั้งหมดนี้ทำงานอย่างไร ผมเป็น admirer มากของ 37Signals และชอบ blogging กับ Hey World ของพวกเขา แตพวกเขาไม่ allow comments ดังนั้นมา create clone ที่ include comments สำหรับแต่ละ blog post
Follow along กับโค้ดในโพสต์นี้
สร้าง new app (ผมใช้ Tailwind CSS แตคุณเลือกสิ่งที่ makes sense ให้คุณ) ผมจะเพิ่ม Action Text สำหรับ Post และ Comment models
Scaffolding ให้เรา quick way เพื่อดู Posts และ Comments เพิ่ม association ใน post.rb และ post จะ display related comments:
ใน posts/_post.html.erb มาเพิ่ม comments partial:
ตอนนี้เรามี sparse post และ comment view Setup inbound email สำหรับ posting ไป blog:
นี่จะ generate ApplicationMailbox เราจะ setup route ให้ anything สำหรับ blog@ ไป Post Mailbox และสร้าง post
คุณสามารถ test นี้เร็วโดยไป https://localhost:3000/rails/conductor/action_mailbox/inbound_emails และส่ง emails บางอย่างไป service หากคุณส่งบางอย่างไป blog@whatever.com email ควร delivered ไป inbox บนแอปของเรา หากคุณ email address อื่น message จะ bounce
Receive Email ด้วย Post Mailbox
มา setup Post Mailbox เพื่อ receive email และ post ไป blog แต่ละ Mailbox มี access ไป original inbound_email และ mail objects InboundEmail เป็น wrapper รอบ mail class ที่ใช้ทั่ว rails
สำหรับ purposes ของเรา เราสนใจว่า email มาจากใคร subject และ body copy เราสามารถ extract พวกมันและสร้าง Post record ที่จะ show up บน front page ของ blog
ส่งอีก email ไป blog address และ refresh index page คุณควรเห็น post!
ตอนนี้ไปเพิ่ม comments สำหรับ post ก่อนอื่น any email commenter ต้อง refer ถึง correct post เมื่อส่ง email วิธี simple คือ encode post ID ใน inbound email address (อย่าง comment+123@whatever.com ที่ 123 ใน email address refers ไป Post element)
Generate CommentMailbox:
เพิ่ม route ใน Action Mailbox ส่ง emails ใดๆ ที่มี comment+123 ไป CommentMailbox:
ใน _post.html.erb เพิ่ม link generate email address เพื่อใครสักคนเปิด email app และส่ง email:
Incoming email จะ routed ไป CommentMailbox และ parsed เข้า comment attached ถึง correct blog post
process method สร้าง comment จาก email body และ sender email มัน reference Post queried ใน post method เมธอดนี้ได้ first recipient's email address และใช้ regular expression เพื่อ pull out post ID
หาก Post ไม่ exist หรือ token ไม่สามารถ parsed ได้ email จะ bounce ซึ่งหยุด processing
ตอนนี้ไปที่ Rails conductor form และส่ง comment ไป address สำหรับแต่ละ Post Comment จะปรากฏใต้ post บน index page!
A More Complex Example ใช้ Action Mailbox
Emails จริงๆ ซับซ้อนมาก ทưởng像คุณมี application monitoring tool setup คุณ deploy something อย่างนี้ไปแอป และเริ่มเห็น errors ใน APM dashboard
คุณอาจเห็น parsing error หรือว่า posts/comments มี weird formatting errors เยอะ
แอปของคุณรับ HTML emails และคุณ take raw body source และ post ไป website mail gem ให้เราเห็นว่า incoming email มี HTML body และเราสามารถ pull whatever parts จาก message ที่เราต้องการ
มา change CommentMailbox และ PostMailbox ให้ check multipart emails และ pull out HTML part falling back ไป text หากนั่นคือ only thing left
Each email either has no parts หรือ multiple parts Preferred order คือดูว่ามี HTML part และใช้มัน และถ้าไม่ได้ ลอง get และใช้ text part หากไม่มี parsed HTML หรือ text sections เราจะใช้ email body เหมือนเดิม
PostMailbox ตอนนี้ซับซ้อนขึ้นนิดหน่อย:
CommentMailbox ก็มี process method ที่ต่างออกไป:
ตอนนี้เราสามารถ handle emails ที่มาจากโทรศัพท์ของใครสักคนได้
การเพิ่ม Action Mailbox เข้า Rails App ของคุณ
ขอบคุณ Action Mailbox เราสามารถ consider emails เป็น another I/O avenue สำหรับ Rails app เราสามารถเขียน code independent ของ email service providers โดยใช้ conceptual compression ผมแม้แต่สามารถ move email providers ด้วย minimal work เพราะผมไม่ต้องกังวล underlying infrastructure
APM tools เช่น AppSignal ยัง provide convenient dashboard เพื่อ monitor all outgoing ActionMailers และ keep an eye on deliverability
นี่คือตัวอย่าง แสดงหนึ่งใน apps ของผมที่ส่งและรับ emails เยอะ:

นี่ให้ visibility มากขึ้นเข้าไปในสิ่งที่เกิดขึ้นภายในแอป
สรุป
ในโพสต์นี้ เราก่อน define capabilities ของ Action Mailer สำหรับ Rails เราตั้งค่า demo project ที่เรา integrated inbound emails และ parsed พวกมันเพื่อสร้าง posts สำหรับ blog
หวังว่าคุณจะพบประโยชน์ Happy coding!
P.S. หากคุณต้องการอ่านโพสต์ Ruby Magic ทันทีที่เผยแพร่ ให้สมัครสมาชิก newsletter Ruby Magic ของเรา และจะไม่พลาดโพสต์เด็ดๆ ไป!
John Beatty
ผู้เขียนแขก John เรียน Rails เพื่อสร้าง APIs สำหรับ iPhone apps ในปี 2010 และไม่ได้ look back เขาปัจจุบันสอน programming ให้นักเรียน High School สร้าง custom software สำหรับโรงเรียน และ blog
บทความทั้งหมดโดย John Beatty