メール製品を作って気づいたmailライブラリの光と闇について語る

21
mail 16 5 29

Transcript of メール製品を作って気づいたmailライブラリの光と闇について語る

Page 1: メール製品を作って気づいたmailライブラリの光と闇について語る

mail

16 5 29

Page 2: メール製品を作って気づいたmailライブラリの光と闇について語る

self.about• Toshio Maki(Twitter: @Kirika_K2)

• Hatena id:Kirika, Github: kirikak2

••

(MilterManager / Ruby / Rails)

•(GitLab / Drone / CloudFoundry)

16 5 29

Page 3: メール製品を作って気づいたmailライブラリの光と闇について語る

mail

• https://github.com/mikel/mail

• ActionMailer

•mail

16 5 29

Page 4: メール製品を作って気づいたmailライブラリの光と闇について語る

Context• mail

•••

• gem mail gem

16 5 29

Page 5: メール製品を作って気づいたmailライブラリの光と闇について語る

Generate new mailmail = Mail.new do to "[email protected]" from "[email protected]" subject "Test mail" body "This is a test mail"end

puts mail.to_sDate: Sun, 15 May 2016 14:10:34 +0900From: [email protected]: [email protected]: <[email protected]>Subject: Test mailMime-Version: 1.0Content-Type: text/plain; charset=UTF-8Content-Transfer-Encoding: 7bit

This is a test mail

16 5 29

Page 6: メール製品を作って気づいたmailライブラリの光と闇について語る

Parse new mailmail = Mail.new(File.read(‘/path/to/mail’))

puts mail.to_sDate: Sun, 15 May 2016 14:10:34 +0900From: [email protected]: [email protected]: <[email protected]>Subject: Test mailMime-Version: 1.0Content-Type: text/plain; charset=UTF-8Content-Transfer-Encoding: 7bit

This is a test mail

16 5 29

Page 7: メール製品を作って気づいたmailライブラリの光と闇について語る

mail•

• Notes / Outlook RFC

• Syntax

••

• uuencode okkez PR

16 5 29

Page 8: メール製品を作って気づいたmailライブラリの光と闇について語る

mail

maildata = File.read("japanese_shift_jis.eml") => "Delivered-To: [email protected]\nDate: Wed, 28 May 2014 17:18:19 +0900 (JST)\nFrom: [email protected]\nTo: [email protected]\nSubject: test\nMessage-ID: <[email protected]>\nMIME-Version: 1.0\nContent-Type: text/plain; charset=\"Shift_JIS\"\nContent-Transfer-Encoding: 8bit\n\n\x82\xA0\x82\xA2\x82\xA4\x82\xA6\x82\xA8\n\n\x82\xB1\x82\x81\x81[\x83\x8B\x82e\x83X\x83g\x97p\x82\x81\x81[\x83\x8B\x82ł\xB7\x81B\n\n\x8D\xA1\x8C\xE3\x82Ƃ\xE0\x82\xE6\x82낵\x82\xAD\x82\xA8\x8A \x90\\\x82\xB5\x8F \x82ݏݎݍܯܮܭܬܫܪܩܨܧܦܥܤܣܢܡܠܟܞܝܜܛܚܙܘܗܖܕܔܓܒܐ܍܌܋܊܉܈܇܆܅܄܃܂܁܀\xB7\x81I\n"

Mail.new(File.read("japanese_shift_jis.eml")).to_s => "Date: Wed, 28 May 2014 17:18:19 +0900\r\nFrom: [email protected]\r\nTo: [email protected]\r\nMessage-ID: <[email protected]>\r\nSubject: test\r\nMime-Version: 1.0\r\nContent-Type: text/plain;\r\n charset=Shift_JIS\r\nContent-Transfer-Encoding: base64\r\nDelivered-To: [email protected]\r\n\r\ngqCCooKkgqaCqAoKgrGCzIOBgVuDi4LNg2WDWINnl3CCzIOBgVuDi4LFgreB\r\nQgoKjaGM44LGguCC5oLrgrWCrYKoiuiCopBcgrWP44KwgtyCt4FJCg==\r\n"

16 5 29

Page 9: メール製品を作って気づいたmailライブラリの光と闇について語る

• Content-Transfer-Encoding

body Encoding

• Mail

Encoding

• Mail

Encoding

16 5 29

Page 10: メール製品を作って気づいたmailライブラリの光と闇について語る

• RFC

••

2

16 5 29

Page 11: メール製品を作って気づいたmailライブラリの光と闇について語る

……

•content-transfer-encoding → Content-Transfer-Encoding

mail gem

16 5 29

Page 12: メール製品を作って気づいたmailライブラリの光と闇について語る

:no_header_formatted

PR

16 5 29

Page 13: メール製品を作って気づいたmailライブラリの光と闇について語る

To get mail data

• To get headermail.tomail.header[:to]

• To get mail bodymail.body.to_s

16 5 29

Page 14: メール製品を作って気づいたmailライブラリの光と闇について語る

To modify header

• mail.to = ‘[email protected]

• smtp_envelope_to

mail.smtp_envelope_to = ‘[email protected]

16 5 29

Page 15: メール製品を作って気づいたmailライブラリの光と闇について語る

To get attachment files

• To get attachment filesmail.attachments

• readme ……

• HTML HTML

16 5 29

Page 16: メール製品を作って気づいたmailライブラリの光と闇について語る

nested multipart

header partContent-Type: multipart/mixed; boundary="----=_Part_2192_32400445.1115745999735"

multipart header

multipart header

multipart body

mail.parts << Mail::Part.new do “This is multipart body”end

mail.parts << Mail::Part.new do part do “This is nested multipart body” endend

Content-Type: text/plain; boundary="--==_mimepart_57387affe6d00_c2e8082dbf8514b

nested multipart

16 5 29

Page 17: メール製品を作って気づいたmailライブラリの光と闇について語る

attachment

Mail::Message

Mail::Part(Mail::Message

16 5 29

Page 18: メール製品を作って気づいたmailライブラリの光と闇について語る

• Mail::Part

attachment?

• mail.parts.select(&:attachment?)

16 5 29

Page 19: メール製品を作って気づいたmailライブラリの光と闇について語る

Mail

• Mail::Parser

• Ragel

DSL

• Ruby

16 5 29

Page 20: メール製品を作って気づいたmailライブラリの光と闇について語る

16 5 29

Page 21: メール製品を作って気づいたmailライブラリの光と闇について語る

Thanks to mail• mail

16 5 29