492 lines
13 KiB
HTML
492 lines
13 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.20.7" />
|
|
<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>SMTP transport</title>
|
|
<link href="https://lettre.github.io/lettre//css/nucleus.css" rel="stylesheet">
|
|
<link href="https://lettre.github.io/lettre//css/font-awesome.min.css" rel="stylesheet">
|
|
<link href="https://lettre.github.io/lettre//css/hybrid.css" rel="stylesheet">
|
|
<link href="https://lettre.github.io/lettre//css/featherlight.min.css" rel="stylesheet">
|
|
<link href="https://lettre.github.io/lettre//css/perfect-scrollbar.min.css" rel="stylesheet">
|
|
<link href="https://lettre.github.io/lettre//css/horsey.css" rel="stylesheet">
|
|
<link href="https://lettre.github.io/lettre//css/theme.css" rel="stylesheet">
|
|
<link href="https://lettre.github.io/lettre//css/hugo-theme.css" rel="stylesheet">
|
|
<script src="https://lettre.github.io/lettre//js/jquery-2.x.min.js"></script>
|
|
<style type="text/css">:root #header + #content > #left > #rlblock_left
|
|
{display:none !important;}</style>
|
|
|
|
|
|
</head>
|
|
<body class="" data-url="/lettre/sending-messages/smtp/">
|
|
|
|
<nav id="sidebar">
|
|
<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="https://lettre.github.io/lettre//js/lunr.min.js"></script>
|
|
<script type="text/javascript" src="https://lettre.github.io/lettre//js/horsey.js"></script>
|
|
<script type="text/javascript">
|
|
var baseurl = "https:\/\/lettre.github.io\/lettre\/";
|
|
</script>
|
|
<script type="text/javascript" src="https://lettre.github.io/lettre//js/search.js"></script>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div class="highlightable">
|
|
<ul class="topics">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="dd-item " data-nav-id="/lettre/getting-started/">
|
|
<a href="/lettre/getting-started/">
|
|
<span>
|
|
|
|
<b>1. </b>
|
|
|
|
Getting started
|
|
|
|
</span>
|
|
</a>
|
|
|
|
<ul>
|
|
|
|
<li class="dd-item " data-nav-id="/lettre/getting-started/intro/">
|
|
<a href="/lettre/getting-started/intro/">
|
|
<span>Introduction </i></span>
|
|
</a>
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="dd-item " data-nav-id="/lettre/creating-messages/">
|
|
<a href="/lettre/creating-messages/">
|
|
<span>
|
|
|
|
<b>2. </b>
|
|
|
|
Creating messages
|
|
|
|
</span>
|
|
</a>
|
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="dd-item parent" data-nav-id="/lettre/sending-messages/">
|
|
<a href="/lettre/sending-messages/">
|
|
<span>
|
|
|
|
<b>3. </b>
|
|
|
|
Sending messages
|
|
|
|
</span>
|
|
</a>
|
|
|
|
<ul>
|
|
|
|
<li class="dd-item " data-nav-id="/lettre/sending-messages/intro/">
|
|
<a href="/lettre/sending-messages/intro/">
|
|
<span>Introduction </i></span>
|
|
</a>
|
|
</li>
|
|
|
|
<li class="dd-item active" data-nav-id="/lettre/sending-messages/smtp/">
|
|
<a href="/lettre/sending-messages/smtp/">
|
|
<span>SMTP transport </i></span>
|
|
</a>
|
|
</li>
|
|
|
|
<li class="dd-item " data-nav-id="/lettre/sending-messages/sendmail/">
|
|
<a href="/lettre/sending-messages/sendmail/">
|
|
<span>Sendmail transport </i></span>
|
|
</a>
|
|
</li>
|
|
|
|
<li class="dd-item " data-nav-id="/lettre/sending-messages/file/">
|
|
<a href="/lettre/sending-messages/file/">
|
|
<span>File transport </i></span>
|
|
</a>
|
|
</li>
|
|
|
|
<li class="dd-item " data-nav-id="/lettre/sending-messages/stub/">
|
|
<a href="/lettre/sending-messages/stub/">
|
|
<span>Stub transport </i></span>
|
|
</a>
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
<hr>
|
|
|
|
<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">
|
|
|
|
<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"><a href=""><i class="fa fa-list-alt"></i></a></span>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<a href="/lettre/sending-messages/" itemprop="url"><span itemprop="title">Sending messages</span></a> <i class="fa fa-angle-right"></i>
|
|
|
|
|
|
|
|
<span itemprop="title"> 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 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 desinged to be:</p>
|
|
|
|
<ul>
|
|
<li>Secured: email are encrypted by default</li>
|
|
<li>Modern: Unicode support for email content and sender/recipient adresses 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>
|
|
use lettre::{SimpleSendableEmail, EmailTransport, EmailAddress, SmtpTransport};
|
|
|
|
let email = SimpleSendableEmail::new(
|
|
EmailAddress::new("user@localhost".to_string()),
|
|
vec![EmailAddress::new("root@localhost".to_string())],
|
|
"message_id".to_string(),
|
|
"Hello world".to_string(),
|
|
);
|
|
|
|
// Open a local connection on port 25
|
|
let mut mailer =
|
|
SmtpTransport::builder_unencrypted_localhost().unwrap().build();
|
|
// Send the email
|
|
let result = mailer.send(&email);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
<h4 id="complete-example">Complete example</h4>
|
|
use lettre::smtp::authentication::{Credentials, Mechanism};
|
|
use lettre::smtp::SUBMISSION_PORT;
|
|
use lettre::{SimpleSendableEmail, EmailTransport, EmailAddress, SmtpTransport};
|
|
use lettre::smtp::extension::ClientId;
|
|
use lettre::smtp::ConnectionReuseParameters;
|
|
|
|
|
|
let email = SimpleSendableEmail::new(
|
|
EmailAddress::new("user@localhost".to_string()),
|
|
vec![EmailAddress::new("root@localhost".to_string())],
|
|
"message_id".to_string(),
|
|
"Hello world".to_string(),
|
|
);
|
|
|
|
// Connect to a remote server on a custom port
|
|
let mut mailer = SmtpTransport::simple_builder("server.tld".to_string()).unwrap()
|
|
// Set the name sent during EHLO/HELO, default is `localhost`
|
|
.hello_name(ClientId::Domain("my.hostname.tld".to_string()))
|
|
// Add credentials for authentication
|
|
.credentials(Credentials::new("username".to_string(), "password".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(&email);
|
|
assert!(result_1.is_ok());
|
|
|
|
// The second email will use the same connection
|
|
let result_2 = mailer.send(&email);
|
|
assert!(result_2.is_ok());
|
|
|
|
// Explicitly close the SMTP transaction as we enabled connection reuse
|
|
mailer.close();
|
|
|
|
<h4 id="lower-level">Lower level</h4>
|
|
|
|
<p>You can also send commands, here is a simple email transaction without
|
|
error handling:</p>
|
|
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::*;
|
|
|
|
let mut email_client: Client<NetworkStream> = Client::new();
|
|
let _ = email_client.connect(&("localhost", SMTP_PORT), None);
|
|
let _ = email_client.smtp_command(EhloCommand::new(ClientId::new("my_hostname".to_string())));
|
|
let _ = email_client.smtp_command(
|
|
MailCommand::new(Some(EmailAddress::new("user@example.com".to_string())), vec![])
|
|
);
|
|
let _ = email_client.smtp_command(
|
|
RcptCommand::new(EmailAddress::new("user@example.org".to_string()), vec![])
|
|
);
|
|
let _ = email_client.smtp_command(DataCommand);
|
|
let _ = email_client.message(Box::new("Test email".as_bytes()));
|
|
let _ = email_client.smtp_command(QuitCommand);
|
|
|
|
|
|
|
|
|
|
</div>
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<div id="navigation">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<a class="nav nav-prev" href="/lettre/sending-messages/intro/"> <i class="fa fa-chevron-left"></i></a>
|
|
|
|
|
|
|
|
|
|
<a class="nav nav-next" href="/lettre/sending-messages/sendmail/" 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="https://lettre.github.io/lettre//js/clipboard.min.js"></script>
|
|
<script src="https://lettre.github.io/lettre//js/perfect-scrollbar.min.js"></script>
|
|
<script src="https://lettre.github.io/lettre//js/perfect-scrollbar.jquery.min.js"></script>
|
|
<script src="https://lettre.github.io/lettre//js/jquery.sticky-kit.min.js"></script>
|
|
<script src="https://lettre.github.io/lettre//js/featherlight.min.js"></script>
|
|
<script src="https://lettre.github.io/lettre//js/html5shiv-printshiv.min.js"></script>
|
|
<script src="https://lettre.github.io/lettre//js/highlight.pack.js"></script>
|
|
<script>hljs.initHighlightingOnLoad();</script>
|
|
<script src="https://lettre.github.io/lettre//js/modernizr.custom.71422.js"></script>
|
|
<script src="https://lettre.github.io/lettre//js/learn.js"></script>
|
|
<script src="https://lettre.github.io/lettre//js/hugo-learn.js"></script>
|
|
|
|
|
|
</body>
|
|
</html>
|
|
|