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.9k
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.4k
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
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
1
260
Remix on Hono on Cloudflare Workers
yusukebe
1
320
初めてDefinitelyTypedにPRを出した話
syumai
0
430
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.4k
TypeScriptでライブラリとの依存を限定的にする方法
tutinoko
3
730
Hotwire or React? ~アフタートーク・本編に含めなかった話~ / Hotwire or React? after talk
harunatsujita
1
120
카카오페이는 어떻게 수천만 결제를 처리할까? 우아한 결제 분산락 노하우
kakao
PRO
0
120
Realtime API 入門
riofujimon
0
150
WebAssembly Unleashed: Powering Server-Side Applications
chrisft25
0
200
Ethereum_.pdf
nekomatu
0
470
最新TCAキャッチアップ
0si43
0
220
OSSで起業してもうすぐ10年 / Open Source Conference 2024 Shimane
furukawayasuto
0
110
Featured
See All Featured
Thoughts on Productivity
jonyablonski
67
4.3k
Gamification - CAS2011
davidbonilla
80
5k
Optimizing for Happiness
mojombo
376
70k
GraphQLとの向き合い方2022年版
quramy
43
13k
4 Signs Your Business is Dying
shpigford
180
21k
It's Worth the Effort
3n
183
27k
Producing Creativity
orderedlist
PRO
341
39k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
31
2.7k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.3k
The Art of Programming - Codeland 2020
erikaheidi
52
13k
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Λ༻͠ͳ͍ଞͷํ๏͋Δͱࢥ
͍·͢ͷͰɺ৭ʑڭ͚͑ͯΔͱخ͍͠Ͱ͢ʂ
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ