May 29, 2008

perl - 取得網頁填寫的資料,並寄送Email

#!/usr/bin/perl
$recipient1 = 'ttt@com';
$mail_from = "ttt\@com";
$subject = "客戶回函";
# Print out a content-type for HTTP/1.0 compatibility
print "Content-type: text/html\n\n";
# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
# Split the name-value pairs
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}

&mail_user;

$mail_to = $FORM{'b5'};
open (MAIL, "|/usr/sbin/sendmail -f '$mail_to' '$recipient1' ")||die "sendmail: $!\n";
print MAIL<<"EOM";
Subject:客戶回函
MIMI-Version:1.0
Content-type:text/plain;charset=big5
這是客戶回函
個人簡介:
---------------------------------------------------------------------
名字 : $FORM{'a1'}
職稱 : $FORM{'a2'}
公司名 : $FORM{'b1'}
地址 : $FORM{'b2'}
電話 : $FORM{'b3'}
傳真 : $FORM{'b4'}
E-Mail : $FORM{'b5'}
EOM
close (MAIL);
print "<body background=/gif/bookback.gif \n";
print " text=\"#000000\" link=\"#0000ff\" vlink=\"#ff0000\" \n";
print "alink=\"#00ff000\">\n";
print "<title>客戶回函</title>\n";
print "<body><center>";
print "<h5><font face=\"Arial\"><a href=\"http://www.simplo.com.tw/\"><strong></strong></a><br>";
print "<hr>\n";
print "</center>";

print " </p>\n";
print " <blockquote>\n";
print "<font size=+1>個人簡介</font><br>\n";
print "<hr><br>\n";
print "名字 : $FORM{'a1'}<br>\n";
print "職稱 : $FORM{'a2'}<br>\n";
print "部門 : $FORM{'a3'}<br>\n";
print "性別 : $FORM{'sex'}<br>\n";
print "<br>\n";
print "<font size=+1>公司簡介:</font><br>\n";
print "<hr><br>\n";
print "公司名 : $FORM{'b1'}<br>\n";
print "地址 : $FORM{'b2'}<br>\n";
print "電話 : $FORM{'b3'}<br>\n";
print "傳真 : $FORM{'b4'}<br>\n";
print "E-Mail : $FORM{'b5'}<br>\n";
print "<br>\n";
print " </blockquote>\n";
print "<br>\n";
print "<center>\n";
print "<hr align=center width=80%>\n";
print "<font size=+1 color=red>感謝您填寫本表單</font>\n";
print "<hr align=center width=80%>\n";

sub mail_user {

$mail_to = $FORM{'b5'};

open (UMAIL, "|/usr/sbin/sendmail -f '$mail_from' '$mail_to'")||die "sendmail: $!\n";
print UMAIL<<"EOM";
Subject:$subject
MIMI-Version:1.0
Content-type:text/plain;charset=big5
Hello $FORM{'a1'},

謝謝您填寫此表單
EOM
close(UMAIL);
}

---------------------------------
###在Subject 之後的兩行可避免發生中文亂碼
MIMI-Version:1.0
Content-type:text/plain;charset=big5

0 意見: