You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

591 lines
17 KiB

8 years ago
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <meta name="theme-color" content="#375EAB">
  7. <title>mail - The Go Programming Language</title>
  8. <link type="text/css" rel="stylesheet" href="../../../lib/godoc/style.css">
  9. <link rel="stylesheet" href="../../../lib/godoc/jquery.treeview.css">
  10. <script type="text/javascript">window.initFuncs = [];</script>
  11. </head>
  12. <body>
  13. <div id='lowframe' style="position: fixed; bottom: 0; left: 0; height: 0; width: 100%; border-top: thin solid grey; background-color: white; overflow: auto;">
  14. ...
  15. </div><!-- #lowframe -->
  16. <div id="topbar" class="wide"><div class="container">
  17. <div class="top-heading" id="heading-wide"><a href="http://localhost:6060/">The Go Programming Language</a></div>
  18. <div class="top-heading" id="heading-narrow"><a href="http://localhost:6060/">Go</a></div>
  19. <a href="index.html#" id="menu-button"><span id="menu-button-arrow">&#9661;</span></a>
  20. <form method="GET" action="http://localhost:6060/search">
  21. <div id="menu">
  22. <a href="http://localhost:6060/doc/">Documents</a>
  23. <a href="http://localhost:6060/pkg/">Packages</a>
  24. <a href="http://localhost:6060/project/">The Project</a>
  25. <a href="http://localhost:6060/help/">Help</a>
  26. <a href="http://localhost:6060/blog/">Blog</a>
  27. <input type="text" id="search" name="q" class="inactive" value="Search" placeholder="Search">
  28. </div>
  29. </form>
  30. </div></div>
  31. <div id="page" class="wide">
  32. <div class="container">
  33. <h1>Package mail</h1>
  34. <div id="nav"></div>
  35. <!--
  36. Copyright 2009 The Go Authors. All rights reserved.
  37. Use of this source code is governed by a BSD-style
  38. license that can be found in the LICENSE file.
  39. -->
  40. <!--
  41. Note: Static (i.e., not template-generated) href and id
  42. attributes start with "pkg-" to make it impossible for
  43. them to conflict with generated attributes (some of which
  44. correspond to Go identifiers).
  45. -->
  46. <script type='text/javascript'>
  47. document.ANALYSIS_DATA = null;
  48. document.CALLGRAPH = null;
  49. </script>
  50. <div id="short-nav">
  51. <dl>
  52. <dd><code>import "net/mail"</code></dd>
  53. </dl>
  54. <dl>
  55. <dd><a href="index.html#pkg-overview" class="overviewLink">Overview</a></dd>
  56. <dd><a href="index.html#pkg-index" class="indexLink">Index</a></dd>
  57. <dd><a href="index.html#pkg-examples" class="examplesLink">Examples</a></dd>
  58. </dl>
  59. </div>
  60. <!-- The package's Name is printed as title by the top-level template -->
  61. <div id="pkg-overview" class="toggleVisible">
  62. <div class="collapsed">
  63. <h2 class="toggleButton" title="Click to show Overview section">Overview ▹</h2>
  64. </div>
  65. <div class="expanded">
  66. <h2 class="toggleButton" title="Click to hide Overview section">Overview ▾</h2>
  67. <p>
  68. Package mail implements parsing of mail messages.
  69. </p>
  70. <p>
  71. For the most part, this package follows the syntax as specified by RFC 5322.
  72. Notable divergences:
  73. </p>
  74. <pre>* Obsolete address formats are not parsed, including addresses with
  75. embedded route information.
  76. * Group addresses are not parsed.
  77. * The full range of spacing (the CFWS syntax element) is not supported,
  78. such as breaking addresses across lines.
  79. </pre>
  80. </div>
  81. </div>
  82. <div id="pkg-index" class="toggleVisible">
  83. <div class="collapsed">
  84. <h2 class="toggleButton" title="Click to show Index section">Index ▹</h2>
  85. </div>
  86. <div class="expanded">
  87. <h2 class="toggleButton" title="Click to hide Index section">Index ▾</h2>
  88. <!-- Table of contents for API; must be named manual-nav to turn off auto nav. -->
  89. <div id="manual-nav">
  90. <dl>
  91. <dd><a href="index.html#pkg-variables">Variables</a></dd>
  92. <dd><a href="index.html#ParseAddressList">func ParseAddressList(list string) ([]*Address, error)</a></dd>
  93. <dd><a href="index.html#Address">type Address</a></dd>
  94. <dd>&nbsp; &nbsp; <a href="index.html#ParseAddress">func ParseAddress(address string) (*Address, error)</a></dd>
  95. <dd>&nbsp; &nbsp; <a href="index.html#Address.String">func (a *Address) String() string</a></dd>
  96. <dd><a href="index.html#AddressParser">type AddressParser</a></dd>
  97. <dd>&nbsp; &nbsp; <a href="index.html#AddressParser.Parse">func (p *AddressParser) Parse(address string) (*Address, error)</a></dd>
  98. <dd>&nbsp; &nbsp; <a href="index.html#AddressParser.ParseList">func (p *AddressParser) ParseList(list string) ([]*Address, error)</a></dd>
  99. <dd><a href="index.html#Header">type Header</a></dd>
  100. <dd>&nbsp; &nbsp; <a href="index.html#Header.AddressList">func (h Header) AddressList(key string) ([]*Address, error)</a></dd>
  101. <dd>&nbsp; &nbsp; <a href="index.html#Header.Date">func (h Header) Date() (time.Time, error)</a></dd>
  102. <dd>&nbsp; &nbsp; <a href="index.html#Header.Get">func (h Header) Get(key string) string</a></dd>
  103. <dd><a href="index.html#Message">type Message</a></dd>
  104. <dd>&nbsp; &nbsp; <a href="index.html#ReadMessage">func ReadMessage(r io.Reader) (msg *Message, err error)</a></dd>
  105. </dl>
  106. </div><!-- #manual-nav -->
  107. <div id="pkg-examples">
  108. <h4>Examples</h4>
  109. <dl>
  110. <dd><a class="exampleLink" href="index.html#example_ParseAddress">ParseAddress</a></dd>
  111. <dd><a class="exampleLink" href="index.html#example_ParseAddressList">ParseAddressList</a></dd>
  112. <dd><a class="exampleLink" href="index.html#example_ReadMessage">ReadMessage</a></dd>
  113. </dl>
  114. </div>
  115. <h4>Package files</h4>
  116. <p>
  117. <span style="font-size:90%">
  118. <a href="http://localhost:6060/src/net/mail/message.go">message.go</a>
  119. </span>
  120. </p>
  121. </div><!-- .expanded -->
  122. </div><!-- #pkg-index -->
  123. <div id="pkg-callgraph" class="toggle" style="display: none">
  124. <div class="collapsed">
  125. <h2 class="toggleButton" title="Click to show Internal Call Graph section">Internal call graph ▹</h2>
  126. </div> <!-- .expanded -->
  127. <div class="expanded">
  128. <h2 class="toggleButton" title="Click to hide Internal Call Graph section">Internal call graph ▾</h2>
  129. <p>
  130. In the call graph viewer below, each node
  131. is a function belonging to this package
  132. and its children are the functions it
  133. calls&mdash;perhaps dynamically.
  134. </p>
  135. <p>
  136. The root nodes are the entry points of the
  137. package: functions that may be called from
  138. outside the package.
  139. There may be non-exported or anonymous
  140. functions among them if they are called
  141. dynamically from another package.
  142. </p>
  143. <p>
  144. Click a node to visit that function's source code.
  145. From there you can visit its callers by
  146. clicking its declaring <code>func</code>
  147. token.
  148. </p>
  149. <p>
  150. Functions may be omitted if they were
  151. determined to be unreachable in the
  152. particular programs or tests that were
  153. analyzed.
  154. </p>
  155. <!-- Zero means show all package entry points. -->
  156. <ul style="margin-left: 0.5in" id="callgraph-0" class="treeview"></ul>
  157. </div>
  158. </div> <!-- #pkg-callgraph -->
  159. <h2 id="pkg-variables">Variables</h2>
  160. <pre>var <span id="ErrHeaderNotPresent">ErrHeaderNotPresent</span> = <a href="../../errors/index.html">errors</a>.<a href="../../errors/index.html#New">New</a>(&#34;mail: header not in message&#34;)</pre>
  161. <h2 id="ParseAddressList">func <a href="http://localhost:6060/src/net/mail/message.go?s=3731:3785#L135">ParseAddressList</a></h2>
  162. <pre>func ParseAddressList(list <a href="../../builtin/index.html#string">string</a>) ([]*<a href="index.html#Address">Address</a>, <a href="../../builtin/index.html#error">error</a>)</pre>
  163. <p>
  164. ParseAddressList parses the given string as a list of addresses.
  165. </p>
  166. <div id="example_ParseAddressList" class="toggle">
  167. <div class="collapsed">
  168. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  169. </div>
  170. <div class="expanded">
  171. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  172. <p>Code:</p>
  173. <pre class="code">const list = &#34;Alice &lt;alice@example.com&gt;, Bob &lt;bob@example.com&gt;, Eve &lt;eve@example.com&gt;&#34;
  174. emails, err := mail.ParseAddressList(list)
  175. if err != nil {
  176. log.Fatal(err)
  177. }
  178. for _, v := range emails {
  179. fmt.Println(v.Name, v.Address)
  180. }
  181. <span class="comment"></pre>
  182. <p>Output:</p>
  183. <pre class="output">Alice alice@example.com
  184. Bob bob@example.com
  185. Eve eve@example.com
  186. </pre>
  187. </div>
  188. </div>
  189. <h2 id="Address">type <a href="http://localhost:6060/src/net/mail/message.go?s=3382:3482#L124">Address</a></h2>
  190. <pre>type Address struct {
  191. Name <a href="../../builtin/index.html#string">string</a> <span class="comment">// Proper name; may be empty.</span>
  192. Address <a href="../../builtin/index.html#string">string</a> <span class="comment">// user@domain</span>
  193. }</pre>
  194. <p>
  195. Address represents a single mail address.
  196. An address such as &#34;Barry Gibbs &lt;bg@example.com&gt;&#34; is represented
  197. as Address{Name: &#34;Barry Gibbs&#34;, Address: &#34;bg@example.com&#34;}.
  198. </p>
  199. <h3 id="ParseAddress">func <a href="http://localhost:6060/src/net/mail/message.go?s=3557:3608#L130">ParseAddress</a></h3>
  200. <pre>func ParseAddress(address <a href="../../builtin/index.html#string">string</a>) (*<a href="index.html#Address">Address</a>, <a href="../../builtin/index.html#error">error</a>)</pre>
  201. <p>
  202. Parses a single RFC 5322 address, e.g. &#34;Barry Gibbs &lt;bg@example.com&gt;&#34;
  203. </p>
  204. <div id="example_ParseAddress" class="toggle">
  205. <div class="collapsed">
  206. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  207. </div>
  208. <div class="expanded">
  209. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  210. <p>Code:</p>
  211. <pre class="code">e, err := mail.ParseAddress(&#34;Alice &lt;alice@example.com&gt;&#34;)
  212. if err != nil {
  213. log.Fatal(err)
  214. }
  215. fmt.Println(e.Name, e.Address)
  216. <span class="comment"></pre>
  217. <p>Output:</p>
  218. <pre class="output">Alice alice@example.com
  219. </pre>
  220. </div>
  221. </div>
  222. <h3 id="Address.String">func (*Address) <a href="http://localhost:6060/src/net/mail/message.go?s=4723:4756#L160">String</a></h3>
  223. <pre>func (a *<a href="index.html#Address">Address</a>) String() <a href="../../builtin/index.html#string">string</a></pre>
  224. <p>
  225. String formats the address as a valid RFC 5322 address.
  226. If the address&#39;s name contains non-ASCII characters
  227. the name will be rendered according to RFC 2047.
  228. </p>
  229. <h2 id="AddressParser">type <a href="http://localhost:6060/src/net/mail/message.go?s=3892:4027#L140">AddressParser</a></h2>
  230. <pre>type AddressParser struct {
  231. <span class="comment">// WordDecoder optionally specifies a decoder for RFC 2047 encoded-words.</span>
  232. WordDecoder *<a href="../../mime/index.html">mime</a>.<a href="../../mime/index.html#WordDecoder">WordDecoder</a>
  233. }</pre>
  234. <p>
  235. An AddressParser is an RFC 5322 address parser.
  236. </p>
  237. <h3 id="AddressParser.Parse">func (*AddressParser) <a href="http://localhost:6060/src/net/mail/message.go?s=4136:4199#L147">Parse</a></h3>
  238. <pre>func (p *<a href="index.html#AddressParser">AddressParser</a>) Parse(address <a href="../../builtin/index.html#string">string</a>) (*<a href="index.html#Address">Address</a>, <a href="../../builtin/index.html#error">error</a>)</pre>
  239. <p>
  240. Parse parses a single RFC 5322 address of the
  241. form &#34;Gogh Fir &lt;gf@example.com&gt;&#34; or &#34;foo@example.com&#34;.
  242. </p>
  243. <h3 id="AddressParser.ParseList">func (*AddressParser) <a href="http://localhost:6060/src/net/mail/message.go?s=4415:4481#L153">ParseList</a></h3>
  244. <pre>func (p *<a href="index.html#AddressParser">AddressParser</a>) ParseList(list <a href="../../builtin/index.html#string">string</a>) ([]*<a href="index.html#Address">Address</a>, <a href="../../builtin/index.html#error">error</a>)</pre>
  245. <p>
  246. ParseList parses the given string as a list of comma-separated addresses
  247. of the form &#34;Gogh Fir &lt;gf@example.com&gt;&#34; or &#34;foo@example.com&#34;.
  248. </p>
  249. <h2 id="Header">type <a href="http://localhost:6060/src/net/mail/message.go?s=2466:2497#L93">Header</a></h2>
  250. <pre>type Header map[<a href="../../builtin/index.html#string">string</a>][]<a href="../../builtin/index.html#string">string</a></pre>
  251. <p>
  252. A Header represents the key-value pairs in a mail message header.
  253. </p>
  254. <h3 id="Header.AddressList">func (Header) <a href="http://localhost:6060/src/net/mail/message.go?s=3039:3098#L113">AddressList</a></h3>
  255. <pre>func (h <a href="index.html#Header">Header</a>) AddressList(key <a href="../../builtin/index.html#string">string</a>) ([]*<a href="index.html#Address">Address</a>, <a href="../../builtin/index.html#error">error</a>)</pre>
  256. <p>
  257. AddressList parses the named header field as a list of addresses.
  258. </p>
  259. <h3 id="Header.Date">func (Header) <a href="http://localhost:6060/src/net/mail/message.go?s=2817:2858#L104">Date</a></h3>
  260. <pre>func (h <a href="index.html#Header">Header</a>) Date() (<a href="../../time/index.html">time</a>.<a href="../../time/index.html#Time">Time</a>, <a href="../../builtin/index.html#error">error</a>)</pre>
  261. <p>
  262. Date parses the Date header field.
  263. </p>
  264. <h3 id="Header.Get">func (Header) <a href="http://localhost:6060/src/net/mail/message.go?s=2625:2663#L97">Get</a></h3>
  265. <pre>func (h <a href="index.html#Header">Header</a>) Get(key <a href="../../builtin/index.html#string">string</a>) <a href="../../builtin/index.html#string">string</a></pre>
  266. <p>
  267. Get gets the first value associated with the given key.
  268. If there are no values associated with the key, Get returns &#34;&#34;.
  269. </p>
  270. <h2 id="Message">type <a href="http://localhost:6060/src/net/mail/message.go?s=882:938#L32">Message</a></h2>
  271. <pre>type Message struct {
  272. Header <a href="index.html#Header">Header</a>
  273. Body <a href="../../io/index.html">io</a>.<a href="../../io/index.html#Reader">Reader</a>
  274. }</pre>
  275. <p>
  276. A Message represents a parsed mail message.
  277. </p>
  278. <h3 id="ReadMessage">func <a href="http://localhost:6060/src/net/mail/message.go?s=1075:1130#L40">ReadMessage</a></h3>
  279. <pre>func ReadMessage(r <a href="../../io/index.html">io</a>.<a href="../../io/index.html#Reader">Reader</a>) (msg *<a href="index.html#Message">Message</a>, err <a href="../../builtin/index.html#error">error</a>)</pre>
  280. <p>
  281. ReadMessage reads a message from r.
  282. The headers are parsed, and the body of the message will be available
  283. for reading from r.
  284. </p>
  285. <div id="example_ReadMessage" class="toggle">
  286. <div class="collapsed">
  287. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  288. </div>
  289. <div class="expanded">
  290. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  291. <p>Code:</p>
  292. <pre class="code">msg := `Date: Mon, 23 Jun 2015 11:40:36 -0400
  293. From: Gopher &lt;from@example.com&gt;
  294. To: Another Gopher &lt;to@example.com&gt;
  295. Subject: Gophers at Gophercon
  296. Message body
  297. `
  298. r := strings.NewReader(msg)
  299. m, err := mail.ReadMessage(r)
  300. if err != nil {
  301. log.Fatal(err)
  302. }
  303. header := m.Header
  304. fmt.Println(&#34;Date:&#34;, header.Get(&#34;Date&#34;))
  305. fmt.Println(&#34;From:&#34;, header.Get(&#34;From&#34;))
  306. fmt.Println(&#34;To:&#34;, header.Get(&#34;To&#34;))
  307. fmt.Println(&#34;Subject:&#34;, header.Get(&#34;Subject&#34;))
  308. body, err := ioutil.ReadAll(m.Body)
  309. if err != nil {
  310. log.Fatal(err)
  311. }
  312. fmt.Printf(&#34;%s&#34;, body)
  313. <span class="comment"></pre>
  314. <p>Output:</p>
  315. <pre class="output">Date: Mon, 23 Jun 2015 11:40:36 -0400
  316. From: Gopher &lt;from@example.com&gt;
  317. To: Another Gopher &lt;to@example.com&gt;
  318. Subject: Gophers at Gophercon
  319. Message body
  320. </pre>
  321. </div>
  322. </div>
  323. <div id="footer">
  324. Build version go1.6.<br>
  325. Except as <a href="https://developers.google.com/site-policies#restrictions">noted</a>,
  326. the content of this page is licensed under the
  327. Creative Commons Attribution 3.0 License,
  328. and code is licensed under a <a href="http://localhost:6060/LICENSE">BSD license</a>.<br>
  329. <a href="http://localhost:6060/doc/tos.html">Terms of Service</a> |
  330. <a href="http://www.google.com/intl/en/policies/privacy/">Privacy Policy</a>
  331. </div>
  332. </div><!-- .container -->
  333. </div><!-- #page -->
  334. <!-- TODO(adonovan): load these from <head> using "defer" attribute? -->
  335. <script type="text/javascript" src="../../../lib/godoc/jquery.js"></script>
  336. <script type="text/javascript" src="../../../lib/godoc/jquery.treeview.js"></script>
  337. <script type="text/javascript" src="../../../lib/godoc/jquery.treeview.edit.js"></script>
  338. <script type="text/javascript" src="../../../lib/godoc/godocs.js"></script>
  339. </body>
  340. </html>