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
|
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>File: Intro.txt</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel=StyleSheet href="../.././rdoc-style.css" type="text/css" media="screen" />
<script type="text/javascript" language="JavaScript">
<!--
function popCode(url) {
window.open(url, "Code",
"resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
}
//-->
</script>
</head>
<body bgcolor="white">
<table summary="Information on file" width="100%">
<tr class="title-row">
<td><table summary="layout" width="100%"><tr>
<td class="big-title-font" colspan="2">Intro.txt</td>
<td align="right"><table summary="layout" cellspacing="0" cellpadding="2">
<tr>
<td class="small-title-font">Path:</td>
<td class="small-title-font">guide/Intro.txt</td>
</tr>
<tr>
<td class="small-title-font">Modified:</td>
<td class="small-title-font">Wed Jan 15 20:46:44 PST 2003</td>
</tr>
</table>
</td></tr></table></td>
</tr>
</table>
<!-- banner header -->
<div class="description"><h1>Introduction to RubyMail</h1>
<p>
This will get you started with the basics of using RubyMail.
</p>
<h2>Turning Text into a Message Object</h2>
<pre>
message = RMail::Parser.read(input)
</pre>
<p>
The input can be a normal Ruby IO object or a string. The result is a <a
href="../../classes/RMail/Message.html">RMail::Message</a> object. See <a
href="../../classes/RMail/Parser.html">RMail::Parser</a> for more
information.
</p>
<h2>Turning a Message Object into Text</h2>
<pre>
File.open('my-message', 'w') { |f|
RMail::Serialize.write(f, message)
}
</pre>
<p>
or
</p>
<pre>
string = RMail::Serialize.write('', message)
</pre>
<p>
See <a href="../../classes/RMail/Serialize.html">RMail::Serialize</a> for
more information.
</p>
<p>
A convenient shortcut when you want the message as a string is
RMail::Message#to_s.
</p>
<pre>
string = message.to_s
</pre>
<p>
You can also use RMail::Message#each to process each line of the serialized
message in turn.
</p>
<pre>
message.each { |line|
puts line
}
</pre>
<h2>Manipulating a Message</h2>
<p>
You use the methods of the <a
href="../../classes/RMail/Message.html">RMail::Message</a> and <a
href="../../classes/RMail/Header.html">RMail::Header</a> classes to
manipulate the message.
</p>
<h3>Retrieve the Body</h3>
<p>
You can retrieve the text of a single part message with
RMail::Message#body.
</p>
<pre>
body = message.body
</pre>
<p>
But beware that if the message is a MIME multipart message, <tt>body</tt>
will be an Array of <a
href="../../classes/RMail/Message.html">RMail::Message</a> objects. If you
know you have a MIME multipart message (easily tested with
RMail::Message#multipart?), then you can retrieve them individually with
RMail::Message#part and RMail::Message#each_part.
</p>
<pre>
first_part = message.part(0)
message.each_part { |part|
# do something with part
}
</pre>
<p>
See <a href="MIME_txt.html">guide/MIME.txt</a> for more tips on dealing
with MIME messages.
</p>
<h3>Manipulate the Message Headers</h3>
<p>
The RMail::Message#header method retrieves the <a
href="../../classes/RMail/Header.html">RMail::Header</a> object that every
message contains. You can then use <a
href="../../classes/RMail/Header.html">RMail::Header</a> methods to
manipulate the message’s header.
</p>
<p>
People often confuse a "message header" with "header
fields." In RubyMail a "header" always means the entire
header of the message, while "field" means an individual header
field (e.g. "Subject").
</p>
<p>
To append new fields, simply access assign to them like an array:
</p>
<pre>
message.header['To'] = 'bob@example.net'
message.header['From'] = 'sally@example.net'
</pre>
<p>
Note that the above will <em>always</em> append a new header, so you must
delete existing headers if you want only one.
</p>
<p>
To retrieve fields, you can access the header like an array. This will
return the field value.
</p>
<pre>
subject = message.header['Subject']
</pre>
<p>
Of course, a header may have several fields with the same name. To retrieve
all the values you can use <a
href="../../classes/RMail/Header.html">RMail::Header</a>.match.
</p>
<pre>
destinations = message.header.match(/^(to|cc|bcc)$/, nil)
</pre>
<p>
See the <a href="../../classes/RMail/Header.html">RMail::Header</a>
documentation for many other useful functions.
</p>
<h2>Dealing with Email Addresses</h2>
<p>
The address syntax for Internet email messages (as specified in RFC2822) is
complex. RubyMail contains the <a
href="../../classes/RMail/Address.html">RMail::Address</a> class that can
be used to parse and generate these addresses in a robust way. For example,
to retrieve all destination addresses from a <a
href="../../classes/RMail/Header.html">RMail::Header</a> object:
</p>
<pre>
recipients = RMail::Address.parse(header.match(/^(to|cc)/, nil))
</pre>
<p>
Then print out just the address portion:
</p>
<pre>
recipients.each { |r|
r.address
}
</pre>
<p>
When creating an address from scratch, you typically do this:
</p>
<pre>
address = RMail::Address.new("Bob Smith <bob@example.net>")
</pre>
<p>
Then to get the text form of the address form back:
</p>
<pre>
address.format
</pre>
<p>
<a href="../TODO.html">TODO</a>: addresses can be keys of a hash, sorted,
etc…
</p>
<h2>More Topics</h2>
<p>
This is just the beginning. See <a
href="TableOfContents_txt.html">guide/TableOfContents.txt</a> for a list of
other things covered in this guide.
</p>
</div>
</body>
</html>
|