Skip to content

Instantly share code, notes, and snippets.

@lopnor
Created August 19, 2010 05:52
Show Gist options
  • Save lopnor/537153 to your computer and use it in GitHub Desktop.
Save lopnor/537153 to your computer and use it in GitHub Desktop.
#!perl
use strict;
use warnings;
use utf8;
use Getopt::Long;
use Email::Sender::Simple;
use Email::Sender::Transport::SMTP;
use Email::MIME;
use Email::MIME::Modifier;
use Text::CSV_XS;
use Text::MicroTemplate 'build_mt';
use Encode;
use Data::Dumper;
my $smtp_port = 25;
GetOptions(
"template=s" => \(my $template_file),
"csv=s" => \(my $csv_file),
"host=s" => \(my $smtp_host),
"port=s" => \$smtp_port,
);
$csv_file or die 'csv file needed';
$template_file or die 'template file needed';
open my $fh, '<:utf8', $csv_file or die 'can not open the file';
my $csv = Text::CSV_XS->new({binary => 1}) or die 'boo!';
open my $tfh, '<:utf8', $template_file;
my $template = do {local $/; <$tfh>};
my $mt = build_mt($template);
$csv->column_names($csv->getline($fh));
while (my $recp = $csv->getline_hr($fh)) {
my $mail = Email::MIME->new($mt->($recp));
for ($mail->header_names) {
$mail->header_set(
$_,
Encode::encode('MIME-Header-ISO_2022_JP', $mail->header($_))
);
}
$mail->header_set('Content-Type' => 'text/plain; charset="ISO-2022-JP"');
$mail->header_set('Content-Transfer-Encoding' => '7bit');
$mail->body_set(Encode::encode('ISO_2022_JP', $mail->body));
Email::Sender::Simple->send(
$mail,
$smtp_host ? (
{
transport => Email::Sender::Transport::SMTP->new( {
host => $smtp_host,
port => $smtp_port,
})
}
) : ()
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment