Posted By

alfirth on 05/27/11


Tagged

sendmail


Versions (?)

simple mailer


 / Published in: Perl
 

  1. use strict; use warnings; require 5.008;
  2.  
  3. my $to = "them\@gmail.com";
  4. my $subject = "Test";
  5. my $reply_to = "you\@gmail.com";
  6. my $content = "This is a test";
  7.  
  8. #check address (from perlfaq 9)
  9. my $atom = qr{[a-zA-Z0-9_!#\$\%&'*+/=?\^`{}~|\-]+};
  10. my $dot_atom = qr{$atom(?:\.$atom)*};
  11. my $quoted = qr{"(?:\\[^
  12. ]|[^\\"])*"};
  13. my $local = qr{(?:$dot_atom|$quoted)};
  14. my $quotedpair = qr{\\[\x00-\x09\x0B-\x0c\x0e-\x7e]};
  15. my $domain_lit = qr{\[(?:$quotedpair|[\x21-\x5a\x5e-\x7e])*\]};
  16. my $domain = qr{(?:$dot_atom|$domain_lit)};
  17. my $addr_spec = qr{$local\@$domain};
  18.  
  19. die ( "Invalid To: address $to\n" ) unless ($to =~ /^${addr_spec}$/);
  20. die ( "Invalid reply_to: address $reply_to\n" ) unless ($reply_to =~ /^${addr_spec}$/);
  21.  
  22. my $sendmail = "/usr/sbin/sendmail -t -oi";
  23. my $send_to = "To: $to";
  24. my $send_reply_to = "Reply-to: $reply_to";
  25. my $send_subject = "Subject: $subject";
  26.  
  27. local $\ = "\n";
  28.  
  29. open( SENDMAIL, "|$sendmail" ) or die "Cannot open $sendmail: $!";
  30. print SENDMAIL $send_to;
  31. print SENDMAIL $send_reply_to;
  32. print SENDMAIL $send_subject;
  33. #extra \n is REQUIRED
  34. print SENDMAIL "Content-type: text/plain\n";
  35. print SENDMAIL $content;
  36. close( SENDMAIL );

Report this snippet  

You need to login to post a comment.