There's an ever-increasing number of modules for sending mail with Perl. With all of the choices that CPAN offers, how does one choose which module best suits their needs? It can be hard to navigate this growing sea of choice, but I spent half a year working with rjbs, whom many of you know as one of Perl's premiere email hackers. I learned a lot from him, and I want to share that knowledge with you to help make your email code suck less.
In this talk, I'll provide a small collection of modules for sending email with Perl, with example code. You really only need to know a few to be productive for most tasks, and only a few more if you have unusual requirements or are an email power user.
14. Email::Valid
✤ Simple email address validation
✤ One (class) method: address($address)
✤ Call method
✤ Receive Bacon*
Bacon may or may not be imaginary. Consult your Perl vendor for details.
15. Email::Valid
✤ Simple email address validation
✤ One (class) method: address($address)
✤ Call method
✤ Receive Bacon*
✤ Also receive true/false value depending upon whether it’s valid
Bacon may or may not be imaginary. Consult your Perl vendor for details.
16. Email::Valid
✤ Simple email address validation
✤ One (class) method: address($address)
✤ Call method
✤ Receive Bacon*
✤ Also receive true/false value depending upon whether it’s valid
✤ There are actually lots of methods for inspecting the address, but
address is the one you’re likely to use the most.
Bacon may or may not be imaginary. Consult your Perl vendor for details.
17. Usage Example
#!env perl
use 5.014;
use strictures 1;
use Email::Valid;
say Email::Valid->address('foo') ? "valid" : "not";
say Email::Valid->address('foo@bar.com') ? "valid" : "not";
say Email::Valid->address('foo+baz@bar.com') ? "valid" : "not";
# yields this:
not
valid
valid
19. Email::Address
✤ Used for accessing different parts of an address
20. Email::Address
✤ Used for accessing different parts of an address
✤ Some mutators, but primarily accessors
21. Email::Address
✤ Used for accessing different parts of an address
✤ Some mutators, but primarily accessors
✤ Two constructors: parse and new
22. Email::Address
✤ Used for accessing different parts of an address
✤ Some mutators, but primarily accessors
✤ Two constructors: parse and new
✤ parse returns a list of addresses
23. Email::Address
✤ Used for accessing different parts of an address
✤ Some mutators, but primarily accessors
✤ Two constructors: parse and new
✤ parse returns a list of addresses
✤ new accepts a phrase, address, and comment*
Actually it accepts a fourth argument, an original string. But you probably should ignore it.
26. Email::Address Methods
✤ address
✤ get/set actual address, e.g. foo@bar.com
✤ host
✤ get/set the hostname of the address, e.g. bar.com
27. Email::Address Methods
✤ address
✤ get/set actual address, e.g. foo@bar.com
✤ host
✤ get/set the hostname of the address, e.g. bar.com
✤ user
✤ get/set the local part of the address, e.g. foo
28. Usage example
#!env perl
use 5.014;
use strictures 1;
use Email::Address;
my ($one, $two, $three) = Email::Address->parse(
'foo@bar.com, baz@quux.com, "hello" <me@example.com>'
);
say $three; # stringify overload
say $three->address; # raw address, no phrase/comment.
say $three->host;
say $three->user;
# yields:
"hello" <me@example.com>
example.com
me
me@example.com
36. Methods of Email::MIME
✤ body_str_set($unicode)
✤ sets body to given Unicode characters. Warning: clears other parts!
37. Methods of Email::MIME
✤ body_str_set($unicode)
✤ sets body to given Unicode characters. Warning: clears other parts!
✤ header_str_set($header, $unicode)
✤ like body_str_set, but for headers.
38. Methods of Email::MIME
✤ body_str_set($unicode)
✤ sets body to given Unicode characters. Warning: clears other parts!
✤ header_str_set($header, $unicode)
✤ like body_str_set, but for headers.
✤ parts_set(@email_mime_objects)
✤ replaces the parts of the object with the given parts
39. Methods of Email::MIME
✤ body_str_set($unicode)
✤ sets body to given Unicode characters. Warning: clears other parts!
✤ header_str_set($header, $unicode)
✤ like body_str_set, but for headers.
✤ parts_set(@email_mime_objects)
✤ replaces the parts of the object with the given parts
✤ parts_add(@more_parts)
✤ appends, not sets
40. Methods of Email::MIME
✤ body_str_set($unicode)
✤ sets body to given Unicode characters. Warning: clears other parts!
✤ header_str_set($header, $unicode)
✤ like body_str_set, but for headers.
✤ parts_set(@email_mime_objects)
✤ replaces the parts of the object with the given parts
✤ parts_add(@more_parts)
✤ appends, not sets
✤ walk_parts($coderef)
✤ give it a callback, gets called with Email::MIME objects
41. From the new perlfaq9:
#!env perl
use strictures 1;
use Email::MIME;
my $message = Email::MIME->create(
header_str => [
From => 'you@example.com',
To => 'friend@example.com',
Subject => 'Happy birthday!',
],
body => 'Happy birthday to you!',
);
42. If we call
$message->as_string,
From: you@example.com
To: friend@example.com
Subject: Happy birthday!
Date: Sat, 8 Oct 2011 16:56:41 -0400
MIME-Version: 1.0
Happy birthday to you!
44. Email::Sender
✤ Moose-based interface to different ways of sending email messages
45. Email::Sender
✤ Moose-based interface to different ways of sending email messages
✤ Different transports for different purposes
46. Email::Sender
✤ Moose-based interface to different ways of sending email messages
✤ Different transports for different purposes
✤ Suggested interface (for now) is Email::Sender::Simple
47. Email::Sender
✤ Moose-based interface to different ways of sending email messages
✤ Different transports for different purposes
✤ Suggested interface (for now) is Email::Sender::Simple
✤ This exposes one function: sendmail().
48. Email::Sender
✤ Moose-based interface to different ways of sending email messages
✤ Different transports for different purposes
✤ Suggested interface (for now) is Email::Sender::Simple
✤ This exposes one function: sendmail().
✤ sendmail() accepts Email::MIME objects, so we’re in business.
50. Email::Sender Transport Gazetteer
✤ Email::Sender::Transport::Sendmail
✤ tries to use /usr/sbin/sendmail or the like; you probably need to
smarthost / relay
51. Email::Sender Transport Gazetteer
✤ Email::Sender::Transport::Sendmail
✤ tries to use /usr/sbin/sendmail or the like; you probably need to
smarthost / relay
✤ Email::Sender::Transport::SMTP
✤ Send mail through an SMTP server, optionally with SASL
authentication.
52. Email::Sender Transport Gazetteer
✤ Email::Sender::Transport::Sendmail
✤ tries to use /usr/sbin/sendmail or the like; you probably need to
smarthost / relay
✤ Email::Sender::Transport::SMTP
✤ Send mail through an SMTP server, optionally with SASL
authentication.
✤ Email::Sender::Transport::SMTP::TLS
✤ Like the above, but uses TLS. If you use Gmail, you’ll need this.
53. Email::Sender Transport Gazetteer
✤ Email::Sender::Transport::Sendmail
✤ tries to use /usr/sbin/sendmail or the like; you probably need to
smarthost / relay
✤ Email::Sender::Transport::SMTP
✤ Send mail through an SMTP server, optionally with SASL
authentication.
✤ Email::Sender::Transport::SMTP::TLS
✤ Like the above, but uses TLS. If you use Gmail, you’ll need this.
✤ Email::Sender::Transport::Test
✤ Delivers to an in-memory structure for analysis
54. Example Usages
sendmail($message); # default transport
sendmail(
$message,
{
transport => Email::Sender::Transport::SMTP->new(
host => ‘mx.example.com’,
port => 25,
);
}
);
55. Bonus material: pmail(1) demo
When You Run Out Of Material, Demo A Very Flexible Program You Wrote
Hinweis der Redaktion
\n
\n
There are a lot of email modules on CPAN. A lot. No, really. I could fill 20 minutes just listing them all. This talk only has time to focus on the most recommended modules.\n\nThere&#x2019;s a number of things you need to do to make sure you&#x2019;re sending email correctly, including address validation, message construction, and actually sending the message. The tools to do this have evolved over the years. Some have fallen out of favor because other tools do 80% of the work in 20% of the code. Others are just deprecated because of problems in architecture or implementation. This talk will be covering the current recommended state of the art \n\nYes, they use Moose. Deal with it.\n
There are a lot of email modules on CPAN. A lot. No, really. I could fill 20 minutes just listing them all. This talk only has time to focus on the most recommended modules.\n\nThere&#x2019;s a number of things you need to do to make sure you&#x2019;re sending email correctly, including address validation, message construction, and actually sending the message. The tools to do this have evolved over the years. Some have fallen out of favor because other tools do 80% of the work in 20% of the code. Others are just deprecated because of problems in architecture or implementation. This talk will be covering the current recommended state of the art \n\nYes, they use Moose. Deal with it.\n
There are a lot of email modules on CPAN. A lot. No, really. I could fill 20 minutes just listing them all. This talk only has time to focus on the most recommended modules.\n\nThere&#x2019;s a number of things you need to do to make sure you&#x2019;re sending email correctly, including address validation, message construction, and actually sending the message. The tools to do this have evolved over the years. Some have fallen out of favor because other tools do 80% of the work in 20% of the code. Others are just deprecated because of problems in architecture or implementation. This talk will be covering the current recommended state of the art \n\nYes, they use Moose. Deal with it.\n
There are a lot of email modules on CPAN. A lot. No, really. I could fill 20 minutes just listing them all. This talk only has time to focus on the most recommended modules.\n\nThere&#x2019;s a number of things you need to do to make sure you&#x2019;re sending email correctly, including address validation, message construction, and actually sending the message. The tools to do this have evolved over the years. Some have fallen out of favor because other tools do 80% of the work in 20% of the code. Others are just deprecated because of problems in architecture or implementation. This talk will be covering the current recommended state of the art \n\nYes, they use Moose. Deal with it.\n
There are a lot of email modules on CPAN. A lot. No, really. I could fill 20 minutes just listing them all. This talk only has time to focus on the most recommended modules.\n\nThere&#x2019;s a number of things you need to do to make sure you&#x2019;re sending email correctly, including address validation, message construction, and actually sending the message. The tools to do this have evolved over the years. Some have fallen out of favor because other tools do 80% of the work in 20% of the code. Others are just deprecated because of problems in architecture or implementation. This talk will be covering the current recommended state of the art \n\nYes, they use Moose. Deal with it.\n
There are a lot of email modules on CPAN. A lot. No, really. I could fill 20 minutes just listing them all. This talk only has time to focus on the most recommended modules.\n\nThere&#x2019;s a number of things you need to do to make sure you&#x2019;re sending email correctly, including address validation, message construction, and actually sending the message. The tools to do this have evolved over the years. Some have fallen out of favor because other tools do 80% of the work in 20% of the code. Others are just deprecated because of problems in architecture or implementation. This talk will be covering the current recommended state of the art \n\nYes, they use Moose. Deal with it.\n
Probably want to make sure our email addresses are valid before trying to send something to one, or even construct an email containing one. So we have Email::Valid for that. It&#x2019;s simple and it does one thing. It&#x2019;s got one class method and that&#x2019;s all it needs.\n
Probably want to make sure our email addresses are valid before trying to send something to one, or even construct an email containing one. So we have Email::Valid for that. It&#x2019;s simple and it does one thing. It&#x2019;s got one class method and that&#x2019;s all it needs.\n
Probably want to make sure our email addresses are valid before trying to send something to one, or even construct an email containing one. So we have Email::Valid for that. It&#x2019;s simple and it does one thing. It&#x2019;s got one class method and that&#x2019;s all it needs.\n
Probably want to make sure our email addresses are valid before trying to send something to one, or even construct an email containing one. So we have Email::Valid for that. It&#x2019;s simple and it does one thing. It&#x2019;s got one class method and that&#x2019;s all it needs.\n
Probably want to make sure our email addresses are valid before trying to send something to one, or even construct an email containing one. So we have Email::Valid for that. It&#x2019;s simple and it does one thing. It&#x2019;s got one class method and that&#x2019;s all it needs.\n
Probably want to make sure our email addresses are valid before trying to send something to one, or even construct an email containing one. So we have Email::Valid for that. It&#x2019;s simple and it does one thing. It&#x2019;s got one class method and that&#x2019;s all it needs.\n
Probably want to make sure our email addresses are valid before trying to send something to one, or even construct an email containing one. So we have Email::Valid for that. It&#x2019;s simple and it does one thing. It&#x2019;s got one class method and that&#x2019;s all it needs.\n
\n
If we want to do something with our addresses before sending them, e.g. to enforce some sort of policy or business logic, Email::Address gives us tools to inspect and reason about our addresses. It provides methods for looking at all the different parts of an email address, including things you likely won&#x2019;t care about like the phrase or the comment.\n
If we want to do something with our addresses before sending them, e.g. to enforce some sort of policy or business logic, Email::Address gives us tools to inspect and reason about our addresses. It provides methods for looking at all the different parts of an email address, including things you likely won&#x2019;t care about like the phrase or the comment.\n
If we want to do something with our addresses before sending them, e.g. to enforce some sort of policy or business logic, Email::Address gives us tools to inspect and reason about our addresses. It provides methods for looking at all the different parts of an email address, including things you likely won&#x2019;t care about like the phrase or the comment.\n
If we want to do something with our addresses before sending them, e.g. to enforce some sort of policy or business logic, Email::Address gives us tools to inspect and reason about our addresses. It provides methods for looking at all the different parts of an email address, including things you likely won&#x2019;t care about like the phrase or the comment.\n
If we want to do something with our addresses before sending them, e.g. to enforce some sort of policy or business logic, Email::Address gives us tools to inspect and reason about our addresses. It provides methods for looking at all the different parts of an email address, including things you likely won&#x2019;t care about like the phrase or the comment.\n
If we want to do something with our addresses before sending them, e.g. to enforce some sort of policy or business logic, Email::Address gives us tools to inspect and reason about our addresses. It provides methods for looking at all the different parts of an email address, including things you likely won&#x2019;t care about like the phrase or the comment.\n
Just a sampling of the methods, again. \n
Just a sampling of the methods, again. \n
Just a sampling of the methods, again. \n
\n
There&#x2019;s lots of different tools for dealing with email messages. They have varying amounts of development behind them and going into them presently. We&#x2019;re focusing on Email::MIME because it works with other tools and it&#x2019;s pretty solid overall.\n
There&#x2019;s lots of different tools for dealing with email messages. They have varying amounts of development behind them and going into them presently. We&#x2019;re focusing on Email::MIME because it works with other tools and it&#x2019;s pretty solid overall.\n
There&#x2019;s lots of different tools for dealing with email messages. They have varying amounts of development behind them and going into them presently. We&#x2019;re focusing on Email::MIME because it works with other tools and it&#x2019;s pretty solid overall.\n
There&#x2019;s lots of different tools for dealing with email messages. They have varying amounts of development behind them and going into them presently. We&#x2019;re focusing on Email::MIME because it works with other tools and it&#x2019;s pretty solid overall.\n
\n
These are some of the common methods that you&#x2019;ll see in every day use. I&#x2019;ve only got 20 minutes to cover a number of tools. RTFM.\n
These are some of the common methods that you&#x2019;ll see in every day use. I&#x2019;ve only got 20 minutes to cover a number of tools. RTFM.\n
These are some of the common methods that you&#x2019;ll see in every day use. I&#x2019;ve only got 20 minutes to cover a number of tools. RTFM.\n
These are some of the common methods that you&#x2019;ll see in every day use. I&#x2019;ve only got 20 minutes to cover a number of tools. RTFM.\n
These are some of the common methods that you&#x2019;ll see in every day use. I&#x2019;ve only got 20 minutes to cover a number of tools. RTFM.\n
So what do we have here? The boilerplate at the top is pretty obvious. We call the create method on Email::MIME and it builds us an object with the given data. We use the header_str parameter to the create constructor instead of header as we&#x2019;re specifying characters, not bytes.\n\nYou are working in characters, right?\n\nThen there&#x2019;s the body parameter. It&#x2019;s pretty simple.\n\nSo you might be wondering what strictures is. Basically it&#x2019;s strict with a bunch of other stuff added in.\n
This looks more or less like what you&#x2019;d expect. To keep the example simple we didn&#x2019;t specify a message-id. A sane MTA will append one when sending a message without one, the same for the date. \n
Okay, awesome. We&#x2019;ve made sure our email addresses are valid and deliverable. We&#x2019;ve analyzed them to do any introspection or policy enforcement on them. We&#x2019;ve created a message and filled it with a complicated DAG of attachments. But this is all academic if we can&#x2019;t actually send the message. Here&#x2019;s where Email::Sender comes into play.\n
Okay, awesome. We&#x2019;ve made sure our email addresses are valid and deliverable. We&#x2019;ve analyzed them to do any introspection or policy enforcement on them. We&#x2019;ve created a message and filled it with a complicated DAG of attachments. But this is all academic if we can&#x2019;t actually send the message. Here&#x2019;s where Email::Sender comes into play.\n
Okay, awesome. We&#x2019;ve made sure our email addresses are valid and deliverable. We&#x2019;ve analyzed them to do any introspection or policy enforcement on them. We&#x2019;ve created a message and filled it with a complicated DAG of attachments. But this is all academic if we can&#x2019;t actually send the message. Here&#x2019;s where Email::Sender comes into play.\n
Okay, awesome. We&#x2019;ve made sure our email addresses are valid and deliverable. We&#x2019;ve analyzed them to do any introspection or policy enforcement on them. We&#x2019;ve created a message and filled it with a complicated DAG of attachments. But this is all academic if we can&#x2019;t actually send the message. Here&#x2019;s where Email::Sender comes into play.\n
Okay, awesome. We&#x2019;ve made sure our email addresses are valid and deliverable. We&#x2019;ve analyzed them to do any introspection or policy enforcement on them. We&#x2019;ve created a message and filled it with a complicated DAG of attachments. But this is all academic if we can&#x2019;t actually send the message. Here&#x2019;s where Email::Sender comes into play.\n