MailExample/bin/mail_example

57 lines
1.5 KiB
Perl

#!/usr/bin/env perl
use v5.30.0;
use strict;
use warnings;
use Getopt::Long::Descriptive;
use Path::Tiny;
use Mail::Example;
sub get_file;
my ( $opt, $usage ) = describe_options(
'mail_example %o',
[
'user|u=s', 'The user to use while sending the mail.', { required => 1 }
],
[ 'port|p=i', 'The port to use for smtp server.', { required => 1 } ],
[ 'host|H=s', 'The host to use for smtp server.', { required => 1 } ],
[
'password|P=s',
'The password to use for smtp server.',
{ required => 1 }
],
[ 'subject|s=s', 'The subject of the message.', { required => 1 } ],
[ 'to|t=s', 'The address of the receiver.', { required => 1 } ],
[ 'text_body_file|b=s', 'The text body of the message.', { required => 1 } ],
[ 'html_body_file|B=s', 'The html body of the message.', { required => 1 } ],
[ 'help|h', 'Print this help', { shortcircuit => 1 } ],
);
print( $usage->text ), exit if $opt->help;
my $html = get_file $opt->html_body_file;
my $text = get_file $opt->text_body_file;
my $mailer = Mail::Example->new(
config => {
sasl_username => $opt->user,
sasl_password => $opt->password,
smtp_host => $opt->host,
smtp_port => $opt->port,
}
);
$mailer->sendmail(
text => $text,
html => $html,
to => $opt->to,
subject => $opt->subject
);
sub get_file {
my $file = shift;
return path($file)->slurp_utf8;
}