デコメールをrubyから送る方法
rubyでおくるとき - 計算機と戯れる日々では色つき文字や画像を添付する送り方をアップしていなかったことに今日気がついた。なんのための備忘録だろうか(笑
ということでさらす。
#!/usr/bin/ruby require 'net/smtp' Net::SMTP.start( 'smtpServerAddress', 25 ) {|smtp| smtp.send_mail <<EndOfMail, 'bar@from.com', 'foo@docomo.ne.jp' To: foo@docomo.ne.jp From: bar@from.com Content-Type: text/html Subject: test MIME-Version: 1.0 Content-Type: multipart/related; boundary="mm00" --mm00 Content-Type: multipart/alternative; boundary="mm01" --mm01 Content-Type: text/plain ここはHTMLで読めないときに表示される部分 --mm01 Content-type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit <font color="red">赤い文字</font><br /> <font color="blue">青い文字</font><br /> <br /><br /><a href="http://d.hatena.ne.jp/n9d/">URL貼り付け例</a><br /> 本文 --mm01-- EndOfMail }
画像を含む場合の送信方法は以下のとおり
#!/usr/bin/ruby require 'net/smtp' Net::SMTP.start( 'smtpServerAddress', 25 ) {|smtp| smtp.send_mail <<EndOfMail, 'bar@from.com', 'foo@docomo.ne.jp' To: foo@docomo.ne.jp From: bar@from.com Content-Type: text/html Subject: test MIME-Version: 1.0 Content-Type: multipart/related; boundary="mm00" --mm00 Content-Type: multipart/alternative; boundary="mm01" --mm01 Content-Type: text/plain ここはHTMLで読めないときに表示される部分 --mm01 Content-type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit <font color="red">赤い文字</font><br /> <font color="blue">青い文字</font><br /> <br /><br /><a href="http://d.hatena.ne.jp/n9d/">URL貼り付け例</a><br /> 1枚目の画像 <br /> <img src="cid:10"> <br /> 2枚目の画像 <br /> <img src="cid:11"> <br /> 本文 --mm01-- --mm00 Content-Type: image/jpeg; name="a.jpg" Content-Transfer-Encoding: base64 Content-ID: <10> /9j/4AAQSkZJRgABAQEASABIAAD/2wBDABsSFBcUERsXFhceHBsgKEIrKCUl KFE6PTBCYFVlZF9VXVtqeJmBanGQc1tdhbWGkJ6jq62rZ4C8ybqmx5moq6T/ 2wBDARweHigjKE4rK06kbl1upKSkpKSkpKSkpKSkpKSkpKSkpKSkpKSkpKSk pKSkpKSkpKSkpKSkpKSkpKSkpKSkpKT/wAARCAAVAGQDASIAAhEBAxEB/8QA GgAAAwEBAQEAAAAAAAAAAAAAAAMFAQIEBv/EACUQAAICAQQCAgMBAQAAAAAA AAECAAMRBBIhMRNBBRQiUVJxMv/EABcBAQEBAQAAAAAAAAAAAAAAAAABAgP/ xAAdEQACAwADAQEAAAAAAAAAAAAAAQIREhMhMVFh/9oADAMBAAIRAxEAPwD6 RmCjJi01G5WPisGPRHJ/yZdUtmPKoceuOoMhC7RwfR29TSSId1XC0Z2sh/lu DF6vU/W8f47t7Y76nNdG19zgNb/YTER8sDspGed3c1GKcqI26PXq7/r0Gzbu wesxlb761frIBkrW1atNOTdcHTI4E7wdTfVp2crWtYbAPfE1hV6TXZTyIijU +XUW1bQPH7z3PCtIo+TrqV2ZcZwT1xN0tS1avUsuT4gSvMYVC2VISIi2XIbG rudz06twI283WVaVbNy2Fiuff+xxd1Y0VoSatf1PkURGYqykkE9zNPp/u1tf ba24k4wf+ZnC9vouinkfuEg0k4bLE/kecwmuL9JovxVlCWOrsWyP0xEITknR sbEarTDU7MsV2HPEIQm07QN1NA1FJrLFQfYi7tElqp+TK6DAYdwhKpNeEpHF Xx61Xrb5XZh3n3HVaZa7rbNxPk7B9QhDm36KQk/HLkhLbEQ9oDxGWaNG8IBK io5A/cIRuQpHT6cPqkv3HKjGImz45Gdilj1hu1U8GEIU2hSMT4xEBAsbGc9Q hCXcvoyj/9k= --mm00 Content-Type: image/jpeg; name="b.jpg" Content-Transfer-Encoding: base64 Content-ID: <11> /9j/4AAQSkZJRgABAQEASABIAAD/2wBDABsSFBcUERsXFhceHBsgKEIrKCUl KFE6PTBCYFVlZF9VXVtqeJmBanGQc1tdhbWGkJ6jq62rZ4C8ybqmx5moq6T/ 2wBDARweHigjKE4rK06kbl1upKSkpKSkpKSkpKSkpK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dx0WJ3r+pWu x2JXDprfj6gsew+BNaJe93RTuwntsLDJtQH4U9pLjUGhSGtezfyxk8TPa5gR ESD/2Q== --mm00-- EndOfMail }
画像ファイルは以下の出力結果を張り付け。
$ wget http://www.hatena.ne.jp/images/top/headbutton_diary.gif $ convert -quality 30 -resize 100x100 headbutton_diary.gif h2.jpg $ cat h2.jpg | ruby -e 'print [STDIN.read].pack("m")'