メール送信
Net::SMTPモジュールを使うと簡単です。
日本語部分の処理にはJcodeモジュールを使っています。
use strict;
use warnings;
use Jcode;
use Net::SMTP;
my $server = 'mail.example.com'; # メールサーバ
my $debug = 0; # デバッグフラグ
my $timeout = 10; # タイムアウト時間
my $sender = 'sender@example.com'; # 送信者
my $to = 'recipient@example.com'; # 宛先
my $smtp = Net::SMTP->new(
$server,
Debug => $debug,
Timeout => $timeout) or die $@;
$smtp->mail($sender);
$smtp->to($to);
$smtp->data();
my $subject = get_subject();
$subject = Jcode->new($subject)->mime_encode;
my $header = get_header($sender, $to, $subject);
my $body = get_body();
$body = Jcode->new($body)->iso_2022_jp;
$smtp->datasend($header);
$smtp->datasend("\n");
$smtp->datasend($body);
$smtp->quit;
exit;
# メール本文
sub get_body {
my $body = <<'EOD';
メールのテストです。
日本語で送ってみます。
EOD
return $body;
}
# サブジェクト
sub get_subject {
return 'テストメール';
}
# メールヘッダ
sub get_header {
my $from = shift;
my $to = shift;
my $subject = shift;
my $header =<<EOD;
From: $from
To: $to
Subject: $subject
Content-Type: text/plain; charset=iso-2022-jp
Content-Transfer-Encoding: 7bit
EOD
return $header;
}
関連項目
・添付ファイル付きのメールを送信する・メールサブジェクトの作成
・メール受信
