Acme-CPANModulesBundle-Import-PerlDancerAdvent-2018
view release on metacpan or search on metacpan
devdata/http_advent.perldancer.org_2018_17 view on Meta::CPAN
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title> Dancer and Email | PerlDancer Advent Calendar</title>
<link rel="stylesheet" href="/css/style.css" />
<link rel="alternate" type="application/rss+xml" title="All Articles " href="/feed/2018" />
<!-- Grab Google CDN's jQuery. fall back to local if necessary -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">/* <![CDATA[ */
!window.jQuery && document.write('<script src="/javascripts/jquery.js"><\/script>')
/* ]]> */</script>
<!-- Prettyfy -->
<link href="/css/prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="/javascripts/prettify.js"></script>
</head>
<body onload="prettyPrint()">
<div id="page">
<div id="sidebar">
<a href="/" class="homelink">Dancer Advent Calendar</a><br />
<p>
The PerlDancer Advent Calendar is a community-driven project that aims
to showcase the Dancer Perl web framework.
</p>
<p>
Each day of December until Christmas, one article about Dancer. Stay tuned for new moves!
</p>
<ul id="sidebar-items">
<li>
<h3>About Dancer</h3>
<ul class="links">
<li><a href="http://www.perldancer.org/">Dancer homepage</a></li>
<li><a href="http://twitter.com/PerlDancer">Official Twitter</a></li>
<li><a href="http://github.com/PerlDancer/Dancer">Dancer on GitHub</a></li>
<li><a href="http://github.com/PerlDancer/Dancer2">Dancer 2 on GitHub</a></li>
<li><a class="feed" href="/feed/2018">RSS</a></li>
</ul>
</li>
</ul>
</div>
<div id="content">
<div class="pod-document"><h1><a name="dancer_and_email"></a>Dancer and Email</h1>
<p>Web applications regularly need to send email to its users, e.g. receipts or password reset links.
The <a href="https://metacpan.org/pod/Dancer2::Plugin::Email">Email</a> plugin for Dancer2 simplifies this task by
providing the <code>email</code> keyword and a sane default configuration.</p>
<p>So the unavoidable "Hello world" example would look like:</p>
<pre class="prettyprint">email {
from => 'foo@perl.dance',
to => 'bar@perl.dance',
subject => 'Hello world',
text => 'Welcome to the dancefloor!',
};</pre>
<p>The more common case would be to use a template from your web application and turn it into a HTML email.</p>
<p>Instead of using the <code>template</code> keyword to return the HTML from your route to the browser, you generate HTML with a specific layout,
store in a variable and send the email.</p>
<pre class="prettyprint">post '/welcome' => {
my $html = template $template, $tokens, { layout => 'email' };
email {
from => 'foo@perl.dance',
to => 'bar@perl.dance',
subject => 'Welcome to the dancefloor!',
type => 'html',
body => $html,
}
redirect '/home';
}</pre>
<h2><a name="utf_8"></a>UTF-8</h2>
<p>Take care to encode your content and mail headers if it may contain UTF-8 characters.
It might even look good in your email client if you don't do this, but not necessarily for other ones.</p>
<pre class="prettyprint">email {
from => 'foo@perl.dance',
to => 'bar@perl.dance',
subject => encode('MIME-Header', 'Über uns'),
type => 'html',
body => encode('UTF-8',
'Die Geschichte unseres Projekts begann mit dem französischen Entwicker Alexis Sukrieh ...'),
};</pre>
<h2><a name="inline_images"></a>Inline images</h2>
<p>You can simply deliver emails with links to images, but usually email clients would not load them without user interaction.
It is possible though to attached the images to the email and reference them in the email body with a custom HTML tag:</p>
<pre class="prettyprint"> email {
from => 'foo@perl.dance',
to => 'bar@perl.dance',
subject => 'Welcome to the dancefloor!',
body => q{<p>Image embedded: <img src="cid:mycid"/></p>},
type => 'html',
attach => [ { Id => 'mycid', Path => '/dancefloor/dcr-header-logo.png' }],
multipart => 'related'
};</pre>
<h2><a name="providing_plain_text_part"></a>Providing plain text part</h2>
<p><a href="https://metacpan.org/pod/HTML::FormatText::WithLinks">HTML::FormatText::WithLinks</a> makes it easy to provide a plain text version
of your HTML email:</p>
<pre class="prettyprint">my $html = template $template, $tokens, { layout => 'email' };
my $f = HTML::FormatText::WithLinks->new;
( run in 0.803 second using v1.01-cache-2.11-cpan-39bf76dae61 )