Mail-Sender-Easy

 view release on metacpan or  search on metacpan

Easy.pm  view on Meta::CPAN

package Mail::Sender::Easy;

use strict;
use warnings;

use Carp;
use Mail::Sender;
use File::Spec;

my $hostname_code = sub {
    require POSIX;
    return (POSIX::uname())[1];  
};

if(!eval {
        require Sys::Hostname::FQDN;
        $hostname_code = \&Sys::Hostname::FQDN::fqdn;
        return 1;
}){
    eval {
       require Net::Domain;
       $hostname_code = \&Net::Domain::hostfqdn;
    }
}

require Exporter;
our @ISA       = qw(Exporter);
our @EXPORT_OK = qw(email);

use version;our $VERSION = qv('0.0.5');

sub email { Mail::Sender->new()->easy(shift()); }

sub Mail::Sender::easy {
    my($sndr, $mail_ref) = @_;

    my $text        = delete $mail_ref->{'_text'};
    my $html        = delete $mail_ref->{'_html'};
    my $attachments = delete $mail_ref->{'_attachments'};

    my $text_info   = ref $mail_ref->{'_text_info'} eq 'HASH' 
                      ? delete $mail_ref->{'_text_info'} : {};
    delete $mail_ref->{'_text_info'} if exists $mail_ref->{'_text_info'};
    delete $text_info->{$_} for qw(ctype disposition msg);

    my $html_info   = ref $mail_ref->{'_html_info'} eq 'HASH'        
                      ? delete $mail_ref->{'_html_info'} : {};
    delete $mail_ref->{'_html_info'} if exists $mail_ref->{'_html_info'};
    delete $html_info->{$_} for qw(ctype disposition msg);

    my $time = time;
    my $user = $^O eq 'MSWin32' ? "(Windows: $<)" : getpwuid($<);
    my $eusr = $^O eq 'MSWin32' ? "(Windows: $>)" : getpwuid($>);
    my $file = File::Spec->rel2abs($0);
    my $host = $hostname_code->();
 
    my @siteheaders = (
        qq{X-Mailer: use SimpleMood; - Sent via the email() function or easy() method of Mail/Sender/Easy.pm and/or SimpleMood.pm both by Daniel Muey.},
        qq{X-Mailer: Sent via $file ($0) on $host by uid $< ($user) / euid $> ($eusr) at $time (unix epoch)},
    );
    push @siteheaders, qq(X-Mailer: SMTP Auth provided by (object data) $sndr->{'authid'}) if $sndr->{'authid'};
    push @siteheaders, qq(X-Mailer: SMTP Auth provided by (hashref arg) $mail_ref->{'authid'}) if $mail_ref->{'authid'};

    croak q{You must specify the "_text" key.} if !defined $text;

    eval {
        local $Mail::Sender::SITE_HEADERS = join("\015\012", @siteheaders) || '';
        
        if($html) {
            $mail_ref->{'multipart'} = 'mixed';
            $sndr->OpenMultipart($mail_ref);
            $sndr->Part({
                'ctype' => 'multipart/alternative'
            });

            $sndr->Part({
                %{ $text_info }, 
                'ctype'       => 'text/plain', 
                'disposition' => 'NONE', 
#               'msg'         => "$text$CRLF" 
            });
            $sndr->SendLineEnc($text);

            $sndr->Part({
                %{ $html_info },
                'ctype'       => 'text/html',  
                'disposition' => 'NONE', 
#               'msg'         => "$html$CRLF" 
            });
            $sndr->SendLineEnc($html);

            $sndr->EndPart('multipart/alternative');
        } 
        elsif(!$html && $attachments) {
            $sndr->OpenMultipart($mail_ref);
            $sndr->Body({
                %{ $text_info },
                'ctype'       => 'text/plain',
                'disposition' => 'NONE',
#               'msg'   => $text,
            });
            $sndr->SendLineEnc($text);
        } 
        else {
            $sndr->Open({
                %{ $text_info },
                %{ $mail_ref },
            });
            $sndr->SendLineEnc($text);
        }

        if(defined $attachments && ref $attachments eq 'HASH') {
            for my $attach (keys %{ $attachments }) {



( run in 2.567 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )