Skip to content

Instantly share code, notes, and snippets.

@Cside
Created January 30, 2011 04:03
Show Gist options
  • Save Cside/802512 to your computer and use it in GitHub Desktop.
Save Cside/802512 to your computer and use it in GitHub Desktop.
Gmailでメールを送信するクラス
package GmailSender;
use strict;
use warnings;
use Class::Accessor::Lite (
new => 1,
rw => [ qw(
username
password
from
to
subject
body
)],
);
use Encode;
use utf8;
use Carp;
use Try::Tiny;
use Email::Sender::Simple qw(sendmail);
use Email::MIME;
use Email::MIME::Creator;
use Email::Sender::Transport::SMTP::TLS;
sub send {
my ($self) = @_;
my $sender = Email::Sender::Transport::SMTP::TLS->new(
host => 'smtp.gmail.com',
port => 587,
username => $self->username,
password => $self->password,
);
my $email = Email::MIME->create(
header => [
From => mine_header_encode($self->from),
To => mine_header_encode($self->to),
Subject => mine_header_encode($self->subject),
],
attributes => {
content_type => 'text/plain',
charset => 'ISO-2022-JP',
encoding => '7bit',
},
body => encode('iso-2022-jp' => $self->body),
);
try { sendmail($email, {transport => $sender}); }
catch { croak "sending email failed : " . $_->message; };
}
sub mine_header_encode {
my $str = shift;
encode('MIME-Header-ISO_2022_JP' => $str) if $str;
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment