Skip to content

Instantly share code, notes, and snippets.

@syohex
Created January 30, 2011 08:11
Show Gist options
  • Save syohex/802679 to your computer and use it in GitHub Desktop.
Save syohex/802679 to your computer and use it in GitHub Desktop.
Perlで指定したファイルをメールで送信する
#!/bin/sh
SEND_FILE=$1
if [ "$SEND_FILE" = "" ]
then
echo "Usage $0 filename"
exit 1
fi
SUBJECT="`date +"%Y%m%d"` $SEND_FILE"
SMTP_SERVER=SMTP_SERVER_ADDRESS
SMTP_USER=YOURNAME
SMTP_PASS=YOUR_PASSWORD
PORT=PORT_NUMBER
FROM=FROM_EMAIL_ADDRESS
TO=TO_EMAIL_ADDRESS
send_mail.pl --smtp $SMTP_SERVER --ssl --username $SMTP_USER \
--password $SMTP_PASS --port $PORT \
--from $FROM --to $TO \
--subject "$SUBJECT" $1
#!/usr/bin/env perl
package MyApp::sendmail;
use 5.10.0;
use Mouse;
use Encode;
use Encode::Guess qw/utf8 euc-jp/;
with 'MouseX::Getopt';
has 'smtp' => (
is => "rw",
isa => "Str",
required => 1,
documentation => 'SMTP server address',
);
has 'username' => (
is => 'rw',
isa => 'Str',
required => 1,
documentation => 'SMTP User name',
);
has 'password' => (
is => 'rw',
isa => 'Str',
required => 1,
documentation => 'SMTP password',
);
has 'ssl' => (
is => 'rw',
isa => 'Bool',
documentation => 'Use SSL connection',
);
has 'port' => (
is => 'rw',
isa => 'Int',
default => 25,
documentation => 'SMTP port number default is 25',
);
has 'from' => (
is => 'rw',
isa => 'Str',
required => 1,
documentation => q(From Header's Email address),
);
has 'to' => (
is => 'rw',
isa => 'Str',
required => 1,
documentation => q(To Header's Email address),
);
has 'subject' => (
is => 'rw',
isa => 'Str',
default => 'No Title',
documentation => q(Subject of sending mail),
);
use Email::Valid;
use Carp;
sub BUILD {
my $self = shift;
unless (Email::Valid->address($self->from)) {
Carp::croak("Invalid 'From' Email Address: ", $self->from, "\n");
}
unless (Email::Valid->address($self->to)) {
Carp::croak("Invalid 'To' Email Address: ", $self->to, "\n");
}
unless (defined $self->subject) {
Carp::carp("Subject is not specified\n");
}
my $decoded = decode("Guess", $self->subject);
$self->subject($decoded);
}
__PACKAGE__->meta->make_immutable;
no Mouse;
use Data::Validator;
use Email::Sender::Simple qw(sendmail);
use Email::Simple;
use Email::Sender::Transport::SMTP;
use Email::Simple::Creator;
use Data::Recursive::Encode;
sub send_mail {
state $rule = Data::Validator->new(
file => { isa => 'Str', xor => [qw(string)]},
string => {isa => 'Str', xor => [qw(file)]},
)->with('Method');
my ($self, $args) = $rule->validate(@_);
my $body;
if (defined $args->{file}) {
open my $fh, "<", $args->{file},
or Carp::croak("Can't open ", $args->{file}, "\n");
$body = do {
local $/;
<$fh>;
};
close $fh;
} else {
$body = $args->{string};
}
my $decoded_body = decode("Guess", $body);
my $transport = $self->_set_smtp_conf();
my $email = $self->_create_email(\$decoded_body);
eval {
sendmail($email, {transport => $transport});
};
if ($@) {
Carp::croak("Error: Can't send mail\n");
}
$self->_print_message;
}
sub _print_message {
my $self = shift;
print "Sucess: send mail\n";
printf "From: %s\nTo: %s\nSubject: %s\n",
$self->from, $self->to, $self->subject;
}
sub _create_email {
my ($self, $body_ref) = @_;
my $email = Email::Simple->create(
header => Data::Recursive::Encode->encode(
'MIME-Header-ISO_2022_JP' => [
To => $self->to,
From => $self->from,
Subject => $self->subject,
]
),
body => encode('iso-2022-jp', ${$body_ref}),
attributes => {
content_type => 'text/plain',
charset => 'ISO-2022-JP',
encoding => '7bit',
},
);
}
sub _set_smtp_conf {
my $self = shift;
my $transport = Email::Sender::Transport::SMTP->new({
sasl_username => $self->username,
sasl_password => $self->password,
host => $self->smtp,
port => $self->port,
ssl => $self->ssl,
});
return $transport;
}
package main;
use strict;
use warnings;
unless (caller) {
my $app = MyApp::sendmail->new_with_options();
my $file = shift @{$app->extra_argv};
die "Please specify send file\n" unless defined $file;
$app->send_mail(file => $file);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment