明辉站/网站教程/内容

Cgi基础图文说明教程之:10 email子过程

网站教程2024-06-03 阅读
[摘要]email子过程 #*****************BEGIN BODY************* print "<h1>Thank you for filling out the form</h1>"; $firstname = $value[...
email子过程 
#*****************BEGIN BODY************* 
print "<h1>Thank you for filling out the form</h1>"; 
$firstname = $value[0]; 
$lastname = $value[1]; 
$email = $value[2]; 

print "Your first name is $firstname<BR>"; 
print "Your last name is $lastname<BR>"; 
print "Your e-mail is $email<BR>"; 
$to = $email; 
$from = "clinton\@whouse.gov"; 
$sub = "subject of my first e-mail"; 
$body = "The form was filled out by $firstname $lastname 
Thank you goes on another line."; 
&email($to,$from,$sub,$body); 

#***************END BODY****************** 
--------------------------------------------------------------------------------
在上面的例子中,我在程序的BODY后面增加了7行。你需要拷贝这些行到test2.cgi的BODY中。有两种方式:

在PC上的文本编辑器中进行拷贝和粘贴,然后用FTP重新上传,这时不必重新运行chmod。 
可以在Unix提示符下运行Emacs或Pico,对文件进行修改,然后保存和退出。 
这时你可以再试试form。要在testform.htm页面中输入你自己的邮件地址。当你提交这个form时,显示结果与以前一样。但如果你在几秒种后查看你的e-mail,你会看到一封来自President Clinton的消息。
让我们看看这些行:
$to = $email; 
- 拷贝变量$email中的内容到变量$to中。
$from = "clinton\@whouse.gov"; 
- 设置变量$form为clinton@whouse.gov。反斜线(\)称为escape character。@符号在Perl中有特殊意义,表示一个数组,这时,如果我们不想引用数组,而只用@符号本身,需要在前面加一个"\"。
例如,如果我敲入下面这行:
$amount = "He owes me $20.00"; 
将得到一个错误,因为Perl将试图访问一个称为$20.00的变量。我们可以这样写:
$amount = "He owes me \$20.00"; 
$sub = "subject of my first e-mail"; 
这行很直接。
$body = "The form was filled out by $firstname $lastname Thank you goes on another line."; 
这只是一个命令 - Perl命令总以分号结束。返回的字符是赋给$body的字符串中的另一个字符。这很方便,因为可以敲入引号,然后象在字处理器中一样敲入多行文本,然后用引号结束。最后,象其它语句一样敲入引号。
也可以象这样而得到相同的结果:
$body = "The form was filled out by $firstname $lastname \n Thank you goes on another line."; 
\n为换行符 - 当双引号中包含\n时,把它翻译成回车符。这对email也起作用 - 它是用Ascii,而不是HTML写的。注意HTML不在意源代码是在一行还是在多行。如果想在HTML中加入一行,需要插入一个<BR>或<P>标记符。
&email($to,$from,$sub,$body); 
email子过程在下面的readparse子过程中定义。它被配置成很好用,只需简单地敲入
&email( addressee , reply-to, subject, message body) 
例子中也可以这样传递参数:
&email($email,"clinton\@whouse.gov","subject of my first e-mail","This is line 1 \nThis is line 2"); 
但是我认为分别赋值对于程序的编辑和阅读更容易。>>

……

相关阅读