Skip to content

Instantly share code, notes, and snippets.

@Cside
Created December 19, 2011 15:50
Show Gist options
  • Save Cside/1497740 to your computer and use it in GitHub Desktop.
Save Cside/1497740 to your computer and use it in GitHub Desktop.
send email to gmail-adress with attachment.
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use Path::Class qw/file/;
use Encode;
use Email::Sender::Simple qw/try_to_sendmail/;
use Email::MIME;
use Email::MIME::Creator;
use Email::Sender::Transport::SMTP::TLS;
my $username = 'gmail-user-name';
my $password = 'gmail-password';
my $from = "$username\@gmail.com";
my $to = '[email protected]';
my $subject = 'subject';
my $body = 'body';
my $file = file('/path/to/attachment');
my $email = Email::MIME->create(
header => [
From => $from,
To => $to,
Subject => Encode::encode('MIME-Header-ISO_2022_JP', $subject),
],
parts => [
# メール本文
Email::MIME->create(
attributes => {
content_type => 'text/plain',
charset => 'iso-2022-jp',
encoding => '7bit',
},
body_str => $body,
),
# 添付ファイル
Email::MIME->create(
attributes => {
filename => $file->basename,
encoding => 'base64',
disposition => "attachment",
},
body => scalar $file->slurp,
),
],
);
try_to_sendmail(
$email,
{
transport => Email::Sender::Transport::SMTP::TLS->new(
host => 'smtp.gmail.com',
port => 587,
username => $username,
password => $password,
)
}
) or do { ... };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment