#!/usr/local/bin/perl use IO::Socket; use CGI::Carp qw(carpout fatalsToBrowser); # 送信元アドレス $eml_from = 'xxx@xxx.xxx'; # 送信先アドレス $eml_to = 'xxx@xxx.xxx'; # SMTPサーバ $serv = "127.0.0.1"; # ポート番号(通常は25) $port = 25; # バイナリモード(Windows対策) binmode(STDOUT); # 接続 $sock = IO::Socket::INET->new( Proto => "tcp", PeerAddr => $serv, PeerPort => $port, ) || die "接続失敗"; # バッファリングしないようにする $sock->autoflush(1); &res(220, 'flush失敗'); # HELOコマンド print $sock "HELO $server\r\n"; &res(250, 'HELO失敗'); # MAILコマンド print $sock "MAIL FROM: $eml_from\r\n"; &res(250, 'MAIL失敗'); # RCPTコマンド print $sock "RCPT TO: $eml_to\r\n"; &res("250|251", 'RCPT失敗'); # DATAコマンド print $sock "DATA\r\n"; &res(354, 'DATA失敗'); # 本文 $body = ''; $body .= "To: $eml_to\r\n"; $body .= "From: $eml_from\r\n"; $body .= "Subject: test mail\r\n\r\n"; $body .= "This is a test mail.\r\n"; $body .= "."; # 本文送信 print $sock "$body\r\n"; &res(250, '本文送信失敗'); # QUITコマンド print $sock "QUIT\r\n"; close $sock; # HTML表示 print < 送信OK EOM #------------------------------------------------- # レスポンスチェック #------------------------------------------------- sub res { my ($str,$err) = @_; my $res = <$sock>; if ($res !~ /^$str/) { die "$err : $res\n"; } }