Files
lettre/docs/sending-messages/smtp/index.html
Alexis Mousset a86cc3328e Fix site base url
2018-04-01 19:28:54 +02:00

749 lines
17 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="http://docs.lettre.at//images/favicon.png" type="image/x-icon" />
<title>SMTP transport :: Lettre site</title>
<link href="/css/nucleus.css?1522603726" rel="stylesheet">
<link href="/css/font-awesome.min.css?1522603726" rel="stylesheet">
<link href="/css/hybrid.css?1522603726" rel="stylesheet">
<link href="/css/featherlight.min.css?1522603726" rel="stylesheet">
<link href="/css/perfect-scrollbar.min.css?1522603726" rel="stylesheet">
<link href="/css/auto-complete.css?1522603726" rel="stylesheet">
<link href="/css/theme.css?1522603726" rel="stylesheet">
<link href="/css/hugo-theme.css?1522603726" rel="stylesheet">
<script src="/js/jquery-2.x.min.js?1522603726"></script>
<style type="text/css">
:root #header + #content > #left > #rlblock_left{
display:none !important;
}
</style>
</head>
<body class="" data-url="/sending-messages/smtp/">
<nav id="sidebar" class="">
<div id="header-wrapper">
<div id="header">
<a href="http://docs.lettre.at//getting-started/intro/"><img src="http://docs.lettre.at//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="/js/lunr.min.js?1522603726"></script>
<script type="text/javascript" src="/js/auto-complete.js?1522603726"></script>
<script type="text/javascript">
var baseurl = "http:\/\/docs.lettre.at\/";
</script>
<script type="text/javascript" src="/js/search.js?1522603726"></script>
</div>
<div class="highlightable">
<ul class="topics">
<li data-nav-id="/getting-started/" title="Getting started" class="dd-item
">
<a href="/getting-started/">
Getting started
</a>
<ul>
<li data-nav-id="/getting-started/intro/" title="Introduction" class="dd-item ">
<a href="/getting-started/intro/">
Introduction
</a>
</li>
</ul>
</li>
<li data-nav-id="/creating-messages/" title="Creating messages" class="dd-item
">
<a href="/creating-messages/">
Creating messages
</a>
<ul>
<li data-nav-id="/creating-messages/email/" title="Email creation" class="dd-item ">
<a href="/creating-messages/email/">
Email creation
</a>
</li>
</ul>
</li>
<li data-nav-id="/sending-messages/" title="Sending messages" class="dd-item
parent
">
<a href="/sending-messages/">
Sending messages
</a>
<ul>
<li data-nav-id="/sending-messages/intro/" title="Introduction" class="dd-item ">
<a href="/sending-messages/intro/">
Introduction
</a>
</li>
<li data-nav-id="/sending-messages/smtp/" title="SMTP transport" class="dd-item active">
<a href="/sending-messages/smtp/">
SMTP transport
</a>
</li>
<li data-nav-id="/sending-messages/sendmail/" title="Sendmail transport" class="dd-item ">
<a href="/sending-messages/sendmail/">
Sendmail transport
</a>
</li>
<li data-nav-id="/sending-messages/file/" title="File transport" class="dd-item ">
<a href="/sending-messages/file/">
File transport
</a>
</li>
<li data-nav-id="/sending-messages/stub/" title="Stub transport" class="dd-item ">
<a href="/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/sending-messages/smtp.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 site</a> > <a href='/sending-messages/'>Sending messages</a> > SMTP transport
</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>
<li><a href="#lower-level">Lower level</a></li>
</ul></li>
</ul></li>
</ul></li>
</ul>
</nav>
</div>
</div>
</div>
</div>
<div id="body-inner">
<h1>SMTP transport</h1>
<p>This transport uses the SMTP protocol to send emails over the network (locally or remotely).</p>
<p>It is designed to be:</p>
<ul>
<li>Secured: email are encrypted by default</li>
<li>Modern: Unicode support for email content and sender/recipient addresses when compatible</li>
<li>Fast: supports tcp connection reuse</li>
</ul>
<p>This client is designed to send emails to a relay server, and should <em>not</em> be used to send
emails directly to the destination.</p>
<p>The relay server can be the local email server, a specific host or a third-party service.</p>
<h4 id="simple-example">Simple example</h4>
<p>This is the most basic example of usage:</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,no_run" data-lang="rust,no_run">extern crate lettre;
use lettre::{SimpleSendableEmail, EmailTransport, SmtpTransport};
fn main() {
let email = SimpleSendableEmail::new(
&#34;user@localhost&#34;.to_string(),
&amp;[&#34;root@localhost&#34;.to_string()],
&#34;message_id&#34;.to_string(),
&#34;Hello world&#34;.to_string(),
).unwrap();
// Open a local connection on port 25
let mut mailer =
SmtpTransport::builder_unencrypted_localhost().unwrap().build();
// Send the email
let result = mailer.send(&amp;email);
assert!(result.is_ok());
}</code></pre></div>
<h4 id="complete-example">Complete example</h4>
<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,no_run" data-lang="rust,no_run">extern crate lettre;
use lettre::smtp::authentication::{Credentials, Mechanism};
use lettre::{SimpleSendableEmail, EmailTransport, SmtpTransport};
use lettre::smtp::extension::ClientId;
use lettre::smtp::ConnectionReuseParameters;
fn main() {
let email = SimpleSendableEmail::new(
&#34;user@localhost&#34;.to_string(),
&amp;[&#34;root@localhost&#34;.to_string()],
&#34;message_id&#34;.to_string(),
&#34;Hello world&#34;.to_string(),
).unwrap();
// Connect to a remote server on a custom port
let mut mailer = SmtpTransport::simple_builder(&#34;server.tld&#34;).unwrap()
// Set the name sent during EHLO/HELO, default is `localhost`
.hello_name(ClientId::Domain(&#34;my.hostname.tld&#34;.to_string()))
// Add credentials for authentication
.credentials(Credentials::new(&#34;username&#34;.to_string(), &#34;password&#34;.to_string()))
// Enable SMTPUTF8 if the server supports it
.smtp_utf8(true)
// Configure expected authentication mechanism
.authentication_mechanism(Mechanism::Plain)
// Enable connection reuse
.connection_reuse(ConnectionReuseParameters::ReuseUnlimited).build();
let result_1 = mailer.send(&amp;email);
assert!(result_1.is_ok());
// The second email will use the same connection
let result_2 = mailer.send(&amp;email);
assert!(result_2.is_ok());
// Explicitly close the SMTP transaction as we enabled connection reuse
mailer.close();
}</code></pre></div>
<h4 id="lower-level">Lower level</h4>
<p>You can also send commands, here is a simple email transaction without
error handling:</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,no_run" data-lang="rust,no_run">extern crate lettre;
use lettre::EmailAddress;
use lettre::smtp::SMTP_PORT;
use lettre::smtp::client::Client;
use lettre::smtp::client::net::NetworkStream;
use lettre::smtp::extension::ClientId;
use lettre::smtp::commands::*;
fn main() {
let mut email_client: Client&lt;NetworkStream&gt; = Client::new();
let _ = email_client.connect(&amp;(&#34;localhost&#34;, SMTP_PORT), None);
let _ = email_client.command(EhloCommand::new(ClientId::new(&#34;my_hostname&#34;.to_string())));
let _ = email_client.command(
MailCommand::new(Some(EmailAddress::new(&#34;user@example.com&#34;.to_string()).unwrap()), vec![])
);
let _ = email_client.command(
RcptCommand::new(EmailAddress::new(&#34;user@example.org&#34;.to_string()).unwrap(), vec![])
);
let _ = email_client.command(DataCommand);
let _ = email_client.message(Box::new(&#34;Test email&#34;.as_bytes()));
let _ = email_client.command(QuitCommand);
}</code></pre></div>
<footer class=" footline" >
</footer>
</div>
</div>
<div id="navigation">
<a class="nav nav-prev" href="/sending-messages/intro/" title="Introduction"> <i class="fa fa-chevron-left"></i></a>
<a class="nav nav-next" href="/sending-messages/sendmail/" title="Sendmail transport" 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="/js/clipboard.min.js?1522603726"></script>
<script src="/js/perfect-scrollbar.min.js?1522603726"></script>
<script src="/js/perfect-scrollbar.jquery.min.js?1522603726"></script>
<script src="/js/jquery.sticky-kit.min.js?1522603726"></script>
<script src="/js/featherlight.min.js?1522603726"></script>
<script src="/js/html5shiv-printshiv.min.js?1522603726"></script>
<script src="/js/highlight.pack.js?1522603726"></script>
<script>hljs.initHighlightingOnLoad();</script>
<script src="/js/modernizr.custom.71422.js?1522603726"></script>
<script src="/js/learn.js?1522603726"></script>
<script src="/js/hugo-learn.js?1522603726"></script>
<link href="/mermaid/mermaid.css?1522603726" type="text/css" rel="stylesheet" />
<script src="/mermaid/mermaid.js?1522603726"></script>
<script>
mermaid.initialize({ startOnLoad: true });
</script>
</body>
</html>