Skip to content

Instantly share code, notes, and snippets.

@dynax60
Created June 23, 2010 11:06
Show Gist options
  • Save dynax60/449783 to your computer and use it in GitHub Desktop.
Save dynax60/449783 to your computer and use it in GitHub Desktop.
Simple mail sender on perl :)
package MyMail;
use common::sense;
use MIME::Lite;
use MIME::Words qw(:all);
use Data::Dumper;
use Carp qw(croak);
sub _defaults {
return {
from_name => '',
to_name => '',
from => '',
to => '',
charset => 'UTF-8',
subj => '',
attach => '',
attach_type => '',
};
}
sub send_mail($) {
my $args = shift;
my $params = {
%{_defaults()},
%$args,
};
utf8::encode($_) for (values %$params);
return undef unless $params->{to} && $params->{data};
my $from = qq{"$params->{from_name}" <$params->{from}>};
my $to = qq{"$params->{to_name}" <$params->{to}>};
for (qw{ from_name to_name subj}) {
$params->{$_} = encode_mimeword( $params->{$_}, 'Q', $params->{charset} );
}
my $msg = MIME::Lite->new(
From => $from,
To => $to,
Subject => $params->{subj},
Type => 'text/plain; charset='. $params->{charset},
Encoding => 'quoted-printable',
Data => $params->{data},
);
if (length $params->{attach} && length $params->{attach_type}) {
croak "Can't read $params->{attach}" unless -r $params->{attach};
my $file = $params->{attach_filename}? $params->{attach_filename}: $params->{attach};
$file =~ s{.*/}{};
$msg->attach(
Path => $params->{attach},
Type => $params->{attach_type},
Filename => $file,
);
}
$msg->send;
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment