Guest User

Untitled

a guest
Mar 16th, 2018
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use Config::Pit;
  7. use Email::MIME;
  8. use Email::Sender::Simple qw(try_to_sendmail);
  9. use Email::Sender::Transport::SMTP;
  10. use Encode;
  11. use FileHandle;
  12. use Media::Type::Simple;
  13.  
  14. my $to = shift;
  15.  
  16. &main;
  17. exit;
  18.  
  19. sub main {
  20. my ($subject, $body, @attachments);
  21.  
  22. while (<STDIN>) {
  23. chomp;
  24.  
  25. if (!-e $_) {
  26. $subject = $_;
  27. last;
  28. } else {
  29. push @attachments, $_;
  30. }
  31. }
  32.  
  33. ($body = join '', <STDIN>) =~ s/^\s*//;
  34.  
  35. my @parts = (
  36. Email::MIME->create(
  37. attributes => {
  38. content_type => "text/plain",
  39. charset => "UTF-8",
  40. encoding => "base64",
  41. },
  42. body_str => decode("utf8", $body),
  43. ),
  44. );
  45.  
  46. foreach my $file (@attachments) {
  47. my $fh = FileHandle->new($file);
  48. binmode $fh;
  49. local $/;
  50. my $content = <$fh>;
  51. push @parts, Email::MIME->create(
  52. attributes => {
  53. content_type => type_from_ext((split(/\./, $file))[-1]),
  54. name => (split(/\\|\//, $file))[-1],
  55. filename => (split(/\\|\//, $file))[-1],
  56. encoding => "base64",
  57. disposition => "attachment",
  58. },
  59. body => $content,
  60. );
  61. }
  62.  
  63. my $config = Config::Pit::get("google.com");
  64.  
  65. my $email = Email::MIME->create(
  66. header_str => [
  67. From => $config->{'username'} . '@google.com',
  68. To => $to,
  69. Subject => decode("utf8", $subject),
  70. ],
  71. parts => [ @parts ],
  72. );
  73.  
  74. my $transport = Email::Sender::Transport::SMTP->new({
  75. host => 'smtp.gmail.com',
  76. ssl => 1,
  77. port => 465,
  78. sasl_username => $config->{'username'},
  79. sasl_password => $config->{'password'},
  80. });
  81.  
  82. try_to_sendmail($email, {
  83. transport => $transport
  84. });
  85.  
  86. print join("\n", @attachments, $subject, "", $body);
  87. }
Add Comment
Please, Sign In to add comment