694 lines
16 KiB
HTML
694 lines
16 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en" class="js csstransforms3d">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
|
|
<meta name="generator" content="Hugo 0.37.1" />
|
|
<meta name="description" content="Documentation for the Lettre mailer in Rust">
|
|
<meta name="author" content="Alexis Mousset">
|
|
|
|
<link rel="shortcut icon" href="https://lettre.github.io/lettre/images/favicon.png" type="image/x-icon" />
|
|
|
|
<title>Email creation :: Lettre site</title>
|
|
|
|
|
|
<link href="/lettre/css/nucleus.css?1522521488" rel="stylesheet">
|
|
<link href="/lettre/css/font-awesome.min.css?1522521488" rel="stylesheet">
|
|
<link href="/lettre/css/hybrid.css?1522521488" rel="stylesheet">
|
|
<link href="/lettre/css/featherlight.min.css?1522521488" rel="stylesheet">
|
|
<link href="/lettre/css/perfect-scrollbar.min.css?1522521488" rel="stylesheet">
|
|
<link href="/lettre/css/auto-complete.css?1522521488" rel="stylesheet">
|
|
<link href="/lettre/css/theme.css?1522521488" rel="stylesheet">
|
|
<link href="/lettre/css/hugo-theme.css?1522521488" rel="stylesheet">
|
|
|
|
|
|
<script src="/lettre/js/jquery-2.x.min.js?1522521488"></script>
|
|
|
|
<style type="text/css">
|
|
:root #header + #content > #left > #rlblock_left{
|
|
display:none !important;
|
|
}
|
|
|
|
</style>
|
|
|
|
</head>
|
|
<body class="" data-url="/lettre/creating-messages/email/">
|
|
<nav id="sidebar" class="">
|
|
|
|
|
|
|
|
<div id="header-wrapper">
|
|
<div id="header">
|
|
<a href="https://lettre.github.io/lettre/getting-started/intro/"><img src="https://lettre.github.io/lettre/images/logo50.png" /></a>
|
|
|
|
</div>
|
|
|
|
<div class="searchbox">
|
|
<label for="search-by"><i class="fa fa-search"></i></label>
|
|
<input data-search-input id="search-by" type="text" placeholder="Search...">
|
|
<span data-search-clear=""><i class="fa fa-close"></i></span>
|
|
</div>
|
|
|
|
<script type="text/javascript" src="/lettre/js/lunr.min.js?1522521488"></script>
|
|
<script type="text/javascript" src="/lettre/js/auto-complete.js?1522521488"></script>
|
|
<script type="text/javascript">
|
|
|
|
var baseurl = "https:\/\/lettre.github.io\/lettre";
|
|
|
|
</script>
|
|
<script type="text/javascript" src="/lettre/js/search.js?1522521488"></script>
|
|
|
|
|
|
</div>
|
|
|
|
<div class="highlightable">
|
|
<ul class="topics">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li data-nav-id="/lettre/getting-started/" title="Getting started" class="dd-item
|
|
|
|
|
|
|
|
">
|
|
<a href="/lettre/getting-started/">
|
|
Getting started
|
|
|
|
</a>
|
|
|
|
|
|
<ul>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li data-nav-id="/lettre/getting-started/intro/" title="Introduction" class="dd-item ">
|
|
<a href="/lettre/getting-started/intro/">
|
|
Introduction
|
|
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li data-nav-id="/lettre/creating-messages/" title="Creating messages" class="dd-item
|
|
parent
|
|
|
|
|
|
">
|
|
<a href="/lettre/creating-messages/">
|
|
Creating messages
|
|
|
|
</a>
|
|
|
|
|
|
<ul>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li data-nav-id="/lettre/creating-messages/email/" title="Email creation" class="dd-item active">
|
|
<a href="/lettre/creating-messages/email/">
|
|
Email creation
|
|
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li data-nav-id="/lettre/sending-messages/" title="Sending messages" class="dd-item
|
|
|
|
|
|
|
|
">
|
|
<a href="/lettre/sending-messages/">
|
|
Sending messages
|
|
|
|
</a>
|
|
|
|
|
|
<ul>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li data-nav-id="/lettre/sending-messages/intro/" title="Introduction" class="dd-item ">
|
|
<a href="/lettre/sending-messages/intro/">
|
|
Introduction
|
|
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li data-nav-id="/lettre/sending-messages/smtp/" title="SMTP transport" class="dd-item ">
|
|
<a href="/lettre/sending-messages/smtp/">
|
|
SMTP transport
|
|
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li data-nav-id="/lettre/sending-messages/sendmail/" title="Sendmail transport" class="dd-item ">
|
|
<a href="/lettre/sending-messages/sendmail/">
|
|
Sendmail transport
|
|
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li data-nav-id="/lettre/sending-messages/file/" title="File transport" class="dd-item ">
|
|
<a href="/lettre/sending-messages/file/">
|
|
File transport
|
|
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li data-nav-id="/lettre/sending-messages/stub/" title="Stub transport" class="dd-item ">
|
|
<a href="/lettre/sending-messages/stub/">
|
|
Stub transport
|
|
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
|
|
|
|
<section id="footer">
|
|
<p>Built with <a href="https://github.com/matcornic/hugo-theme-learn"><i class="fa fa-heart"></i></a> from <a href="http://getgrav.org">Grav</a> and <a href="http://gohugo.io/">Hugo</a></p>
|
|
|
|
</section>
|
|
</div>
|
|
</nav>
|
|
|
|
|
|
|
|
|
|
|
|
<section id="body">
|
|
<div id="overlay"></div>
|
|
<div class="padding highlightable sticky-parent">
|
|
|
|
<div class="sticky-spacer">
|
|
<div id="top-bar">
|
|
|
|
|
|
|
|
|
|
<div id="top-github-link">
|
|
<a class="github-link" href="https://github.com/lettre/lettre/edit/master/website/content/creating-messages/email.md" target="blank">
|
|
<i class="fa fa-code-fork"></i>
|
|
Edit this page
|
|
</a>
|
|
</div>
|
|
|
|
|
|
|
|
<div id="breadcrumbs" itemscope="" itemtype="http://data-vocabulary.org/Breadcrumb">
|
|
<span id="sidebar-toggle-span">
|
|
<a href="#" id="sidebar-toggle" data-sidebar-toggle="">
|
|
<i class="fa fa-bars"></i>
|
|
</a>
|
|
</span>
|
|
|
|
<span id="toc-menu"><i class="fa fa-list-alt"></i></span>
|
|
|
|
<span class="links">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<a href='/lettre/'>Lettre site</a> > <a href='/lettre/creating-messages/'>Creating messages</a> > Email creation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</span>
|
|
</div>
|
|
|
|
<div class="progress">
|
|
<div class="wrapper">
|
|
<nav id="TableOfContents">
|
|
<ul>
|
|
<li>
|
|
<ul>
|
|
<li>
|
|
<ul>
|
|
<li>
|
|
<ul>
|
|
<li><a href="#simple-example">Simple example</a></li>
|
|
<li><a href="#complete-example">Complete example</a></li>
|
|
</ul></li>
|
|
</ul></li>
|
|
</ul></li>
|
|
</ul>
|
|
</nav>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
</div>
|
|
</div>
|
|
|
|
|
|
|
|
<div id="body-inner">
|
|
|
|
<h1>Email creation</h1>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<h4 id="simple-example">Simple example</h4>
|
|
|
|
<p>The <code>email</code> part builds email messages. For now, it does not support attachments.
|
|
An email is built using an <code>EmailBuilder</code>. The simplest email could be:</p>
|
|
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-rust" data-lang="rust"><span style="color:#66d9ef">extern</span> <span style="color:#66d9ef">crate</span> lettre_email;
|
|
|
|
<span style="color:#66d9ef">use</span> lettre_email::EmailBuilder;
|
|
|
|
<span style="color:#66d9ef">fn</span> <span style="color:#a6e22e">main</span>() {
|
|
<span style="color:#75715e">// Create an email
|
|
</span><span style="color:#75715e"></span> <span style="color:#66d9ef">let</span> email <span style="color:#f92672">=</span> EmailBuilder::new()
|
|
<span style="color:#75715e">// Addresses can be specified by the tuple (email, alias)
|
|
</span><span style="color:#75715e"></span> .to((<span style="color:#e6db74">"user@example.org"</span>, <span style="color:#e6db74">"Firstname Lastname"</span>))
|
|
<span style="color:#75715e">// ... or by an address only
|
|
</span><span style="color:#75715e"></span> .from(<span style="color:#e6db74">"user@example.com"</span>)
|
|
.subject(<span style="color:#e6db74">"Hi, Hello world"</span>)
|
|
.text(<span style="color:#e6db74">"Hello world."</span>)
|
|
.build();
|
|
|
|
assert<span style="color:#f92672">!</span>(email.is_ok());
|
|
}</code></pre></div>
|
|
<p>When the <code>build</code> method is called, the <code>EmailBuilder</code> will add the missing headers (like
|
|
<code>Message-ID</code> or <code>Date</code>) and check for missing necessary ones (like <code>From</code> or <code>To</code>). It will
|
|
then generate an <code>Email</code> that can be sent.</p>
|
|
|
|
<p>The <code>text()</code> method will create a plain text email, while the <code>html()</code> method will create an
|
|
HTML email. You can use the <code>alternative()</code> method to provide both versions, using plain text
|
|
as fallback for the HTML version.</p>
|
|
|
|
<h4 id="complete-example">Complete example</h4>
|
|
|
|
<p>Below is a more complete example, not using method chaining:</p>
|
|
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-rust" data-lang="rust"><span style="color:#66d9ef">extern</span> <span style="color:#66d9ef">crate</span> lettre_email;
|
|
|
|
<span style="color:#66d9ef">use</span> lettre_email::EmailBuilder;
|
|
|
|
<span style="color:#66d9ef">fn</span> <span style="color:#a6e22e">main</span>() {
|
|
<span style="color:#66d9ef">let</span> <span style="color:#66d9ef">mut</span> builder <span style="color:#f92672">=</span> EmailBuilder::new();
|
|
builder.add_to((<span style="color:#e6db74">"user@example.org"</span>, <span style="color:#e6db74">"Alias name"</span>));
|
|
builder.add_cc((<span style="color:#e6db74">"user@example.net"</span>, <span style="color:#e6db74">"Alias name"</span>));
|
|
builder.add_from(<span style="color:#e6db74">"no-reply@example.com"</span>);
|
|
builder.add_from(<span style="color:#e6db74">"no-reply@example.eu"</span>);
|
|
builder.set_sender(<span style="color:#e6db74">"no-reply@example.com"</span>);
|
|
builder.set_subject(<span style="color:#e6db74">"Hello world"</span>);
|
|
builder.set_alternative(<span style="color:#e6db74">"<h2>Hi, Hello world.</h2>"</span>, <span style="color:#e6db74">"Hi, Hello world."</span>);
|
|
builder.add_reply_to(<span style="color:#e6db74">"contact@example.com"</span>);
|
|
builder.add_header((<span style="color:#e6db74">"X-Custom-Header"</span>, <span style="color:#e6db74">"my header"</span>));
|
|
|
|
<span style="color:#66d9ef">let</span> email <span style="color:#f92672">=</span> builder.build();
|
|
assert<span style="color:#f92672">!</span>(email.is_ok());
|
|
}</code></pre></div>
|
|
<p>See the <code>EmailBuilder</code> documentation for a complete list of methods.</p>
|
|
|
|
|
|
<footer class=" footline" >
|
|
|
|
</footer>
|
|
|
|
|
|
|
|
</div>
|
|
</div>
|
|
|
|
<div id="navigation">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<a class="nav nav-prev" href="/lettre/creating-messages/" title="Creating messages"> <i class="fa fa-chevron-left"></i></a>
|
|
|
|
|
|
<a class="nav nav-next" href="/lettre/sending-messages/" title="Sending messages" style="margin-right: 0px;"><i class="fa fa-chevron-right"></i></a>
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
<div style="left: -1000px; overflow: scroll; position: absolute; top: -1000px; border: none; box-sizing: content-box; height: 200px; margin: 0px; padding: 0px; width: 200px;">
|
|
<div style="border: none; box-sizing: content-box; height: 200px; margin: 0px; padding: 0px; width: 200px;"></div>
|
|
</div>
|
|
<script src="/lettre/js/clipboard.min.js?1522521488"></script>
|
|
<script src="/lettre/js/perfect-scrollbar.min.js?1522521488"></script>
|
|
<script src="/lettre/js/perfect-scrollbar.jquery.min.js?1522521488"></script>
|
|
<script src="/lettre/js/jquery.sticky-kit.min.js?1522521488"></script>
|
|
<script src="/lettre/js/featherlight.min.js?1522521488"></script>
|
|
<script src="/lettre/js/html5shiv-printshiv.min.js?1522521488"></script>
|
|
<script src="/lettre/js/highlight.pack.js?1522521488"></script>
|
|
<script>hljs.initHighlightingOnLoad();</script>
|
|
<script src="/lettre/js/modernizr.custom.71422.js?1522521488"></script>
|
|
<script src="/lettre/js/learn.js?1522521488"></script>
|
|
<script src="/lettre/js/hugo-learn.js?1522521488"></script>
|
|
|
|
<link href="/lettre/mermaid/mermaid.css?1522521488" type="text/css" rel="stylesheet" />
|
|
<script src="/lettre/mermaid/mermaid.js?1522521488"></script>
|
|
<script>
|
|
mermaid.initialize({ startOnLoad: true });
|
|
</script>
|
|
|
|
|
|
</body>
|
|
</html>
|
|
|