Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Gotanda.rb#47 Mailgun3分クッキング
Search
Hiroki Tanaka
October 27, 2020
Programming
1
6.8k
Gotanda.rb#47 Mailgun3分クッキング
Gotanda.rb#47でLT発表した『Mailgun3分クッキング』です。
Hiroki Tanaka
October 27, 2020
Tweet
Share
More Decks by Hiroki Tanaka
See All by Hiroki Tanaka
機能QA会のすゝめ
hiroki_tanaka
0
180
定期リリースの導入
hiroki_tanaka
0
130
noteの品質課題に立ち上げ直後のQAチームが挑んだ軌跡
hiroki_tanaka
1
1.3k
note初のBug Bashを やってみた
hiroki_tanaka
1
1.3k
コロナ禍の1年間でAWSの資格を 3つ取得した話
hiroki_tanaka
0
320
Rubocop対応のすゝめ
hiroki_tanaka
0
45
Gotanda.rb#48 ECS on Fargateでのハマりポイント
hiroki_tanaka
1
290
Gotanda.rb#46 権限管理のつらみとPundit
hiroki_tanaka
1
6.9k
Other Decks in Programming
See All in Programming
Outline View in SwiftUI
1024jp
1
310
Realtime API 入門
riofujimon
0
150
どうして僕の作ったクラスが手続き型と言われなきゃいけないんですか
akikogoto
1
110
Compose 1.7のTextFieldはPOBox Plusで日本語変換できない
tomoya0x00
0
180
Importmapを使ったJavaScriptの 読み込みとブラウザアドオンの影響
swamp09
4
1.4k
外部システム連携先が10を超えるシステムでのアーキテクチャ設計・実装事例
kiwasaki
1
290
よくできたテンプレート言語として TypeScript + JSX を利用する試み / Using TypeScript + JSX outside of Web Frontend #TSKaigiKansai
izumin5210
5
1.6k
Enabling DevOps and Team Topologies Through Architecture: Architecting for Fast Flow
cer
PRO
0
290
ECS Service Connectのこれまでのアップデートと今後のRoadmapを見てみる
tkikuc
2
250
Remix on Hono on Cloudflare Workers
yusukebe
1
170
PLoP 2024: The evolution of the microservice architecture pattern language
cer
PRO
0
2.7k
Pinia Colada が実現するスマートな非同期処理
naokihaba
4
220
Featured
See All Featured
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
Building Applications with DynamoDB
mza
90
6.1k
For a Future-Friendly Web
brad_frost
175
9.4k
Bash Introduction
62gerente
608
210k
Typedesign – Prime Four
hannesfritz
40
2.4k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
A better future with KSS
kneath
238
17k
Unsuck your backbone
ammeep
668
57k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
506
140k
Transcript
Gotanda.rb#47 Mailgun3ΫοΩϯά 2020/10/27 גࣜձࣾτϨλ @hiroki_tanaka
ࣗݾհ ɾాத جɿ@hiroki_tanaka ɾSIerɿϑϡʔνϟʔΞʔΩςΫτ(4ؒ) →WebܥϕϯνϟʔɿגࣜձࣾτϨλ(1ؒ) ɾRubyɿ2ɾJavaɿ2ɾSQLɿ5 →࠷ۙΠϯϑϥΤϯδχΞݟश͍தɻ ɾझຯɿεϓϥτΡʔϯɾમ౬८Γ
αʔϏεհ - τϨλҿ৯ళ͚ͷ༧ாɾސ٬ா͕ϝΠϯ ϓϩμΫτɻ(࠷ۙͩͱGoToEatʹࢀՃ͍ͯ͠·͢ɻ)
αʔϏεհ - ίϩφՒʹҿ৯ళ͕͜Ε·ͰஷΊ͖ͯͨாͷސ٬ʹ DMΛҰׅૹ৴ग़དྷΔʮτϨλϝοηʔδʯͱ͍͏αʔ Ϗε͕ੜ·Εͨɻ
ࠓ͢͜ͱɾ͞ͳ͍͜ͱ ʲ͢͜ͱʳ - RailsͱMailgunΛ༻͍ͨૹ৴ϝʔϧͷड৴֬ೝɾ։෧֬ೝɾ URLͷΫϦοΫ֬ೝͷऔಘʹ͍ͭͯ (MailgunΛ͍ͬͯΔਓʹͱͬͯجຊతͳΛ͢ΔͷͰɺऍᷟʹઆ๏͔Ͱ͢…) ʲ͞ͳ͍͜ͱʳ - ϝʔϧؔ࿈ͷgem →ruby-gmailletter_opener_webͳͲ
- ActionMailerͷ͍ํ
ϝʔϧͰ͋Γ͕ͪͳཁ͕ τϨλϝοηʔδͰ͋Γ·ͨ͠ɻ
γεςϜ͔Βૹ৴ͨ͠ϝʔϧ͕Ͳͷఔड৴ ͞Ε͔ͨΓ͍ͨɻ
ૹ৴ͨ͠ϝʔϧͷɺԿ%͕։෧͞ ΕͯຊจͷURL͕ͲͷఔΫϦοΫ͞Ε͔ͨ Γ͍ͨɻ
Ͳ͏࣮ͬͯݱ͠Α͏ʁ
ϝʔϧૹ৴ʹඞཁͳESPଆͰ Կͱ͔ग़དྷͳ͍͔ͳ…ʁ
ESPͱ - ESPͱEmail Service ProdiverͷུͰిࢠϝʔϧ৴ γεςϜαʔϏεΛఏڙ͢Δࣄۀऀͷ͜ͱɻ - ಛʹϚʔέςΟϯάϝʔϧτϥϯβΫγϣϯϝʔϧ(༧֬ ೝϝʔϧͳͲ)ͱ͍ͬͨࣄۀʹ݁͢Δϝʔϧͷ৴͕ओɻ -
༗໊ॴͩͱSendGridMailChimpɾAmazon SESɻ - ٛʹGmailyahooϝʔϧ֘͢Δɻ
ࠓճ༻͢ΔESPɿMailgun - ࠓճMailgunͱ͍͏ESPΛ༻ɻ - Mailgun Technologies Inc͕ఏڙ͍ͯ͠ΔαʔυύʔςΟͷ ΫϥυϝʔϧαʔϏεɻ - ຖ݄࠷େ1ສ௨·ͰແྉͰϝʔϧΛૹ৴Ͱ͖ΔͷͰɺखܰʹ
ελʔτग़དྷΔɻ (Googleύʔτφʔϖʔδ͔ΒͷਃࠐΈͩͱ3ສ௨·Ͱແྉɻ)
MailgunͷϝϦοτ ʲϝϦοτʳ - ॳظઃఆ͕؆୯ɻ →ΞΧϯτ࡞~Mailgunઃఆ~DNSઃఆ~RailsଆʹͦͷMailgunઃఆΛద༻͢Δ͚ͩ Ͱɺ͙͢ʹϝʔϧૹ৴ՄೳʹͳΔɻ - APIΛެ։͍ͯ͠ΔͷͰɺϓϩάϥϜ͔ΒͷΧελϚΠζ͕Γ͢ ͍ɻ -
ϩάϏϡʔϫʔ͕͋Γ͔ͦ͜Βϝʔϧͷ࠶ૹ͕Ͱ͖ɺࣗલͷϩάཧ ෆཁɻ - ཧऀҎ֎ͷݖݶͰϢʔβൃߦ͕ग़དྷΔɻ →ඇΤϯδχΞͷνʔϜʹτϥϒϧγϡʔτϝʔϧੳΛґཔͰ͖Δɻ
MailgunͷσϝϦοτ ʲσϝϦοτʳ - RFC४ڌ͍ͯ͠ͳ͍ຊͷΩϟϦΞಠࣗڐ༰ͷϝʔϧΞυϨε ͷ৴Λແ༻Ͱ͘ɻ →ʮ
[email protected]
ʯʮ
[email protected]
ʯͳͲ - ͦͦ͜͜ߴՁͰAmazon SESͷ4ഒఔɻ
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/
Webhook͔ΒPOST͞ΕΔύϥϝʔλྫ - `event-data`ͱ͍͏ͷϋογϡΩʔͷόϦϡʔͱ͠ ͯҎԼ͕֨ೲ͞ΕΔɻ
࣮खॱ 1. ϝʔϧػೳΛHTMLϝʔϧܗࣜͰ࣮͢Δɻ →Mailgunͷ։෧֬ೝϝʔϧʹࣗಈͰຒΊࠐ·Εͨը૾͕։͔Ε͔ͨʹΑͬͯఆ͞ΕΔͨ ΊɺϓϨʔϯςΩετϝʔϧͰ։෧֬ೝग़དྷͳ͍ɻ 2. ֤ϝʔϧͷϔομʔʹMailgunͷWebhookΛൃՐ͢ΔͨΊͷΩʔόϦϡʔΛࠐ Ήɻ →ࠓճυϝΠϯ୯ҐͰͳ͘ɺϝʔϧ୯ҐͰߦ͏ɻ 3.
MailgunଆʹWebhook࣮ߦ࣌ͷPOSTઌͷURLΛઃఆ͢Δɻ 4. POST͞ΕͨσʔλΛRailsଆͰड͚औͬͯɺϏδωεϩδοΫʹ߹ΘͤͯΑ͠ͳʹ ࣮͢Δɻ
࣮खॱ 1. ϝʔϧػೳΛHTMLϝʔϧܗࣜͰ࣮͢Δɻ →Mailgunͷ։෧֬ೝϝʔϧʹࣗಈͰຒΊࠐ·Εͨը૾͕։͔Ε͔ͨʹΑͬͯఆ͞ΕΔͨ ΊɺϓϨʔϯςΩετϝʔϧͰ։෧֬ೝग़དྷͳ͍ɻ 2. ֤ϝʔϧͷϔομʔʹMailgunͷWebhookΛൃՐ͢ΔͨΊͷΩʔόϦϡʔΛࠐ Ήɻ →ࠓճυϝΠϯ୯ҐͰͳ͘ɺϝʔϧ୯ҐͰߦ͏ɻ 3.
MailgunଆʹWebhook࣮ߦ࣌ͷPOSTઌͷURLΛઃఆ͢Δɻ 4. POST͞ΕͨσʔλΛRailsଆͰड͚औͬͯɺϏδωεϩδοΫʹ߹ΘͤͯΑ͠ͳʹ ࣮͢Δɻ ͜Ε͔Βઆ໌͢Δ෦
ϝʔϧͷϔομʔઃఆ - ϝʔϧૹ৴ॲཧͷafter_actionͰMailgunͷ։෧֬ೝɾ ΫϦοΫ֬ೝͷͨΊͷϔομʔΛ༩͢Δɻ (ड৴֬ೝϔομʔͷઃఆ͕ෆཁɻ) - ࢠͷMailerΫϥε - ͷApplicationMailerΫϥε
࣮ࡍͷϝʔϧͷϔομʔ - ૹ৴͞Εͨϝʔϧͷϔομʔʹ։෧֬ೝͱΫϦοΫ֬ ೝͷͨΊͷ`X-Mailgun-Track`͕ઃఆ͞ΕΔɻ
MailgunͷWebhookઃఆ - RailsΞϓϦͷWebhookͷPOSTઌΛMailgunͷ Webhookʹઃఆ͢Δɻ - Mailgunଆͷઃఆ - RailsଆͷϧʔςΟϯάઃఆ (ɿPathμϛʔͰ͢)
RailsͰWebhookͷPOSTΛड͚ͯͷॲཧ - Mailgun͔ΒͷPOSTΛड͚࣮ͯߦ͢ΔॲཧΛRailsʹ࣮͢ Δɻ - `event-data`Լʹ͋Δ`event`ʹԠͯ͡ॲཧΛذͤ͞Δɻ
ૹ৴݁Ռ֬ೝը໘Ͱͷ֬ೝ - ࠷ޙʹRailsΞϓϦଆͷૹ৴݁Ռ֬ೝը໘͔ΒνΣοΫ͢Δɻ - ԼهͷྫͰड৴֬ೝͰ͢ɻ
্खʹग़དྷ·ͨ͠ʂ
ҙ 1. Mailgunͷ։෧֬ೝϝʔϧʹࣗಈͰຒΊࠐ·Εͨ ը૾͕։͔Ε͔ͨʹΑͬͯఆ͞ΕΔͨΊɺϓϨʔϯς ΩετϝʔϧඇରԠɻ 2. େྔͷϝʔϧΛMailgunܦ༝Ͱૹ৴͢ΔͱMailgun ΤϥʔʹͳΒͣʹਖ਼ৗʹૹ৴ॲཧΛߦͬͯ͘ΕΔɻ →ಉ࣌ʹେྔͷWebhook͕ΞϓϦέʔγϣϯʹPOST͞ ΕΔͨΊɺηϧϑDoS߈ܸঢ়ଶʹͳͬͯ͠·͏ɻ
(࣮ࡍʹͦΕͰΞϓϦέʔγϣϯ͕1མͪ·ͨ͠…)
͓ΘΓʹ - MailgunͷWebhookػೳΛར༻͢Δ͜ͱͰૹ৴ϝʔ ϧͷड৴֬ೝɾ։෧֬ೝɾΫϦοΫ֬ೝΛ؆୯ʹग़དྷ ·ͨ͠ɻ - ੳ͕खܰʹͰ͖Δ͜ͱࣄۀͷʹ݁͢ΔͷͰ େࣄɻ - ͱ͍͑ɺMailgunΛ༻͠ͳ͍ଞͷํ๏͋Δͱࢥ
͍·͢ͷͰɺ৭ʑڭ͚͑ͯΔͱخ͍͠Ͱ͢ʂ
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ