$30 off During Our Annual Pro Sale. View Details »
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
7.3k
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
260
定期リリースの導入
hiroki_tanaka
0
190
noteの品質課題に立ち上げ直後のQAチームが挑んだ軌跡
hiroki_tanaka
1
1.5k
note初のBug Bashを やってみた
hiroki_tanaka
1
1.5k
コロナ禍の1年間でAWSの資格を 3つ取得した話
hiroki_tanaka
0
430
Rubocop対応のすゝめ
hiroki_tanaka
0
72
Gotanda.rb#48 ECS on Fargateでのハマりポイント
hiroki_tanaka
1
360
Gotanda.rb#46 権限管理のつらみとPundit
hiroki_tanaka
1
7.3k
Other Decks in Programming
See All in Programming
関数の挙動書き換える
takatofukui
4
750
非同期処理の迷宮を抜ける: 初学者がつまづく構造的な原因
pd1xx
1
250
Java_プロセスのメモリ監視の落とし穴_NMT_で見抜けない_glibc_キャッシュ問題_.pdf
ntt_dsol_java
0
230
Querying Design System デザインシステムの意思決定を支える構造検索
ikumatadokoro
1
1.2k
Module Harmony
petamoriken
2
580
全員アーキテクトで挑む、 巨大で高密度なドメインの紐解き方
agatan
8
11k
レイトレZ世代に捧ぐ、今からレイトレを始めるための小径
ichi_raven
0
480
AWS CDKの推しポイントN選
akihisaikeda
1
220
「文字列→日付」の落とし穴 〜Ruby Date.parseの意外な挙動〜
sg4k0
0
320
Combinatorial Interview Problems with Backtracking Solutions - From Imperative Procedural Programming to Declarative Functional Programming - Part 1
philipschwarz
PRO
0
110
知られているようで知られていない JavaScriptの仕様 4選
syumai
0
640
CSC305 Lecture 17
javiergs
PRO
0
200
Featured
See All Featured
Building Applications with DynamoDB
mza
96
6.8k
Thoughts on Productivity
jonyablonski
73
4.9k
The Pragmatic Product Professional
lauravandoore
36
7k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
11
950
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Large-scale JavaScript Application Architecture
addyosmani
514
110k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
We Have a Design System, Now What?
morganepeng
54
7.9k
Scaling GitHub
holman
464
140k
Building Adaptive Systems
keathley
44
2.8k
It's Worth the Effort
3n
187
29k
[RailsConf 2023] Rails as a piece of cake
palkan
58
6.1k
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Λ༻͠ͳ͍ଞͷํ๏͋Δͱࢥ
͍·͢ͷͰɺ৭ʑڭ͚͑ͯΔͱخ͍͠Ͱ͢ʂ
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ