|
|
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="theme-color" content="#375EAB">
<title>mail - The Go Programming Language</title>
<link type="text/css" rel="stylesheet" href="../../../lib/godoc/style.css">
<link rel="stylesheet" href="../../../lib/godoc/jquery.treeview.css"> <script type="text/javascript">window.initFuncs = [];</script> </head> <body>
<div id='lowframe' style="position: fixed; bottom: 0; left: 0; height: 0; width: 100%; border-top: thin solid grey; background-color: white; overflow: auto;"> ... </div><!-- #lowframe -->
<div id="topbar" class="wide"><div class="container"> <div class="top-heading" id="heading-wide"><a href="http://localhost:6060/">The Go Programming Language</a></div> <div class="top-heading" id="heading-narrow"><a href="http://localhost:6060/">Go</a></div> <a href="index.html#" id="menu-button"><span id="menu-button-arrow">▽</span></a> <form method="GET" action="http://localhost:6060/search"> <div id="menu"> <a href="http://localhost:6060/doc/">Documents</a> <a href="http://localhost:6060/pkg/">Packages</a> <a href="http://localhost:6060/project/">The Project</a> <a href="http://localhost:6060/help/">Help</a> <a href="http://localhost:6060/blog/">Blog</a>
<input type="text" id="search" name="q" class="inactive" value="Search" placeholder="Search"> </div> </form>
</div></div>
<div id="page" class="wide"> <div class="container">
<h1>Package mail</h1>
<div id="nav"></div>
<!--
Copyright 2009 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. --> <!--
Note: Static (i.e., not template-generated) href and id attributes start with "pkg-" to make it impossible for them to conflict with generated attributes (some of which correspond to Go identifiers). -->
<script type='text/javascript'> document.ANALYSIS_DATA = null; document.CALLGRAPH = null; </script>
<div id="short-nav"> <dl> <dd><code>import "net/mail"</code></dd> </dl> <dl> <dd><a href="index.html#pkg-overview" class="overviewLink">Overview</a></dd> <dd><a href="index.html#pkg-index" class="indexLink">Index</a></dd> <dd><a href="index.html#pkg-examples" class="examplesLink">Examples</a></dd> </dl> </div> <!-- The package's Name is printed as title by the top-level template --> <div id="pkg-overview" class="toggleVisible"> <div class="collapsed"> <h2 class="toggleButton" title="Click to show Overview section">Overview ▹</h2> </div> <div class="expanded"> <h2 class="toggleButton" title="Click to hide Overview section">Overview ▾</h2> <p> Package mail implements parsing of mail messages. </p> <p> For the most part, this package follows the syntax as specified by RFC 5322. Notable divergences: </p> <pre>* Obsolete address formats are not parsed, including addresses with embedded route information. * Group addresses are not parsed. * The full range of spacing (the CFWS syntax element) is not supported, such as breaking addresses across lines. </pre>
</div> </div>
<div id="pkg-index" class="toggleVisible"> <div class="collapsed"> <h2 class="toggleButton" title="Click to show Index section">Index ▹</h2> </div> <div class="expanded"> <h2 class="toggleButton" title="Click to hide Index section">Index ▾</h2>
<!-- Table of contents for API; must be named manual-nav to turn off auto nav. --> <div id="manual-nav"> <dl> <dd><a href="index.html#pkg-variables">Variables</a></dd> <dd><a href="index.html#ParseAddressList">func ParseAddressList(list string) ([]*Address, error)</a></dd> <dd><a href="index.html#Address">type Address</a></dd> <dd> <a href="index.html#ParseAddress">func ParseAddress(address string) (*Address, error)</a></dd> <dd> <a href="index.html#Address.String">func (a *Address) String() string</a></dd> <dd><a href="index.html#AddressParser">type AddressParser</a></dd> <dd> <a href="index.html#AddressParser.Parse">func (p *AddressParser) Parse(address string) (*Address, error)</a></dd> <dd> <a href="index.html#AddressParser.ParseList">func (p *AddressParser) ParseList(list string) ([]*Address, error)</a></dd> <dd><a href="index.html#Header">type Header</a></dd> <dd> <a href="index.html#Header.AddressList">func (h Header) AddressList(key string) ([]*Address, error)</a></dd> <dd> <a href="index.html#Header.Date">func (h Header) Date() (time.Time, error)</a></dd> <dd> <a href="index.html#Header.Get">func (h Header) Get(key string) string</a></dd> <dd><a href="index.html#Message">type Message</a></dd> <dd> <a href="index.html#ReadMessage">func ReadMessage(r io.Reader) (msg *Message, err error)</a></dd> </dl> </div><!-- #manual-nav -->
<div id="pkg-examples"> <h4>Examples</h4> <dl> <dd><a class="exampleLink" href="index.html#example_ParseAddress">ParseAddress</a></dd> <dd><a class="exampleLink" href="index.html#example_ParseAddressList">ParseAddressList</a></dd> <dd><a class="exampleLink" href="index.html#example_ReadMessage">ReadMessage</a></dd> </dl> </div>
<h4>Package files</h4> <p> <span style="font-size:90%"> <a href="http://localhost:6060/src/net/mail/message.go">message.go</a> </span> </p> </div><!-- .expanded --> </div><!-- #pkg-index -->
<div id="pkg-callgraph" class="toggle" style="display: none"> <div class="collapsed"> <h2 class="toggleButton" title="Click to show Internal Call Graph section">Internal call graph ▹</h2> </div> <!-- .expanded --> <div class="expanded"> <h2 class="toggleButton" title="Click to hide Internal Call Graph section">Internal call graph ▾</h2> <p> In the call graph viewer below, each node is a function belonging to this package and its children are the functions it calls—perhaps dynamically. </p> <p> The root nodes are the entry points of the package: functions that may be called from outside the package. There may be non-exported or anonymous functions among them if they are called dynamically from another package. </p> <p> Click a node to visit that function's source code. From there you can visit its callers by clicking its declaring <code>func</code> token. </p> <p> Functions may be omitted if they were determined to be unreachable in the particular programs or tests that were analyzed. </p> <!-- Zero means show all package entry points. --> <ul style="margin-left: 0.5in" id="callgraph-0" class="treeview"></ul> </div> </div> <!-- #pkg-callgraph -->
<h2 id="pkg-variables">Variables</h2> <pre>var <span id="ErrHeaderNotPresent">ErrHeaderNotPresent</span> = <a href="../../errors/index.html">errors</a>.<a href="../../errors/index.html#New">New</a>("mail: header not in message")</pre> <h2 id="ParseAddressList">func <a href="http://localhost:6060/src/net/mail/message.go?s=3731:3785#L135">ParseAddressList</a></h2> <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> <p> ParseAddressList parses the given string as a list of addresses. </p>
<div id="example_ParseAddressList" class="toggle"> <div class="collapsed"> <p class="exampleHeading toggleButton">▹ <span class="text">Example</span></p> </div> <div class="expanded"> <p class="exampleHeading toggleButton">▾ <span class="text">Example</span></p> <p>Code:</p> <pre class="code">const list = "Alice <alice@example.com>, Bob <bob@example.com>, Eve <eve@example.com>" emails, err := mail.ParseAddressList(list) if err != nil { log.Fatal(err) }
for _, v := range emails { fmt.Println(v.Name, v.Address) }
<span class="comment"></pre> <p>Output:</p> <pre class="output">Alice alice@example.com Bob bob@example.com Eve eve@example.com </pre> </div> </div>
<h2 id="Address">type <a href="http://localhost:6060/src/net/mail/message.go?s=3382:3482#L124">Address</a></h2> <pre>type Address struct { Name <a href="../../builtin/index.html#string">string</a> <span class="comment">// Proper name; may be empty.</span> Address <a href="../../builtin/index.html#string">string</a> <span class="comment">// user@domain</span> }</pre> <p> Address represents a single mail address. An address such as "Barry Gibbs <bg@example.com>" is represented as Address{Name: "Barry Gibbs", Address: "bg@example.com"}. </p>
<h3 id="ParseAddress">func <a href="http://localhost:6060/src/net/mail/message.go?s=3557:3608#L130">ParseAddress</a></h3> <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> <p> Parses a single RFC 5322 address, e.g. "Barry Gibbs <bg@example.com>" </p>
<div id="example_ParseAddress" class="toggle"> <div class="collapsed"> <p class="exampleHeading toggleButton">▹ <span class="text">Example</span></p> </div> <div class="expanded"> <p class="exampleHeading toggleButton">▾ <span class="text">Example</span></p> <p>Code:</p> <pre class="code">e, err := mail.ParseAddress("Alice <alice@example.com>") if err != nil { log.Fatal(err) }
fmt.Println(e.Name, e.Address)
<span class="comment"></pre> <p>Output:</p> <pre class="output">Alice alice@example.com </pre> </div> </div>
<h3 id="Address.String">func (*Address) <a href="http://localhost:6060/src/net/mail/message.go?s=4723:4756#L160">String</a></h3> <pre>func (a *<a href="index.html#Address">Address</a>) String() <a href="../../builtin/index.html#string">string</a></pre> <p> String formats the address as a valid RFC 5322 address. If the address's name contains non-ASCII characters the name will be rendered according to RFC 2047. </p>
<h2 id="AddressParser">type <a href="http://localhost:6060/src/net/mail/message.go?s=3892:4027#L140">AddressParser</a></h2> <pre>type AddressParser struct { <span class="comment">// WordDecoder optionally specifies a decoder for RFC 2047 encoded-words.</span> WordDecoder *<a href="../../mime/index.html">mime</a>.<a href="../../mime/index.html#WordDecoder">WordDecoder</a> }</pre> <p> An AddressParser is an RFC 5322 address parser. </p>
<h3 id="AddressParser.Parse">func (*AddressParser) <a href="http://localhost:6060/src/net/mail/message.go?s=4136:4199#L147">Parse</a></h3> <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> <p> Parse parses a single RFC 5322 address of the form "Gogh Fir <gf@example.com>" or "foo@example.com". </p>
<h3 id="AddressParser.ParseList">func (*AddressParser) <a href="http://localhost:6060/src/net/mail/message.go?s=4415:4481#L153">ParseList</a></h3> <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> <p> ParseList parses the given string as a list of comma-separated addresses of the form "Gogh Fir <gf@example.com>" or "foo@example.com". </p>
<h2 id="Header">type <a href="http://localhost:6060/src/net/mail/message.go?s=2466:2497#L93">Header</a></h2> <pre>type Header map[<a href="../../builtin/index.html#string">string</a>][]<a href="../../builtin/index.html#string">string</a></pre> <p> A Header represents the key-value pairs in a mail message header. </p>
<h3 id="Header.AddressList">func (Header) <a href="http://localhost:6060/src/net/mail/message.go?s=3039:3098#L113">AddressList</a></h3> <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> <p> AddressList parses the named header field as a list of addresses. </p>
<h3 id="Header.Date">func (Header) <a href="http://localhost:6060/src/net/mail/message.go?s=2817:2858#L104">Date</a></h3> <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> <p> Date parses the Date header field. </p>
<h3 id="Header.Get">func (Header) <a href="http://localhost:6060/src/net/mail/message.go?s=2625:2663#L97">Get</a></h3> <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> <p> Get gets the first value associated with the given key. If there are no values associated with the key, Get returns "". </p>
<h2 id="Message">type <a href="http://localhost:6060/src/net/mail/message.go?s=882:938#L32">Message</a></h2> <pre>type Message struct { Header <a href="index.html#Header">Header</a> Body <a href="../../io/index.html">io</a>.<a href="../../io/index.html#Reader">Reader</a> }</pre> <p> A Message represents a parsed mail message. </p>
<h3 id="ReadMessage">func <a href="http://localhost:6060/src/net/mail/message.go?s=1075:1130#L40">ReadMessage</a></h3> <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> <p> ReadMessage reads a message from r. The headers are parsed, and the body of the message will be available for reading from r. </p>
<div id="example_ReadMessage" class="toggle"> <div class="collapsed"> <p class="exampleHeading toggleButton">▹ <span class="text">Example</span></p> </div> <div class="expanded"> <p class="exampleHeading toggleButton">▾ <span class="text">Example</span></p> <p>Code:</p> <pre class="code">msg := `Date: Mon, 23 Jun 2015 11:40:36 -0400 From: Gopher <from@example.com> To: Another Gopher <to@example.com> Subject: Gophers at Gophercon
Message body `
r := strings.NewReader(msg) m, err := mail.ReadMessage(r) if err != nil { log.Fatal(err) }
header := m.Header fmt.Println("Date:", header.Get("Date")) fmt.Println("From:", header.Get("From")) fmt.Println("To:", header.Get("To")) fmt.Println("Subject:", header.Get("Subject"))
body, err := ioutil.ReadAll(m.Body) if err != nil { log.Fatal(err) } fmt.Printf("%s", body)
<span class="comment"></pre> <p>Output:</p> <pre class="output">Date: Mon, 23 Jun 2015 11:40:36 -0400 From: Gopher <from@example.com> To: Another Gopher <to@example.com> Subject: Gophers at Gophercon Message body </pre> </div> </div>
<div id="footer"> Build version go1.6.<br> Except as <a href="https://developers.google.com/site-policies#restrictions">noted</a>, the content of this page is licensed under the Creative Commons Attribution 3.0 License, and code is licensed under a <a href="http://localhost:6060/LICENSE">BSD license</a>.<br> <a href="http://localhost:6060/doc/tos.html">Terms of Service</a> | <a href="http://www.google.com/intl/en/policies/privacy/">Privacy Policy</a> </div>
</div><!-- .container --> </div><!-- #page -->
<!-- TODO(adonovan): load these from <head> using "defer" attribute? --> <script type="text/javascript" src="../../../lib/godoc/jquery.js"></script> <script type="text/javascript" src="../../../lib/godoc/jquery.treeview.js"></script> <script type="text/javascript" src="../../../lib/godoc/jquery.treeview.edit.js"></script>
<script type="text/javascript" src="../../../lib/godoc/godocs.js"></script>
</body> </html>
|