Sending email with perl

1,548 views
1,407 views

Published on

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.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,548
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \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’s a number of things you need to do to make sure you’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’s a number of things you need to do to make sure you’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’s a number of things you need to do to make sure you’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’s a number of things you need to do to make sure you’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’s a number of things you need to do to make sure you’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’s a number of things you need to do to make sure you’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’s simple and it does one thing. It’s got one class method and that’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’s simple and it does one thing. It’s got one class method and that’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’s simple and it does one thing. It’s got one class method and that’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’s simple and it does one thing. It’s got one class method and that’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’s simple and it does one thing. It’s got one class method and that’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’s simple and it does one thing. It’s got one class method and that’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’s simple and it does one thing. It’s got one class method and that’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’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’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’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’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’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’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’s lots of different tools for dealing with email messages. They have varying amounts of development behind them and going into them presently. We’re focusing on Email::MIME because it works with other tools and it’s pretty solid overall.\n
  • There’s lots of different tools for dealing with email messages. They have varying amounts of development behind them and going into them presently. We’re focusing on Email::MIME because it works with other tools and it’s pretty solid overall.\n
  • There’s lots of different tools for dealing with email messages. They have varying amounts of development behind them and going into them presently. We’re focusing on Email::MIME because it works with other tools and it’s pretty solid overall.\n
  • There’s lots of different tools for dealing with email messages. They have varying amounts of development behind them and going into them presently. We’re focusing on Email::MIME because it works with other tools and it’s pretty solid overall.\n
  • \n
  • These are some of the common methods that you’ll see in every day use. I’ve only got 20 minutes to cover a number of tools. RTFM.\n
  • These are some of the common methods that you’ll see in every day use. I’ve only got 20 minutes to cover a number of tools. RTFM.\n
  • These are some of the common methods that you’ll see in every day use. I’ve only got 20 minutes to cover a number of tools. RTFM.\n
  • These are some of the common methods that you’ll see in every day use. I’ve only got 20 minutes to cover a number of tools. RTFM.\n
  • These are some of the common methods that you’ll see in every day use. I’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’re specifying characters, not bytes.\n\nYou are working in characters, right?\n\nThen there’s the body parameter. It’s pretty simple.\n\nSo you might be wondering what strictures is. Basically it’s strict with a bunch of other stuff added in.\n
  • This looks more or less like what you’d expect. To keep the example simple we didn’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’ve made sure our email addresses are valid and deliverable. We’ve analyzed them to do any introspection or policy enforcement on them. We’ve created a message and filled it with a complicated DAG of attachments. But this is all academic if we can’t actually send the message. Here’s where Email::Sender comes into play.\n
  • Okay, awesome. We’ve made sure our email addresses are valid and deliverable. We’ve analyzed them to do any introspection or policy enforcement on them. We’ve created a message and filled it with a complicated DAG of attachments. But this is all academic if we can’t actually send the message. Here’s where Email::Sender comes into play.\n
  • Okay, awesome. We’ve made sure our email addresses are valid and deliverable. We’ve analyzed them to do any introspection or policy enforcement on them. We’ve created a message and filled it with a complicated DAG of attachments. But this is all academic if we can’t actually send the message. Here’s where Email::Sender comes into play.\n
  • Okay, awesome. We’ve made sure our email addresses are valid and deliverable. We’ve analyzed them to do any introspection or policy enforcement on them. We’ve created a message and filled it with a complicated DAG of attachments. But this is all academic if we can’t actually send the message. Here’s where Email::Sender comes into play.\n
  • Okay, awesome. We’ve made sure our email addresses are valid and deliverable. We’ve analyzed them to do any introspection or policy enforcement on them. We’ve created a message and filled it with a complicated DAG of attachments. But this is all academic if we can’t actually send the message. Here’s where Email::Sender comes into play.\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Sending email with perl

    1. 1. Sending Email with PerlWhy does it hurt so much, daddy?2011-10-09
    2. 2. About Me✤ apeiron (IRC/CPAN) ✤ Chris Nehren if you insist on a meatspace name✤ I like Perl and tabletop RPGs
    3. 3. Tools of the trade
    4. 4. Tools of the trade✤ Mail::Box (though if you don’t mind complexity, it’s really solid)
    5. 5. Tools of the trade✤ Mail::Box (though if you don’t mind complexity, it’s really solid)✤ Email::Send
    6. 6. Tools of the trade✤ Mail::Box (though if you don’t mind complexity, it’s really solid)✤ Email::Send✤ Email::Valid
    7. 7. Tools of the trade✤ Mail::Box (though if you don’t mind complexity, it’s really solid)✤ Email::Send✤ Email::Valid✤ Email::Address
    8. 8. Tools of the trade✤ Mail::Box (though if you don’t mind complexity, it’s really solid)✤ Email::Send✤ Email::Valid✤ Email::Address✤ Email::MIME
    9. 9. Tools of the trade✤ Mail::Box (though if you don’t mind complexity, it’s really solid)✤ Email::Send✤ Email::Valid✤ Email::Address✤ Email::MIME✤ Email::Sender
    10. 10. Email::Valid
    11. 11. Email::Valid✤ Simple email address validation
    12. 12. Email::Valid✤ Simple email address validation✤ One (class) method: address($address)
    13. 13. Email::Valid✤ Simple email address validation✤ One (class) method: address($address)✤ Call method
    14. 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. 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 validBacon may or may not be imaginary. Consult your Perl vendor for details.
    16. 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. 17. Usage Example#!env perluse 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:notvalidvalid
    18. 18. Email::Address
    19. 19. Email::Address✤ Used for accessing different parts of an address
    20. 20. Email::Address✤ Used for accessing different parts of an address✤ Some mutators, but primarily accessors
    21. 21. Email::Address✤ Used for accessing different parts of an address✤ Some mutators, but primarily accessors✤ Two constructors: parse and new
    22. 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. 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.
    24. 24. Email::Address Methods
    25. 25. Email::Address Methods✤ address ✤ get/set actual address, e.g. foo@bar.com
    26. 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. 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. 28. Usage example#!env perluse 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 overloadsay $three->address; # raw address, no phrase/comment.say $three->host;say $three->user;# yields:"hello" <me@example.com>example.commeme@example.com
    29. 29. Email::MIME -- Why?
    30. 30. Email::MIME -- Why?✤ It’s simple
    31. 31. Email::MIME -- Why?✤ It’s simple✤ It’s easy
    32. 32. Email::MIME -- Why?✤ It’s simple✤ It’s easy✤ It works
    33. 33. Email::MIME -- Why?✤ It’s simple✤ It’s easy✤ It works✤ ... but really it just sucks the least.
    34. 34. So how do youuse it?This won’t hurt a bit.
    35. 35. Methods of Email::MIME
    36. 36. Methods of Email::MIME✤ body_str_set($unicode) ✤ sets body to given Unicode characters. Warning: clears other parts!
    37. 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. 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. 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. 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. 41. From the new perlfaq9:#!env perluse 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. 42. If we call $message->as_string,From: you@example.comTo: friend@example.comSubject: Happy birthday!Date: Sat, 8 Oct 2011 16:56:41 -0400MIME-Version: 1.0Happy birthday to you!
    43. 43. Email::Sender
    44. 44. Email::Sender✤ Moose-based interface to different ways of sending email messages
    45. 45. Email::Sender✤ Moose-based interface to different ways of sending email messages✤ Different transports for different purposes
    46. 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. 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. 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.
    49. 49. Email::Sender Transport Gazetteer
    50. 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. 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. 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. 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. 54. Example Usagessendmail($message); # default transportsendmail( $message, { transport => Email::Sender::Transport::SMTP->new( host => ‘mx.example.com’, port => 25, ); });
    55. 55. Bonus material: pmail(1) demoWhen You Run Out Of Material, Demo A Very Flexible Program You Wrote

    ×