#!/usr/bin/perl # $Id$ # Copyright(c) caldron.jp 2007 All Rights Reserved. # This script is written by/for Japanese. # 必要な環境・ソフトウェア # * perl 5.8.8 以降 # * Template Toolkit 2.15 以降 # * Webサーバ # * 送信依頼を行えるSMTPサーバ use strict; use utf8; use CGI; use Template; use Net::SMTP; use Encode; use Encode::Guess; # ==環境設定=================================================== #送信依頼するメールサーバ my $server = 'mail.caldron.jp'; #送信先メールアドレス my $to = 'info@caldron.jp'; #送信するメッセージの件名 my $subject = 'Contact from WWW'; #SMTP-AUTHで認証するユーザ名(空にすると認証しない) my $user = ''; #SMTP-AUTHのパスワード my $passwd = ''; # ============================================================= my $conf ={ server => $server, to => $to, subject => encode('utf-8',decode('utf-8',$subject)), user => $user, passwd => $passwd }; my $mode = 0; # 処理モード 0:作成モード、1:送信モード # 値取得 my $query = CGI->new; my $params = {}; if ($query->param()) { # binmode STDIN, ':bytes'; foreach my $key ($query->param()) { # print STDERR "DEBUG: decode $key = ".$query->param($key); if ($query->param($key)) { $$params{$key} = encode('utf-8', decode('Guess', $query->param($key))); } else { $$params{$key} = ''; } } print STDERR "paramaters are stored.\n"; # 入力値(メールアドレス)チェック print STDERR "email(".$$params{'email'}.")\n"; if (&valid_address($$params{'email'})) { $mode = 1; } else { $mode = 0; $$params{'notice'} = encode('utf-8', 'メールアドレスを適切に入力してください。'); } } # テンプレート初期化 my $template = Template->new; my $tmp; my $result; if ($mode == 0) { # 入力モード my $stat = $template->process ( 'write.html', $params, \$tmp, ); $result = decode('utf-8', $tmp); if ($stat == 0) { &template_error; } print "Content-type: text/html\n\n"; print encode('utf-8', $result); exit(0); } # メッセージ本文生成 my $stat = $template->process ( 'message.tpl', $params, \$tmp, ); if ($stat == 0) { } my $message = decode('utf-8', $tmp); if (&send($conf, $params, $message)) { # 送信成功 $tmp = ''; $template->process ( 'result.html', $params, \$tmp, ); $result = decode('utf-8', $tmp); print "Content-type: text/html\n\n"; print encode('utf-8', $result); exit(0); } else { # 送信失敗 print STDERR "Send Failed.\n"; $$params{'notice'} = encode('utf-8', 'メールの送信に失敗しました。'); $tmp = ''; my $stat = $template->process ( 'write.html', $params, \$tmp, ); $result = decode('utf-8', $tmp); if ($stat == 0) { &template_error; } print "Content-type: text/html\n\n"; print encode('utf-8', $result); exit(0); } exit 0; # end of script # メールを送信する sub send { my $conf = shift @_; my $params = shift @_; my $message = shift @_; # オブジェクト作成 my $smtp = Net::SMTP->new($$conf{'server'}); if (! $smtp) { print STDERR "SMTP Connect Error.\n"; return $smtp; } my $stat; if ($user != ''){ $stat = $smtp->auth($$conf{'user'},$$conf{'passwd'}); if (! $stat) { # 認証失敗 print STDERR "SMTP Authentication failed.\n"; return $stat; } } $stat = 1; # 送信元の指定 $stat *= $smtp->mail($$params{'email'}); # 宛先の指定 $stat *= $smtp->to($$conf{'to'}); # ヘッダ my $date = &date; $stat *= $smtp->data(); $stat *= $smtp->datasend("Date:$date\n"); $stat *= $smtp->datasend("From:$$params{'email'}\n"); $stat *= $smtp->datasend("To:$$conf{'to'}\n"); $stat *= $smtp->datasend("Subject: $$conf{'subject'}\n"); $stat *= $smtp->datasend("Content-Transfer-Encoding: 7bit\n"); $stat *= $smtp->datasend("Content-Type: text/plain;charset=\"ISO-2022-JP\"\n\n"); # 本文 $stat *= $smtp->datasend(encode('iso-2022-jp',$message)); # データの終わり、メール送信 $stat *= $smtp->dataend(); # SMTP接続の終了 $stat *= $smtp->quit; return $stat; } # 日付を取得・生成する sub date { $ENV{'TZ'} = "JST-9"; my ($sec,$min,$hour,$mday,$mon,$year,$wday) = localtime(time); my @week = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat'); my @month = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'); my $d = sprintf("%s, %d %s %04d %02d:%02d:%02d +0900 (JST)", $week[$wday],$mday,$month[$mon],$year+1900,$hour,$min,$sec); return $d; } # メールアドレスチェック sub valid_address { my ($address) = @_; if ($address =~ /^[^@]+@[^.]+\..+/ ) { return 1; } else { return 0; } } # テンプレートのエラー時 sub template_error { my $html = << "EOS"; Content-type: text/html
テンプレートを適用しようとした際、エラーが発生しました。
設定を確認してください。
EOS print encode('utf-8', $html); exit (0); }