<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="ja" xmlns="http://www.w3.org/2005/Atom">
  <title>code&#12394;&#12395;&#12364;&#12375;: Ruby&#12391;&#26085;&#26412;&#35486;&#12513;&#12540;&#12523;&#12434;&#36865;&#20449;&#12377;&#12427;&#38306;&#25968;</title>
  <link type="text/html" rel="alternate" href="http://code.nanigac.com/source/view/108"/>
  <link rel="icon" href="/favicon.ico"/>
  <updated>2008-05-18T13:35:02+09:00</updated>
  <author>
    <name>genki</name>
  </author>
  <entry>
    <title>1</title>
    <link type="text/html" rel="alternate" href="http://code.nanigac.com/source/view/108?comment=1"/>
    <author>
      <name>ujihisa</name>
    </author>
    <updated>2007-08-04T11:03:51+09:00</updated>
    <summary>&#12401;&#12387;&#12392;&#12477;&#12540;&#12473;&#12434;&#35501;&#12435;&#12384;&#38480;&#12426;&#12391;&#12399;
msg&#12399;&#12464;&#12525;&#12540;&#12496;&#12523;&#22793;&#25968;&#12391;&#12394;&#12367;&#12390;&#12418;&#21205;&#12367;&#12392;&#24605;&#12358;&#12398;&#12391;&#12377;&#12364;&#12289;
&#20309;&#12363;&#29305;&#27530;&#12394;&#20107;&#24773;&#12364;&#12354;&#12427;&#12398;&#12391;&#12375;&#12423;&#12358;&#12363;?

# &#31169;&#12399;net/smtp&#12395;&#12399;&#12354;&#12414;&#12426;&#35443;&#12375;&#12367;&#12394;&#12356;&#12391;&#12377;:-)
</summary>
  </entry>
  <entry>
    <title>2</title>
    <link type="text/html" rel="alternate" href="http://code.nanigac.com/source/view/108?comment=2"/>
    <author>
      <name>&#12466;&#12473;&#12488;</name>
    </author>
    <updated>2007-08-04T11:19:23+09:00</updated>
    <summary>&#12392;&#12356;&#12358;&#12363;&#12289;&#12371;&#12398;&#20154;&#12398;&#26360;&#12356;&#12383;Ruby&#12473;&#12463;&#12522;&#12503;&#12488;&#12399;&#12393;&#12428;&#12418;&#12300;Ruby&#12425;&#12375;&#12367;&#12394;&#12356;&#12301;&#12418;&#12398;&#12400;&#12363;&#12426;&#12290;
http://code.nanigac.com/user/source/14
TRUE&#12394;&#12435;&#12390;&#23450;&#25968;&#20351;&#12387;&#12390;&#12427;&#20154;&#20037;&#12375;&#12406;&#12426;&#12395;&#35211;&#12383;&#12424;&#12290;
&#12354;&#12414;&#12426;Ruby&#12395;&#12399;&#35443;&#12375;&#12367;&#12394;&#12356;&#12435;&#12384;&#12429;&#12358;&#12392;&#24605;&#12358;&#12290;

&#12354;&#12392;&#12513;&#12540;&#12523;&#20316;&#25104;&#12394;&#12425;tmail&#12434;&#12452;&#12531;&#12473;&#12488;&#12540;&#12523;&#12377;&#12428;&#12400;&#21106;&#12392;&#31777;&#21336;&#12290;
http://i.loveruby.net/ja/projects/tmail/doc/usage.html
TMail::Mail.new &#12375;&#12390; mail.from = ['from@example.com'] &#12392;&#12363;&#12375;&#12390;&#12356;&#12387;&#12390;
&#26368;&#24460;&#12395; mail.encoded &#12375;&#12390;&#36969;&#24403;&#12395; SMTP &#12391;&#36865;&#20449;&#12290;
</summary>
  </entry>
  <entry>
    <title>3</title>
    <link type="text/html" rel="alternate" href="http://code.nanigac.com/source/view/108?comment=3"/>
    <author>
      <name>genki</name>
    </author>
    <updated>2007-08-05T15:39:53+09:00</updated>
    <summary>&gt;&gt;1 
 &#38291;&#36949;&#12360;&#12390; $msg &#12392;&#26360;&#12356;&#12390;&#12414;&#12375;&#12383;&#12290;&#12290;&#12290;

&gt;&gt;2
 &#12399;&#12356;&#12289;&#12381;&#12358;&#12391;&#12377;&#12290;&#32013;&#26399;&#12395;&#36861;&#12356;&#12414;&#12367;&#12425;&#12428;&#12390;&#35226;&#12360;&#12383;&#65295;&#26360;&#12356;&#12390;&#12427;&#12435;&#12391;&#12289;&#21361;&#12394;&#12367;&#12394;&#12356;&#12467;&#12540;&#12489;&#12391;&#12354;&#12427;&#12424;&#12358;&#12395;&#12377;&#12427;&#12371;&#12392;&#12399;&#27671;&#12395;&#12363;&#12369;&#12390;&#12356;&#12427;&#12435;&#12391;&#12377;&#12364;&#12289;&#12354;&#12414;&#12426;Ruby&#12398;&#27969;&#20736;&#12395;&#12399;&#35443;&#12375;&#12367;&#28961;&#12356;&#12435;&#12391;&#12377;&#12424;&#12397;(^^;
Ruby&#12398;&#27969;&#20736;&#12391;&#12399;&#12289;&#26222;&#36890;&#20309;&#12434;&#36820;&#12377;&#20107;&#12364;&#22810;&#12356;&#12435;&#12391;&#12377;&#12363;&#65311;

TMail &#12394;&#12363;&#12394;&#12363;&#33391;&#12373;&#12381;&#12358;&#12391;&#12377;&#12397;&#12290;&#26247;&#12434;&#35211;&#12388;&#12369;&#12390;&#35211;&#12390;&#12415;&#12414;&#12377;&#12397;&#12290;&#12354;&#12426;&#12364;&#12392;&#12358;&#12372;&#12374;&#12356;&#12414;&#12377;&#12290;

</summary>
  </entry>
  <entry>
    <title>4</title>
    <link type="text/html" rel="alternate" href="http://code.nanigac.com/source/view/108?comment=4"/>
    <author>
      <name>&#12469;&#12452;</name>
    </author>
    <updated>2007-08-06T09:43:51+09:00</updated>
    <summary>&#12356;&#12420;&#12356;&#12420;&#12289;true/false&#12434;&#36820;&#12377;&#20107;&#12418;&#12424;&#12367;&#12354;&#12426;&#12414;&#12377;&#12424;&#12290;
&#12464;&#12464;&#12387;&#12383;&#12425;&#12289;&#26132;&#12398;&#12496;&#12540;&#12472;&#12519;&#12531;&#12391;&#12399;&#23450;&#25968;TRUE&#12289;FALSE&#12289;NIL&#12375;&#12363;
&#20351;&#12360;&#12394;&#12363;&#12387;&#12383;&#12424;&#12358;&#12391;&#12377;&#12364;&#12289;&#29694;&#22312;&#12399;&#22793;&#25968;true, false, nil&#12364;&#20351;&#29992;&#12391;&#12365;&#12427;&#12398;&#12391;&#12289;&#12371;&#12385;&#12425;&#12434;&#20351;&#29992;&#12377;&#12427;&#20107;&#12364;&#25512;&#22888;&#12373;&#12428;&#12427;&#12424;&#12358;&#12391;&#12377;&#12290;

</summary>
  </entry>
  <entry>
    <title>5</title>
    <link type="text/html" rel="alternate" href="http://code.nanigac.com/source/view/108?comment=5"/>
    <author>
      <name>&#12466;&#12473;&#12488;</name>
    </author>
    <updated>2007-11-13T14:28:53+09:00</updated>
    <summary>&#65298;&#12385;&#12419;&#12435;&#12397;&#12427;&#12391;&#12371;&#12435;&#12394;&#12398;&#35211;&#12388;&#12369;&#12414;&#12375;&#12383;&#12290;
$ cat jmail.rb
=begin RDoc

= JMail is class for japanese mail only (MUA)

= Synopsis
 THIS SCRIPT IS PREPARE FOR SENDMAIL TO MIME ENCODE.
 ONLY JAPANESE MAIL BOTH PLAIN TEXT MAIL AND MULTIPART MAIL.
 &#26085;&#26412;&#35486;&#12398;&#12513;&#12540;&#12523;&#12434;&#20316;&#25104;&#12377;&#12427;&#12521;&#12452;&#12502;&#12521;&#12522;
 &#12486;&#12461;&#12473;&#12488;&#12513;&#12540;&#12523;&#12384;&#12369;&#12391;&#12394;&#12367;&#12501;&#12449;&#12452;&#12523;&#12434;&#28155;&#20184;&#12377;&#12427;&#12371;&#12392;&#12418;&#12391;&#12365;&#12427;&#12290;
 ISO-2022-JP&#65292;Base64&#12391;&#12456;&#12531;&#12467;&#12540;&#12489;&#12373;&#12428;&#12427;&#12290;

= Usage
 mail = JMail.new
 mail.from    = address of sender
 mail.to      = address of recipient
 mail.subject = subject
 mail.text    = mail body               &#12513;&#12540;&#12523;&#26412;&#25991;(String&#12463;&#12521;&#12473;)
 mail.attach  = path of attachment file &#28155;&#20184;&#12501;&#12449;&#12452;&#12523;&#12398;&#12497;&#12473;
 mail.prepare
 puts mail.encoded

= Author
 UNKNOWN

= Copyright
 Copyright (c) Licensed under the same terms as Ruby.
 
=end

require 'mime/types'
require 'nkf'
require 'tmail'

class JMail &lt; TMail::Mail
        include TMail

        # &#21021;&#26399;&#21270;&#12513;&#12477;&#12483;&#12489;
        def initialize
                super
                @plain_text = String.new
                @multi = Array.new
        end

        private

        # &#12450;&#12489;&#12524;&#12473;&#29992;&#12504;&#12483;&#12480;&#12395;&#26085;&#26412;&#35486;&#12364;&#21547;&#12414;&#12428;&#12427;&#22580;&#21512;&#12398;JIS Base64&#12456;&#12531;&#12467;&#12540;&#12489;&#12513;&#12477;&#12483;&#12489;
        def header_encode(str)
                /(.*?)&lt;(.*?)&gt;/ =~ str
                return $&amp; ? "#{NKF.nkf('-jM', $1)}&lt;#{$2}&gt;" : str
        end

        public

        # &#26085;&#26412;&#35486;&#12513;&#12540;&#12523;&#29992;&#12504;&#12483;&#12480;&#12398;&#12458;&#12540;&#12496;&#12540;&#12521;&#12452;&#12489;
        def bcc=(*strs)
                super strs.flatten.map!{|str| header_encode str }
        end

        # &#26085;&#26412;&#35486;&#12513;&#12540;&#12523;&#29992;&#12504;&#12483;&#12480;&#12398;&#12458;&#12540;&#12496;&#12540;&#12521;&#12452;&#12489;
        def cc=(*strs)
                super strs.flatten.map!{|str| header_encode str }
        end

        # &#26085;&#26412;&#35486;&#12513;&#12540;&#12523;&#29992;&#12504;&#12483;&#12480;&#12398;&#12458;&#12540;&#12496;&#12540;&#12521;&#12452;&#12489;
        def from=(*strs)
                super strs.flatten.map!{|str| header_encode str }
        end

        # &#26085;&#26412;&#35486;&#12513;&#12540;&#12523;&#29992;&#12504;&#12483;&#12480;&#12398;&#12458;&#12540;&#12496;&#12540;&#12521;&#12452;&#12489;
        def to=(*strs)
                super strs.flatten.map!{|str| header_encode str }
        end

        # &#26085;&#26412;&#35486;&#12513;&#12540;&#12523;&#29992;&#12504;&#12483;&#12480;&#12398;&#12458;&#12540;&#12496;&#12540;&#12521;&#12452;&#12489;
        def subject=(str)
                super NKF.nkf('-jM', str)
        end

        # &#12501;&#12449;&#12452;&#12523;&#12434;&#28155;&#20184;&#12377;&#12427;&#12513;&#12477;&#12483;&#12489;&#12290;&#35079;&#25968;&#12398;&#28155;&#20184;&#21487;&#12290;&#12501;&#12523;&#12497;&#12473;&#12434;&#25351;&#23450;&#12290;
        def attach(filepath)
                @multi &lt;&lt; filepath
        end

        # &#12513;&#12540;&#12523;&#26412;&#25991;&#12434;&#12475;&#12483;&#12488;&#12377;&#12427;&#12513;&#12477;&#12483;&#12489;
        def text=(str)
                @plain_text = NKF.nkf('-j', str)
        end

        # &#12513;&#12540;&#12523;&#26412;&#25991;&#12434;&#36820;&#12377;&#12513;&#12477;&#12483;&#12489;
        def text
                puts @plain_text
        end

        # &#12513;&#12540;&#12523;&#20316;&#25104;&#29992;&#12513;&#12477;&#12483;&#12489;
        def prepare
                self.mime_version = '1.0'
                if @multi.size == 0 then
                        self.body = @plain_text
                        self.set_content_type 'text', 'plain', 'charset' =&gt; 'ISO-2022-JP'
                        self.transfer_encoding = '7bit'
                else
                        plain = TMail::Mail.new
                        plain.set_content_type 'text', 'plain', 'charset' =&gt; 'ISO-2022-JP'
                        plain.transfer_encoding = '7bit'
                        plain.body = @plain_text
                        self.parts.push(plain)

                        @multi.each{|part|
                                filename = NKF.nkf('-jM', File.basename(part))
#                               part_encoded = NKF.nkf('-MB', File.read(part))
                                part_encoded = [File.read(part)].pack('m').chomp.gsub(/.{76}/, "\\1\n")
                                type = MIME::Types.of(part)[0].instance_variable_get(:@raw_media_type)
                                sub = MIME::Types.of(part)[0].instance_variable_get(:@raw_sub_type)
                                attachment = TMail::Mail.new
                                attachment.body = part_encoded
                                attachment.transfer_encoding = 'base64'
                                attachment.set_content_type(type, sub, 'name' =&gt; filename)
                                attachment.set_content_disposition('inline', 'filename' =&gt; filename)
                                self.parts.push(attachment)
                        }
                        self.write_back
                end
        end

        alias add_attachment attach
        alias attach= attach
end

</summary>
  </entry>
  <entry>
    <title>6</title>
    <link type="text/html" rel="alternate" href="http://code.nanigac.com/source/view/108?comment=6"/>
    <author>
      <name>hokousya</name>
    </author>
    <updated>2008-05-18T13:27:19+09:00</updated>
    <summary>&#36890;&#12426;&#12377;&#12364;&#12426;&#12391;&#12377;&#12364;&#12289;&#27671;&#12395;&#12394;&#12387;&#12383;&#12398;&#12391;&#12467;&#12513;&#12531;&#12488;&#12290;

Ruby &#12387;&#12413;&#12367;&#35211;&#12360;&#12394;&#12356;&#12398;&#12399;&#12289;&#12502;&#12525;&#12483;&#12463;&#12434;&#20351;&#12387;&#12390;&#12356;&#12394;&#12356;&#12398;&#12364;&#22823;&#12365;&#12356;&#12392;&#24605;&#12356;&#12414;&#12377;&#12424;&#12290;
&#20363;&#12360;&#12400;&#20170;&#22238;&#12398;&#22580;&#21512;&#12289;&#20197;&#19979;&#12398;&#12424;&#12358;&#12395;&#12377;&#12428;&#12400;&#12356;&#12356;&#12398;&#12391;&#12399;&#12290;

m = Net::SMTPSession.new('localhost', 25)
m.open_timeout=600
start() do
  m.sendmail(msg, from, to)
end

&#12371;&#12358;&#12377;&#12427;&#12392;&#12289; sendmail &#12391;&#20363;&#22806;&#12364;&#36215;&#12365;&#12390;&#12418;&#33258;&#21205;&#30340;&#12395; finish &#12373;&#12428;&#12414;&#12377;&#12290;
open_timeout &#12398;&#25351;&#23450;&#12364;&#24517;&#35201;&#12394;&#12369;&#12428;&#12400;&#12289;&#20197;&#19979;&#12391; OK &#12391;&#12377;&#12290;

Net::SMTPSession.start('localhost', 25) do |m|
  m.sendmail(msg, from, to)
end

&#12354;&#12392;&#12289; Ruby &#12399;&#12414;&#12384;&#21517;&#21069;&#20184;&#12365;&#24341;&#25968;&#12434;&#12469;&#12509;&#12540;&#12488;&#12375;&#12390;&#12356;&#12394;&#12356;&#12398;&#12391;&#12289;
address= &#12392;&#12363;&#12399;&#21336;&#12394;&#12427;&#12525;&#12540;&#12459;&#12523;&#22793;&#25968;&#12408;&#12398;&#20195;&#20837;&#12395;&#12394;&#12387;&#12390;&#12375;&#12414;&#12356;&#12414;&#12377;&#12290;
&#12414;&#12383;&#12289;&#26368;&#24460;&#12395;&#35413;&#20385;&#12375;&#12383;&#20516;&#12364;&#36820;&#12426;&#20516;&#12395;&#12394;&#12427;&#12398;&#12391;&#12289; return &#12399;&#19981;&#35201;&#12391;&#12377;&#12397;&#12290;

&#12371;&#12435;&#12394;&#12392;&#12371;&#12429;&#12391;&#12375;&#12423;&#12358;&#12363;&#12290;
&#30906;&#12363;&#12395; obsolete &#12384;&#12369;&#12393;&#12289;&#23450;&#25968;&#12398;&#26360;&#12365;&#26041;&#12391; dis &#12425;&#12428;&#12390;&#12418;&#22256;&#12387;&#12385;&#12419;&#12358;&#12424;&#12397;&#65288;&#65342;&#65342;&#65307;

</summary>
  </entry>
  <entry>
    <title>7</title>
    <link type="text/html" rel="alternate" href="http://code.nanigac.com/source/view/108?comment=7"/>
    <author>
      <name>hokousya</name>
    </author>
    <updated>2008-05-18T13:35:02+09:00</updated>
    <summary>&#12362;&#12387;&#12392;&#12289; start &#12395;&#12524;&#12471;&#12540;&#12496;&#20184;&#12369;&#24536;&#12428;&#12383;&#12290;

m = Net::SMTPSession.new('localhost', 25) 
m.open_timeout=600 
m.start() do 
 m.sendmail(msg, from, to) 
end

&#12391;&#12377;&#12397;&#12290;

</summary>
  </entry>
</feed>
