Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Gotanda.rb#47 Mailgun3分クッキング

Gotanda.rb#47 Mailgun3分クッキング

Gotanda.rb#47でLT発表した『Mailgun3分クッキング』です。

963393f2d643a54c41db3812c679f361?s=128

Hiroki Tanaka

October 27, 2020
Tweet

Transcript

  1. Gotanda.rb#47 Mailgun3෼ΫοΩϯά 2020/10/27 גࣜձࣾτϨλ @hiroki_tanaka

  2. ࣗݾ঺հ ɾాத ޺جɿ@hiroki_tanaka ɾSIerɿϑϡʔνϟʔΞʔΩςΫτ(4೥ؒ) →WebܥϕϯνϟʔɿגࣜձࣾτϨλ(1೥ؒ) ɾRubyɿ2೥൒ɾJavaɿ2೥൒ɾSQLɿ5೥ →࠷ۙ͸ΠϯϑϥΤϯδχΞݟश͍தɻ ɾझຯɿεϓϥτΡʔϯɾમ౬८Γ

  3. αʔϏε঺հ - τϨλ͸ҿ৯ళ޲͚ͷ༧໿୆ாɾސ٬୆ா͕ϝΠϯ ϓϩμΫτɻ(࠷ۙͩͱGoToEatʹ΋ࢀՃ͍ͯ͠·͢ɻ)

  4. αʔϏε঺հ - ίϩφՒʹҿ৯ళ͕͜Ε·ͰஷΊ͖ͯͨ୆ாͷސ٬ʹ DMΛҰׅૹ৴ग़དྷΔʮτϨλϝοηʔδʯͱ͍͏αʔ Ϗε͕ੜ·Εͨɻ

  5. ࠓ೔࿩͢͜ͱɾ࿩͞ͳ͍͜ͱ ʲ࿩͢͜ͱʳ - RailsͱMailgunΛ༻͍ͨૹ৴ϝʔϧͷड৴֬ೝɾ։෧֬ೝɾ URLͷΫϦοΫ֬ೝͷऔಘʹ͍ͭͯ (MailgunΛ஌͍ͬͯΔਓʹͱͬͯ͸جຊతͳ࿩Λ͢ΔͷͰɺऍᷟʹઆ๏͔΋Ͱ͢…) ʲ࿩͞ͳ͍͜ͱʳ - ϝʔϧؔ࿈ͷgem →ruby-gmail΍letter_opener_webͳͲ

    - ActionMailerͷ࢖͍ํ
  6. ϝʔϧͰ͋Γ͕ͪͳཁ๬͕ τϨλϝοηʔδͰ΋͋Γ·ͨ͠ɻ

  7. γεςϜ͔Βૹ৴ͨ͠ϝʔϧ͕Ͳͷఔ౓ड৴ ͞Ε͔ͨ஌Γ͍ͨɻ

  8. ૹ৴ͨ͠ϝʔϧͷ಺ɺԿ%͕։෧͞ Εͯຊจ಺ͷURL͕Ͳͷఔ౓ΫϦοΫ͞Ε͔ͨ ஌Γ͍ͨɻ

  9. Ͳ͏΍࣮ͬͯݱ͠Α͏ʁ

  10. ϝʔϧૹ৴ʹඞཁͳESPଆͰ Կͱ͔ग़དྷͳ͍͔ͳ…ʁ

  11. ESPͱ͸ - ESPͱ͸Email Service ProdiverͷུͰిࢠϝʔϧ഑৴ γεςϜ΍αʔϏεΛఏڙ͢Δࣄۀऀͷ͜ͱɻ - ಛʹϚʔέςΟϯάϝʔϧ΍τϥϯβΫγϣϯϝʔϧ(༧໿֬ ೝϝʔϧͳͲ)ͱ͍ͬͨࣄۀʹ௚݁͢Δϝʔϧͷ഑৴͕ओɻ -

    ༗໊ॴͩͱSendGrid΍MailChimpɾAmazon SESɻ - ޿ٛʹ͸Gmail΍yahooϝʔϧ΋֘౰͢Δɻ
  12. ࠓճ࢖༻͢ΔESPɿMailgun - ࠓճ͸Mailgunͱ͍͏ESPΛ࢖༻ɻ - Mailgun Technologies Inc͕ఏڙ͍ͯ͠ΔαʔυύʔςΟͷ Ϋϥ΢υϝʔϧαʔϏεɻ - ຖ݄࠷େ1ສ௨·ͰແྉͰϝʔϧΛૹ৴Ͱ͖ΔͷͰɺखܰʹ

    ελʔτग़དྷΔɻ (Googleύʔτφʔϖʔδ͔ΒͷਃࠐΈͩͱ3ສ௨·Ͱແྉɻ)
  13. MailgunͷϝϦοτ ʲϝϦοτʳ - ॳظઃఆ͕؆୯ɻ →ΞΧ΢ϯτ࡞੒~Mailgunઃఆ~DNSઃఆ~RailsଆʹͦͷMailgunઃఆΛద༻͢Δ͚ͩ Ͱɺ͙͢ʹϝʔϧૹ৴ՄೳʹͳΔɻ - APIΛެ։͍ͯ͠ΔͷͰɺϓϩάϥϜ͔ΒͷΧελϚΠζ͕΍Γ΍͢ ͍ɻ -

    ϩάϏϡʔϫʔ͕͋Γ͔ͦ͜Βϝʔϧͷ࠶ૹ͕Ͱ͖ɺࣗલͷϩά؅ཧ ෆཁɻ - ؅ཧऀҎ֎ͷݖݶͰϢʔβൃߦ͕ग़དྷΔɻ →ඇΤϯδχΞͷνʔϜʹτϥϒϧγϡʔτ΍ϝʔϧ෼ੳΛґཔͰ͖Δɻ
  14. MailgunͷσϝϦοτ ʲσϝϦοτʳ - RFC४ڌ͍ͯ͠ͳ͍೔ຊͷΩϟϦΞಠࣗڐ༰ͷϝʔϧΞυϨε ΁ͷ഑৴Λ໰౴ແ༻Ͱ஄͘ɻ →ʮa..a@example.comʯ΍ʮtest.@example.comʯͳͲ - ͦͦ͜͜ߴՁͰAmazon SESͷ4ഒఔ౓ɻ

  15. MailgunͷWebhook - Mailgunʹ͸ಛఆͷΠϕϯτൃੜ࣌ʹΞϓϦʹΠϕϯτ௨஌Λ POST͢ΔWebhook͕͋ΓɺͦΕΛར༻͍ͯ͘͠ɻ - Webhook͸༷ʑ༻ҙ͞Ε͍ͯΔ͕ɺࠓճ͸delivered(഑৴)ɾ opened(։෧)ɾclicked(ΫϦοΫ)Λར༻͢Δɻ - ઃఆ୯Ґ͸υϝΠϯ୯Ґ͔ϝʔϧ୯ҐͰઃఆͰ͖Δɻ ※ࢀߟ

    - WebhookͷҰཡɿhttps://documentation.mailgun.com/en/latest/api- webhooks.html#webhooks - Webhookͷ࢖͍ํΨΠυɿhttps://www.mailgun.com/guides/your-guide-to-webhooks/
  16. Webhook͔ΒPOST͞ΕΔύϥϝʔλྫ - `event-data`ͱ͍͏਌ͷϋογϡΩʔͷόϦϡʔͱ͠ ͯҎԼ͕֨ೲ͞ΕΔɻ

  17. ࣮૷खॱ 1. ϝʔϧػೳΛHTMLϝʔϧܗࣜͰ࣮૷͢Δɻ →Mailgunͷ։෧֬ೝ͸ϝʔϧ಺ʹࣗಈͰຒΊࠐ·Εͨը૾͕։͔Ε͔ͨʹΑͬͯ൑ఆ͞ΕΔͨ ΊɺϓϨʔϯςΩετϝʔϧͰ͸։෧֬ೝग़དྷͳ͍ɻ 2. ֤ϝʔϧͷϔομʔʹMailgunͷWebhookΛൃՐ͢ΔͨΊͷΩʔόϦϡʔΛ࢓ࠐ Ήɻ →ࠓճ͸υϝΠϯ୯ҐͰ͸ͳ͘ɺϝʔϧ୯ҐͰߦ͏ɻ 3.

    MailgunଆʹWebhook࣮ߦ࣌ͷPOSTઌͷURLΛઃఆ͢Δɻ 4. POST͞ΕͨσʔλΛRailsଆͰड͚औͬͯɺϏδωεϩδοΫʹ߹ΘͤͯΑ͠ͳʹ ࣮૷͢Δɻ
  18. ࣮૷खॱ 1. ϝʔϧػೳΛHTMLϝʔϧܗࣜͰ࣮૷͢Δɻ →Mailgunͷ։෧֬ೝ͸ϝʔϧ಺ʹࣗಈͰຒΊࠐ·Εͨը૾͕։͔Ε͔ͨʹΑͬͯ൑ఆ͞ΕΔͨ ΊɺϓϨʔϯςΩετϝʔϧͰ͸։෧֬ೝग़དྷͳ͍ɻ 2. ֤ϝʔϧͷϔομʔʹMailgunͷWebhookΛൃՐ͢ΔͨΊͷΩʔόϦϡʔΛ࢓ࠐ Ήɻ →ࠓճ͸υϝΠϯ୯ҐͰ͸ͳ͘ɺϝʔϧ୯ҐͰߦ͏ɻ 3.

    MailgunଆʹWebhook࣮ߦ࣌ͷPOSTઌͷURLΛઃఆ͢Δɻ 4. POST͞ΕͨσʔλΛRailsଆͰड͚औͬͯɺϏδωεϩδοΫʹ߹ΘͤͯΑ͠ͳʹ ࣮૷͢Δɻ ͜Ε͔Βઆ໌͢Δ෦෼
  19. ϝʔϧͷϔομʔઃఆ - ϝʔϧૹ৴ॲཧͷafter_actionͰMailgunͷ։෧֬ೝɾ ΫϦοΫ֬ೝͷͨΊͷϔομʔΛ෇༩͢Δɻ (ड৴֬ೝ͸ϔομʔͷઃఆ͕ෆཁɻ) - ࢠͷMailerΫϥε - ਌ͷApplicationMailerΫϥε

  20. ࣮ࡍͷϝʔϧͷϔομʔ - ૹ৴͞Εͨϝʔϧͷϔομʔʹ։෧֬ೝͱΫϦοΫ֬ ೝͷͨΊͷ`X-Mailgun-Track`͕ઃఆ͞ΕΔɻ

  21. MailgunͷWebhookઃఆ - RailsΞϓϦ΁ͷWebhookͷPOSTઌΛMailgunͷ Webhookʹઃఆ͢Δɻ - Mailgunଆͷઃఆ - RailsଆͷϧʔςΟϯάઃఆ (஫ɿPath͸μϛʔͰ͢)

  22. RailsͰWebhookͷPOSTΛड͚ͯͷॲཧ - Mailgun͔ΒͷPOSTΛड͚࣮ͯߦ͢ΔॲཧΛRailsʹ࣮૷͢ Δɻ - `event-data`഑Լʹ͋Δ`event`ʹԠͯ͡ॲཧΛ෼ذͤ͞Δɻ

  23. ૹ৴݁Ռ֬ೝը໘Ͱͷ֬ೝ - ࠷ޙʹRailsΞϓϦଆͷૹ৴݁Ռ֬ೝը໘͔ΒνΣοΫ͢Δɻ - ԼهͷྫͰ͸ड৴֬ೝͰ͢ɻ

  24. ্खʹग़དྷ·ͨ͠ʂ

  25. ஫ҙ఺ 1. Mailgunͷ։෧֬ೝ͸ϝʔϧ಺ʹࣗಈͰຒΊࠐ·Εͨ ը૾͕։͔Ε͔ͨʹΑͬͯ൑ఆ͞ΕΔͨΊɺϓϨʔϯς Ωετϝʔϧ͸ඇରԠɻ 2. େྔͷϝʔϧΛMailgunܦ༝Ͱૹ৴͢ΔͱMailgun͸ ΤϥʔʹͳΒͣʹਖ਼ৗʹૹ৴ॲཧΛߦͬͯ͘ΕΔɻ →ಉ࣌ʹେྔͷWebhook͕ΞϓϦέʔγϣϯʹPOST͞ ΕΔͨΊɺηϧϑDoS߈ܸঢ়ଶʹͳͬͯ͠·͏ɻ

    (࣮ࡍʹͦΕͰΞϓϦέʔγϣϯ͕1౓མͪ·ͨ͠…)
  26. ͓ΘΓʹ - MailgunͷWebhookػೳΛར༻͢Δ͜ͱͰૹ৴ϝʔ ϧͷड৴֬ೝɾ։෧֬ೝɾΫϦοΫ֬ೝΛ؆୯ʹग़དྷ ·ͨ͠ɻ - ෼ੳ͕खܰʹͰ͖Δ͜ͱ͸ࣄۀͷ੒௕଎౓ʹ΋௚݁͢ΔͷͰ େࣄɻ - ͱ͸͍͑ɺMailgunΛ࢖༻͠ͳ͍ଞͷํ๏΋͋Δͱࢥ

    ͍·͢ͷͰɺ৭ʑڭ͑ͯ௖͚Δͱخ͍͠Ͱ͢ʂ
  27. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ