Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use Config::Pit;
- use Email::MIME;
- use Email::Sender::Simple qw(try_to_sendmail);
- use Email::Sender::Transport::SMTP;
- use Encode;
- use FileHandle;
- use Media::Type::Simple;
- my $to = shift;
- &main;
- exit;
- sub main {
- my ($subject, $body, @attachments);
- while (<STDIN>) {
- chomp;
- if (!-e $_) {
- $subject = $_;
- last;
- } else {
- push @attachments, $_;
- }
- }
- ($body = join '', <STDIN>) =~ s/^\s*//;
- my @parts = (
- Email::MIME->create(
- attributes => {
- content_type => "text/plain",
- charset => "UTF-8",
- encoding => "base64",
- },
- body_str => decode("utf8", $body),
- ),
- );
- foreach my $file (@attachments) {
- my $fh = FileHandle->new($file);
- binmode $fh;
- local $/;
- my $content = <$fh>;
- push @parts, Email::MIME->create(
- attributes => {
- content_type => type_from_ext((split(/\./, $file))[-1]),
- name => (split(/\\|\//, $file))[-1],
- filename => (split(/\\|\//, $file))[-1],
- encoding => "base64",
- disposition => "attachment",
- },
- body => $content,
- );
- }
- my $config = Config::Pit::get("google.com");
- my $email = Email::MIME->create(
- header_str => [
- From => $config->{'username'} . '@google.com',
- To => $to,
- Subject => decode("utf8", $subject),
- ],
- parts => [ @parts ],
- );
- my $transport = Email::Sender::Transport::SMTP->new({
- host => 'smtp.gmail.com',
- ssl => 1,
- port => 465,
- sasl_username => $config->{'username'},
- sasl_password => $config->{'password'},
- });
- try_to_sendmail($email, {
- transport => $transport
- });
- print join("\n", @attachments, $subject, "", $body);
- }
Add Comment
Please, Sign In to add comment