SlideShare ist ein Scribd-Unternehmen logo
1 von 55
Sending Email with Perl
Why does it hurt so much, daddy?

2011-10-09
About Me



✤   apeiron (IRC/CPAN)

    ✤   Chris Nehren if you insist on a meatspace name

✤   I like Perl and tabletop RPGs
Tools of the trade
Tools of the trade

✤   Mail::Box (though if you don’t mind complexity, it’s really solid)
Tools of the trade

✤   Mail::Box (though if you don’t mind complexity, it’s really solid)

✤   Email::Send
Tools of the trade

✤   Mail::Box (though if you don’t mind complexity, it’s really solid)

✤   Email::Send

✤   Email::Valid
Tools of the trade

✤   Mail::Box (though if you don’t mind complexity, it’s really solid)

✤   Email::Send

✤   Email::Valid

✤   Email::Address
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
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
Email::Valid
Email::Valid

✤   Simple email address validation
Email::Valid

✤   Simple email address validation

✤   One (class) method: address($address)
Email::Valid

✤   Simple email address validation

✤   One (class) method: address($address)

✤   Call method
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.
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.
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.
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
Email::Address
Email::Address


✤   Used for accessing different parts of an address
Email::Address


✤   Used for accessing different parts of an address

✤   Some mutators, but primarily accessors
Email::Address


✤   Used for accessing different parts of an address

✤   Some mutators, but primarily accessors

✤   Two constructors: parse and new
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
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.
Email::Address Methods
Email::Address Methods

✤   address

    ✤   get/set actual address, e.g. foo@bar.com
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
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
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
Email::MIME -- Why?
Email::MIME -- Why?


✤   It’s simple
Email::MIME -- Why?


✤   It’s simple

✤   It’s easy
Email::MIME -- Why?


✤   It’s simple

✤   It’s easy

✤   It works
Email::MIME -- Why?


✤   It’s simple

✤   It’s easy

✤   It works

✤   ... but really it just sucks the least.
So how do you
use it?
This won’t hurt a bit.
Methods of Email::MIME
Methods of Email::MIME

✤   body_str_set($unicode)
    ✤   sets body to given Unicode characters. Warning: clears other parts!
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.
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
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
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
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!',
);
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!
Email::Sender
Email::Sender


✤   Moose-based interface to different ways of sending email messages
Email::Sender


✤   Moose-based interface to different ways of sending email messages

✤   Different transports for different purposes
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
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().
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.
Email::Sender Transport Gazetteer
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 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 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 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
Example Usages
sendmail($message); # default transport
sendmail(
  $message,
  {
    transport => Email::Sender::Transport::SMTP->new(
       host => ‘mx.example.com’,
       port => 25,
    );
  }
);
Bonus material: pmail(1) demo
When You Run Out Of Material, Demo A Very Flexible Program You Wrote

Weitere ähnliche Inhalte

Ähnlich wie Sending email with perl

Node mailer example how to send email using nodemailer with gmail &amp; mailtrap
Node mailer example how to send email using nodemailer with gmail &amp; mailtrapNode mailer example how to send email using nodemailer with gmail &amp; mailtrap
Node mailer example how to send email using nodemailer with gmail &amp; mailtrapKaty Slemon
 
Eventful Email in Ruby
Eventful Email in RubyEventful Email in Ruby
Eventful Email in Rubyhassox
 
N E T Coding Best Practices
N E T  Coding  Best  PracticesN E T  Coding  Best  Practices
N E T Coding Best PracticesAbhishek Desai
 
How To Build A Bulk Email Sending Application In PHP
How To Build A Bulk Email Sending Application In PHPHow To Build A Bulk Email Sending Application In PHP
How To Build A Bulk Email Sending Application In PHPSudheer Satyanarayana
 
Классифицируем текст в iOS без CoreML: как и зачем?
Классифицируем текст в iOS без CoreML: как и зачем? Классифицируем текст в iOS без CoreML: как и зачем?
Классифицируем текст в iOS без CoreML: как и зачем? EatDog
 
Action Mailer
Action MailerAction Mailer
Action MailerSHC
 
Validate Email with JavaScript.pdf
Validate Email with JavaScript.pdfValidate Email with JavaScript.pdf
Validate Email with JavaScript.pdfEmailConcern
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl TechniquesDave Cross
 
Email Configuration
Email ConfigurationEmail Configuration
Email ConfigurationProdigyView
 
Setting up your own email server with hmailserver
Setting up your own email server with hmailserverSetting up your own email server with hmailserver
Setting up your own email server with hmailserverrifqirr
 
Python session.11 By Shanmugam
Python session.11 By ShanmugamPython session.11 By Shanmugam
Python session.11 By ShanmugamNavaneethan Naveen
 
Lecture 3 - Comm Lab: Web @ ITP
Lecture 3 - Comm Lab: Web @ ITP Lecture 3 - Comm Lab: Web @ ITP
Lecture 3 - Comm Lab: Web @ ITP yucefmerhi
 
Perl Teach-In (part 1)
Perl Teach-In (part 1)Perl Teach-In (part 1)
Perl Teach-In (part 1)Dave Cross
 
Hide email address in sourc...
Hide email address in sourc...Hide email address in sourc...
Hide email address in sourc...chaitanya535
 

Ähnlich wie Sending email with perl (20)

Ruby
RubyRuby
Ruby
 
i &lt;3 email
i &lt;3 emaili &lt;3 email
i &lt;3 email
 
Node mailer example how to send email using nodemailer with gmail &amp; mailtrap
Node mailer example how to send email using nodemailer with gmail &amp; mailtrapNode mailer example how to send email using nodemailer with gmail &amp; mailtrap
Node mailer example how to send email using nodemailer with gmail &amp; mailtrap
 
Eventful Email in Ruby
Eventful Email in RubyEventful Email in Ruby
Eventful Email in Ruby
 
N E T Coding Best Practices
N E T  Coding  Best  PracticesN E T  Coding  Best  Practices
N E T Coding Best Practices
 
EmailTracing.ppt
EmailTracing.pptEmailTracing.ppt
EmailTracing.ppt
 
Send email
Send emailSend email
Send email
 
How To Build A Bulk Email Sending Application In PHP
How To Build A Bulk Email Sending Application In PHPHow To Build A Bulk Email Sending Application In PHP
How To Build A Bulk Email Sending Application In PHP
 
Классифицируем текст в iOS без CoreML: как и зачем?
Классифицируем текст в iOS без CoreML: как и зачем? Классифицируем текст в iOS без CoreML: как и зачем?
Классифицируем текст в iOS без CoreML: как и зачем?
 
Action Mailer
Action MailerAction Mailer
Action Mailer
 
Validate Email with JavaScript.pdf
Validate Email with JavaScript.pdfValidate Email with JavaScript.pdf
Validate Email with JavaScript.pdf
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl Techniques
 
Email Configuration
Email ConfigurationEmail Configuration
Email Configuration
 
Setting up your own email server with hmailserver
Setting up your own email server with hmailserverSetting up your own email server with hmailserver
Setting up your own email server with hmailserver
 
Python session.11 By Shanmugam
Python session.11 By ShanmugamPython session.11 By Shanmugam
Python session.11 By Shanmugam
 
Python session 11
Python session 11Python session 11
Python session 11
 
Lecture 3 - Comm Lab: Web @ ITP
Lecture 3 - Comm Lab: Web @ ITP Lecture 3 - Comm Lab: Web @ ITP
Lecture 3 - Comm Lab: Web @ ITP
 
XML::Writer::Simple
XML::Writer::SimpleXML::Writer::Simple
XML::Writer::Simple
 
Perl Teach-In (part 1)
Perl Teach-In (part 1)Perl Teach-In (part 1)
Perl Teach-In (part 1)
 
Hide email address in sourc...
Hide email address in sourc...Hide email address in sourc...
Hide email address in sourc...
 

Kürzlich hochgeladen

From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 

Kürzlich hochgeladen (20)

From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 

Sending email with perl

  • 1. Sending Email with Perl Why does it hurt so much, daddy? 2011-10-09
  • 2. About Me ✤ apeiron (IRC/CPAN) ✤ Chris Nehren if you insist on a meatspace name ✤ I like Perl and tabletop RPGs
  • 3. Tools of the trade
  • 4. Tools of the trade ✤ Mail::Box (though if you don’t mind complexity, it’s really solid)
  • 5. Tools of the trade ✤ Mail::Box (though if you don’t mind complexity, it’s really solid) ✤ Email::Send
  • 6. Tools of the trade ✤ Mail::Box (though if you don’t mind complexity, it’s really solid) ✤ Email::Send ✤ Email::Valid
  • 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. 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. 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
  • 11. Email::Valid ✤ Simple email address validation
  • 12. Email::Valid ✤ Simple email address validation ✤ One (class) method: address($address)
  • 13. Email::Valid ✤ Simple email address validation ✤ One (class) method: address($address) ✤ Call method
  • 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.
  • 25. Email::Address Methods ✤ address ✤ get/set actual address, e.g. foo@bar.com
  • 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
  • 30. Email::MIME -- Why? ✤ It’s simple
  • 31. Email::MIME -- Why? ✤ It’s simple ✤ It’s easy
  • 32. Email::MIME -- Why? ✤ It’s simple ✤ It’s easy ✤ It works
  • 33. Email::MIME -- Why? ✤ It’s simple ✤ It’s easy ✤ It works ✤ ... but really it just sucks the least.
  • 34. So how do you use it? This won’t hurt a bit.
  • 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

  1. \n
  2. \n
  3. 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&amp;#x2019;s a number of things you need to do to make sure you&amp;#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
  4. 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&amp;#x2019;s a number of things you need to do to make sure you&amp;#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
  5. 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&amp;#x2019;s a number of things you need to do to make sure you&amp;#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
  6. 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&amp;#x2019;s a number of things you need to do to make sure you&amp;#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
  7. 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&amp;#x2019;s a number of things you need to do to make sure you&amp;#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
  8. 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&amp;#x2019;s a number of things you need to do to make sure you&amp;#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
  9. 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&amp;#x2019;s simple and it does one thing. It&amp;#x2019;s got one class method and that&amp;#x2019;s all it needs.\n
  10. 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&amp;#x2019;s simple and it does one thing. It&amp;#x2019;s got one class method and that&amp;#x2019;s all it needs.\n
  11. 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&amp;#x2019;s simple and it does one thing. It&amp;#x2019;s got one class method and that&amp;#x2019;s all it needs.\n
  12. 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&amp;#x2019;s simple and it does one thing. It&amp;#x2019;s got one class method and that&amp;#x2019;s all it needs.\n
  13. 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&amp;#x2019;s simple and it does one thing. It&amp;#x2019;s got one class method and that&amp;#x2019;s all it needs.\n
  14. 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&amp;#x2019;s simple and it does one thing. It&amp;#x2019;s got one class method and that&amp;#x2019;s all it needs.\n
  15. 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&amp;#x2019;s simple and it does one thing. It&amp;#x2019;s got one class method and that&amp;#x2019;s all it needs.\n
  16. \n
  17. 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&amp;#x2019;t care about like the phrase or the comment.\n
  18. 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&amp;#x2019;t care about like the phrase or the comment.\n
  19. 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&amp;#x2019;t care about like the phrase or the comment.\n
  20. 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&amp;#x2019;t care about like the phrase or the comment.\n
  21. 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&amp;#x2019;t care about like the phrase or the comment.\n
  22. 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&amp;#x2019;t care about like the phrase or the comment.\n
  23. Just a sampling of the methods, again. \n
  24. Just a sampling of the methods, again. \n
  25. Just a sampling of the methods, again. \n
  26. \n
  27. There&amp;#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&amp;#x2019;re focusing on Email::MIME because it works with other tools and it&amp;#x2019;s pretty solid overall.\n
  28. There&amp;#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&amp;#x2019;re focusing on Email::MIME because it works with other tools and it&amp;#x2019;s pretty solid overall.\n
  29. There&amp;#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&amp;#x2019;re focusing on Email::MIME because it works with other tools and it&amp;#x2019;s pretty solid overall.\n
  30. There&amp;#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&amp;#x2019;re focusing on Email::MIME because it works with other tools and it&amp;#x2019;s pretty solid overall.\n
  31. \n
  32. These are some of the common methods that you&amp;#x2019;ll see in every day use. I&amp;#x2019;ve only got 20 minutes to cover a number of tools. RTFM.\n
  33. These are some of the common methods that you&amp;#x2019;ll see in every day use. I&amp;#x2019;ve only got 20 minutes to cover a number of tools. RTFM.\n
  34. These are some of the common methods that you&amp;#x2019;ll see in every day use. I&amp;#x2019;ve only got 20 minutes to cover a number of tools. RTFM.\n
  35. These are some of the common methods that you&amp;#x2019;ll see in every day use. I&amp;#x2019;ve only got 20 minutes to cover a number of tools. RTFM.\n
  36. These are some of the common methods that you&amp;#x2019;ll see in every day use. I&amp;#x2019;ve only got 20 minutes to cover a number of tools. RTFM.\n
  37. 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&amp;#x2019;re specifying characters, not bytes.\n\nYou are working in characters, right?\n\nThen there&amp;#x2019;s the body parameter. It&amp;#x2019;s pretty simple.\n\nSo you might be wondering what strictures is. Basically it&amp;#x2019;s strict with a bunch of other stuff added in.\n
  38. This looks more or less like what you&amp;#x2019;d expect. To keep the example simple we didn&amp;#x2019;t specify a message-id. A sane MTA will append one when sending a message without one, the same for the date. \n
  39. Okay, awesome. We&amp;#x2019;ve made sure our email addresses are valid and deliverable. We&amp;#x2019;ve analyzed them to do any introspection or policy enforcement on them. We&amp;#x2019;ve created a message and filled it with a complicated DAG of attachments. But this is all academic if we can&amp;#x2019;t actually send the message. Here&amp;#x2019;s where Email::Sender comes into play.\n
  40. Okay, awesome. We&amp;#x2019;ve made sure our email addresses are valid and deliverable. We&amp;#x2019;ve analyzed them to do any introspection or policy enforcement on them. We&amp;#x2019;ve created a message and filled it with a complicated DAG of attachments. But this is all academic if we can&amp;#x2019;t actually send the message. Here&amp;#x2019;s where Email::Sender comes into play.\n
  41. Okay, awesome. We&amp;#x2019;ve made sure our email addresses are valid and deliverable. We&amp;#x2019;ve analyzed them to do any introspection or policy enforcement on them. We&amp;#x2019;ve created a message and filled it with a complicated DAG of attachments. But this is all academic if we can&amp;#x2019;t actually send the message. Here&amp;#x2019;s where Email::Sender comes into play.\n
  42. Okay, awesome. We&amp;#x2019;ve made sure our email addresses are valid and deliverable. We&amp;#x2019;ve analyzed them to do any introspection or policy enforcement on them. We&amp;#x2019;ve created a message and filled it with a complicated DAG of attachments. But this is all academic if we can&amp;#x2019;t actually send the message. Here&amp;#x2019;s where Email::Sender comes into play.\n
  43. Okay, awesome. We&amp;#x2019;ve made sure our email addresses are valid and deliverable. We&amp;#x2019;ve analyzed them to do any introspection or policy enforcement on them. We&amp;#x2019;ve created a message and filled it with a complicated DAG of attachments. But this is all academic if we can&amp;#x2019;t actually send the message. Here&amp;#x2019;s where Email::Sender comes into play.\n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n