0
0

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: Starttls でメールの送信

mail_ex01.pl
# ! /usr/bin/perl
#
#	mail_ex01.pl
#
#					Apr/15/2021
#
# ----------------------------------------------------------------
use Dotenv;
use Net::SMTP;
#
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 $to_mail = %$env{'TO'};
my $from_mail = %$env{'FROM'};
my $smtp = Net::SMTP->new($mailhost, Port => $mailport);
#
$smtp->auth($mail_username, $mail_password);
$smtp->mail($from_mail);
$smtp->to($to_mail);

my $subject = "Subject: Hello Apr/15/2021 PM 13:47 from Perl\n";

my $header = << "MAILHEADER_1";
From: $from_mail
To: $to_mail
Subject: $subject
Mime-Version: 1.0
Content-Type: text/plain; charset = "ISO-2022-JP"
Content-Transfer-Encoding: 7bit

MAILHEADER_1

$smtp->data();
$smtp->datasend("$header\n");

my $name = "夏目漱石";
my $store = "小山支店";
my $price = "1,200";
my $url = "https://test.example.com";

my $message =<<EOF;
Apr/15/2021 PM 13:46
Hello! This E-mail is automatically sent from Perl program.
こんにちは。これは、テストのプログラムです。
Please check the item
	[NAME]: $name
	[STORE]: $store
	[PRICE]: $price
	[URL]: $url
EOF
#
$smtp->datasend("$message\n");
$smtp->dataend();

$smtp->quit;
#
print STDERR "*** end ***\n";
#
# ----------------------------------------------------------------
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?