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

Post on 07-Apr-2017

350 views 2 download

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

mail

16 5 29

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

• Hatena id:Kirika, Github: kirikak2

••

(MilterManager / Ruby / Rails)

•(GitLab / Drone / CloudFoundry)

16 5 29

mail

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

• ActionMailer

•mail

16 5 29

Context• mail

•••

• gem mail gem

16 5 29

Generate new mailmail = Mail.new do to "alice@example.com" from "bob@example.com" subject "Test mail" body "This is a test mail"end

puts mail.to_sDate: Sun, 15 May 2016 14:10:34 +0900From: bob@example.comTo: alice@example.comMessage-ID: <573804ca7b995_6788082dbf8745d3@gitorious.orz.cn.mail>Subject: Test mailMime-Version: 1.0Content-Type: text/plain; charset=UTF-8Content-Transfer-Encoding: 7bit

This is a test mail

16 5 29

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

puts mail.to_sDate: Sun, 15 May 2016 14:10:34 +0900From: bob@example.comTo: alice@example.comMessage-ID: <573804ca7b995_6788082dbf8745d3@gitorious.orz.cn.mail>Subject: Test mailMime-Version: 1.0Content-Type: text/plain; charset=UTF-8Content-Transfer-Encoding: 7bit

This is a test mail

16 5 29

mail•

• Notes / Outlook RFC

• Syntax

••

• uuencode okkez PR

16 5 29

mail

maildata = File.read("japanese_shift_jis.eml") => "Delivered-To: unknown@example.com\nDate: Wed, 28 May 2014 17:18:19 +0900 (JST)\nFrom: xxxxxxx@docomo.ne.jp\nTo: unknown@example.com\nSubject: test\nMessage-ID: <xxxxx@docomo.ne.jp>\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: xxxxxxx@docomo.ne.jp\r\nTo: unknown@example.com\r\nMessage-ID: <xxxxx@docomo.ne.jp>\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: unknown@example.com\r\n\r\ngqCCooKkgqaCqAoKgrGCzIOBgVuDi4LNg2WDWINnl3CCzIOBgVuDi4LFgreB\r\nQgoKjaGM44LGguCC5oLrgrWCrYKoiuiCopBcgrWP44KwgtyCt4FJCg==\r\n"

16 5 29

• Content-Transfer-Encoding

body Encoding

• Mail

Encoding

• Mail

Encoding

16 5 29

• RFC

••

2

16 5 29

……

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

mail gem

16 5 29

:no_header_formatted

PR

16 5 29

To get mail data

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

• To get mail bodymail.body.to_s

16 5 29

To modify header

• mail.to = ‘maki@example.com’

• smtp_envelope_to

mail.smtp_envelope_to = ‘maki@example.org’

16 5 29

To get attachment files

• To get attachment filesmail.attachments

• readme ……

• HTML HTML

16 5 29

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

attachment

Mail::Message

Mail::Part(Mail::Message

16 5 29

• Mail::Part

attachment?

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

16 5 29

Mail

• Mail::Parser

• Ragel

DSL

• Ruby

16 5 29

16 5 29

Thanks to mail• mail

16 5 29