1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
j
= TMail λȤ
e
= TMail Usage
.
j
== TMail γ
e
== Abstruction
.
j
TMail Żҥ饤֥Ǥȥ
ܥåΥϥɥäޤŪκä
ᡢʻȤȤƤϡ֥뤫סֿ
बꤵƤꡢ饤¦ν˶ʤäƤޤ
e
TMail is 90% RFC compatible mail library. By using TMail, You can
get data from internet mail (e-mail) and write data to mail,
without knowning standard details.
.
j
== 뤫
ϰֺǽ˼줿ʬǤꡢTMail ǤդȤǤ
ޤ
e
== Getting information from e-mail
.
j
=== TMail::Mail 饹
e
=== class TMail::Mail
.
j
TMail::Mail 饹ϥ̤ä륪֥ȤǤޤɤˤ
ƤΥ֥ȤʤȤޤΥ֥Ȥˡ
̤ꤢޤ
(1) ʸĤ
(2) ե(̾)Ĥ
(3) Port Ĥ
ʸեϤ줾ʬޤǤʤФޤ
ΤǰʲΤ褦˺ޤ
e
At first you must create TMail::Mail object. There's three ways
to create Mail object. First one is "creating from string", second
way is "creating from file (name)". Examples are below:
.
--
require 'tmail'
mail = TMail::Mail.parse(string) # from String
mail = TMail::Mail.load(filename) # from file
--
j
ˤäϤʤȻפޤ
.
j
=== Port Loader
e
=== Port and Loader
.
j
Port ȤΤ TMail ˤ륽ɽǤȤ
Ҥʸե̾륽ǡTMail::Mail#parse load
ʸե Port ǥåפ Mail ֥Ȥ
Ƥޤ Port ǥåפ뤳Ȥʸե
(Ūˤ IMAP ץȥ⡩) ΰ㤤äƤޤ
Port 桼ľܺ뤳ȤϤޤʤǤ礦˥桼
Port 뤳ȤˤʤΤϡܥåΥåѡǤ Loader
ȤȤǤȤ MH ܥåˤ֤˽
ˤϰʲΤ褦ˤޤ
e
The third way to get TMail::Mail object is using the "port".
"port" is the abstruction of mail sources, e.g. strings or file names.
You can get ports by using mail loaders (TMail::*Loader classes).
Here's simple example:
.
--
require 'tmail'
loader = TMail::MhLoader.new( '/home/aamine/Mail/inbox' )
loader.each_port do |port|
mail = TMail::Mail.new(port)
# ....
end
--
j
=== TMail::Mail ֥Ȥ
e
=== Accessing EMail Attributes via TMail::Mail object
.
j
ʾΤ褦ʼʤ TMail::Mail ֥Ȥä顢ȤϤΥåɤ
Ƥ֤ǤƤΤȤϤǤޤȤ To: ɥ쥹ʤ
e
Now you can get any data from e-mail, by calling methods of
TMail::Mail object. For example, to get To: addresses...
.
--
require 'tmail'
mail = TMail::Mail.parse( 'To: Minero Aoki <aamine@loveruby.net>' )
p mail.to # => ["aamine@loveruby.net"]
--
j
Subject: ʤ
e
to get subject,
.
--
p mail.subject
--
j
Τʤ
e
to get mail body,
--
p mail.body
--
j
Ȥ褦ˡȤƤñǤ
.
j
ܤ TMail::Mail 饹Υե
ŪȤƤ sample/from-check.rb Ƥ
e
For more TMail::Mail class details, see reference manual.
For more examples, see sample/from-check.rb.
.
j
=== MIME ޥѡȥ
MIME ޥѡȥˤбƤޤޥѡȤΤȤ
Mail#multipart? ˤʤꡢ#parts TMail::Mail ֥Ȥ
ޤ
e
=== MIME multipart mail
TMail also supports MIME multipart mails.
If mail is multipart mail, Mail#multipart? returns true,
and Mail#parts contains an array of parts (TMail::Mail object).
.
--
require 'tmail'
mail = TMail::Mail.parse( multipart_mail_string )
if mail.multipart? then
mail.parts.each do |m|
puts m.main_type
end
end
--
j
ŪȤƤ sample/multipart.rb Ƥ
e
For examples, see sample/multipart.rb.
.
j
=== TMail ʤ
TMail ϡإåϼưǥǥɡɤޤ(ʸ)
ѹޤᤤˤ Base64 ΥǥɤϼưǤ
⤷ޤ
e
=== What TMail is NOT
TMail does not touch mail body. Does not decode body,
does not encode body, does not change line terminator.
(I want to support Base64 auto-decoding although.)
.
j
==
TMail::Mail 饹ΤǤȤˤФ
϶ʸ顢ܥå˺ꤿϥͳ
ݡȤƤ顢륪֥Ȥޤ
e
== Creating New Mail
.
--
require 'tmail'
# Example 1: create mail on only memory
mail = TMail::Mail.new
# Example 2: create mail on mailbox (on disk)
loader = TMail::MhLoader.new('/home/aamine/Mail/drafts')
mail = TMail::Mail.new( loader.new_port )
--
j
ä顢Ȥޤ
e
then fill headers and body.
.
--
mail.to = 'test@loveruby.net'
mail.from = 'Minero Aoki <aamine@loveruby.net>'
mail.subject = 'test mail'
mail.date = Time.now
mail.mime_version = '1.0'
mail.set_content_type 'text', 'plain', {'charset'=>'iso-2022-jp'}
mail.body = 'This is test mail.'
--
j
ɤΥإååȤ餤ʤɺ٤ʬ⤦äȥС
ΤǤޤƤޤȤꤢ嵭Υإå
åȤۤ褤Ǥ礦ޤֿžξϤޤ줾
ޤ⥫СΤǤޤƤޤ
С 1.0 ˴ԤƤ
Ǹʸޤ
e
At last, convert mail object to string.
.
--
str = mail.encoded
--
j
ݡȤ˽᤹ʤ顢˰ʲΤ褦ˤޤ
e
If you want to write mails against files directly
(without intermediate string), use Mail#write_back.
.
--
mail.write_back
--
j
write_back ʸ𤹤뤳Ȥʤեľܽߤޤ
ŪȤƤ sample/sendmail.rb Ƥ
e
For more examples, see sample/sendmail.rb.
.
|