0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Perl: メールの送信 (添付ファイル付き)

Last updated at Posted at 2021-04-15

こちらのプログラムを参考にしました。
(Perl) Net::SMTPSを使ってメール送信

Ubuntu 20.10 で必要なライブラリーのインストール

sudo apt install libemail-mime-perl
sudo apt install libpath-class-perl

linux_logo.gif というファイルを添付するサンプルです。

mail_attache.pl
# ! /usr/bin/perl
#
#	mail_attache.pl
#
#						Apr/15/2021
#
# ---------------------------------------------------------------- 
use warnings;
use strict;
use utf8;
use Encode qw(encode);
use Net::SMTP;
use Email::MIME;
use Email::MIME::Creator;
use Path::Class;
use Dotenv;
 
# ---------------------------------------------------------------- 
print STDERR "*** start ***\n";

my $env = Dotenv->parse('.env');
print STDERR %$env{'SERVER'} . "\n";
print STDERR %$env{'PORT'} . "\n";
#
my $mailhost = %$env{'SERVER'};
my $mailport = %$env{'PORT'};
my $mail_username = %$env{'FROM'};
my $mail_password = %$env{'PASSWORD'};

my $from_mail = %$env{'FROM'};
my $to_mail = %$env{'TO'};


my $subject = 'test テストApr/15/2021 PM 14:51';
my $body =
    "年齢からいえば、関羽がいちばん年上であり、次が劉備、"
  . "その次が張飛という順になるのであるが、"
  . "義約のうえの義兄弟だから年順をふむ必要はないとあって、"
  . "「長兄には、どうか、あなたがなって下さい。\n"
  . "それでないと、張飛の我ままにも、おさえが利きませんから」と、関羽がいった。";
 
my $mail = Email::MIME->create(
	'header' => [
        'From'    => encode( 'MIME-Header-ISO_2022_JP', $from_mail ),
        'To'      => encode( 'MIME-Header-ISO_2022_JP', $to_mail ),
        'Subject' => encode( 'MIME-Header-ISO_2022_JP', $subject ),
    ],
parts => [
	Email::MIME->create(
		'attributes' => {
                      'content_type' => 'text/plain',
                      'charset'      => 'ISO-2022-JP',
                      'encoding'     => '7bit',
    },
    'body' => encode( 'iso-2022-jp', $body ),
),
Email::MIME->create(
	'attributes' => {
		'content_type' => 'image/gif',
		'name'         => 'attached image name',
		'filename'     => 'linux_logo.gif',
		'encoding'     => 'base64',
		'disposition'  => 'attachment',
		},
		'body' => scalar Path::Class::file('linux_logo.gif')->slurp,
               ),
    ],
);

 
eval {
    my $smtp = Net::SMTP->new(
	$mailhost,
	Port  => $mailport,
	doSSL => 'starttls',
#	Debug => 1
    );
 
	$smtp->auth($mail_username, $mail_password, 'CRAM-MD5' );
	$smtp->mail($from_mail);
	$smtp->recipient( $to_mail, { SkipBad => 1 } );
	$smtp->data();
	$smtp->datasend( $mail->as_string() );
	$smtp->dataend();
	$smtp->quit;
};

if ($@) {
    die "ERROR : $@\n";
}

print STDERR "*** end ***\n";
# ---------------------------------------------------------------- 
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?