PC-NET 網業

http://

PC-NET 網業           留言板

         關於我們

PC-NET 網業

PC-NET 網業

 www.pc-net.com.tw     網路電腦專業評論雜誌
首頁新聞 專題報導 新品測試 每週報價 資訊話題 電腦應用 ASP專區 火線討論區 下載 廠商佈告欄  
會員登入!! PC-NET廣告 中強
B_cir_up_l.GIF (144 個位元組) ASP專區   B_cir_up_r.GIF (153 個位元組)

 

ASP應用 

By: Jackie Chen

 

什麼叫做Form To Email

大家都有在網路上加入會員的經驗,一開始這家網站會要求你填入一些個人資料(姓名、

Email、密碼等等....),等到你填完之後,這家公司會告訴你請到你的信箱伺服器收信,因為它

們已經將包含你個人資料的信件回覆到你的信件伺服器中,大家一定很好奇這是怎麼做到的,

其實很簡單,同樣的也是用到ASP送信元件Jmail,筆者將在這裡告訴大家如何撰寫這個簡單

的小程式,一開始也是利用一個簡單的表單來收集客戶的資料,程式片段如下:

Register.asp

<head>
<title>加入PC-NET會員</title>
</head>
<body BACKGROUND="Bg.gif" text="black">
<h2>加入PC-NET會員</h2>
<form method="post" action="add.asp">
<table cellspacing="1" height="230">
<tr>
<td nowrap width="672" height="46"><img SRC="HTBUL.gif" WIDTH="13" HEIGHT="13"> 
姓名<font face="arial">:&nbsp;&nbsp; <input type="text" name="Re_Name" size="18" MAXLENGTH="20"></font></td> 
</tr> 
<tr> 
<td nowrap width="672" height="46"><font face="arial"><img SRC="HTBUL.gif" WIDTH="13" HEIGHT="13"> 
</font>E-Mail<font face="arial">: 
<input type="text" name="Re_Email" size="18" MAXLENGTH="30"></font></td> 
</tr> 

<tr> 
<td nowrap width="672" height="46"><font face="arial"><img SRC="HTBUL.gif" WIDTH="13" HEIGHT="13"> 
</font>密碼<font face="arial">: 
<input type="password" name="Re_Pass" size="18" MAXLENGTH="30"></font></td> 
</tr> 

<tr> 
<td nowrap width="672" height="46"><font face="arial"><img SRC="HTBUL.gif" WIDTH="13" HEIGHT="13"> 
</font>確認密碼<font face="arial">: 
<input type="password" name="Re_Pass1" size="18" MAXLENGTH="30"></font></td> 
</tr> 
<input type=hidden name="Re_Date" value="<%=DATE%>">
<tr> 
<td width="678" height="46"><font face="Arial"> 
<input type="submit" value="註冊"></font></td> 
</tr> 
</table> 
</form> 

而表單畫面如下圖所示:
                                                

而表單送出之後,是由add.asp將資料寫進資料庫以及利用Jmail元件來做送信的動作,一樣的,我們必須先建立存放客戶資料的資料庫,這個資料庫包含六個欄位,各欄位定義如下:

Register
Re_ID 自動編號
Re_Name 文字
Re_Email 文字
Re_Pass 文字
Re_Pass1 文字
Re_Date 日期/時間

再來看看add.asp這個程式的原始碼:

add.asp
      
   <%
'決定是要從表單傳送資料還是利用參數傳遞的方式傳送資料
function getFormObject ()
if Request.ServerVariables("REQUEST_METHOD") = "GET" then
set getFormObject=Request.QueryString
else
set getFormObject=Request.Form
end if
end function

%> 
<!-- #Include file="adovbs.inc" -->
<%


strdbName="register.mdb"
strDBPath=Server.MapPath (strDBName) 
set oConn=Server.CreateObject("ADODB.Connection")
oConn.Open "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & strDBPath
%>

<head><meta http-equiv="refresh" content="5;url=register.asp">

<title>檢驗資料</title>
</head>
<body BACKGROUND="Bg.gif" text="black">
<%

hadErrors=false
set oFormVars=GetFormObject()
strTableName="Register"
if trim(Request("Re_Name")) & "" = "" then
hadErrors=true
strError="( 對不起 ) -- 你必須輸入你姓名 !!"
end if
if trim(Request("Re_Email")) & "" = "" then
hadErrors=true
strError="( 對不起 ) -- 你必須輸入你的EMail帳號 !!"
end if 
if hadErrors=false then
set oRS=oConn.Execute ("select Re_EMail from Register " &_
"where Re_EMail like '%" & Request("Re_Email") & "%'")
if not oRS.EOF then
hadErrors=true
strError="( 對不起 ) -- 這個E-Mail已經有人註冊過了 !!"
end if
set oRS=Nothing
end if
if hadErrors=false then
set oRS=oConn.Execute ("select Re_Name from Register " &_
"where Re_Name = '" & Request("Re_Name") & "'")
if not oRS.EOF then
hadErrors=true
strError="( 對不起 ) -- 這個姓名已經有人註冊過了 !!"
end if
set oRS=Nothing
end if
if hadErrors=false then
if Request("Re_Pass") <> Request("Re_Pass1") then
hadErrors=true
strError="( 對不起 ) -- 密碼和確認密碼不合,請重新輸入 !!"
end if
end if
if hadErrors=false then
set oRS=Server.CreateObject("ADODB.Recordset")

oRS.CursorType = adOpenKeyset
oRS.LockType = adLockOptimistic
oRS.Open strTableName, oConn, , , adCmdTable

oRS.AddNew

for each f in oRS.Fields
if oFormVars(f.Name) <> "" then
f.value=oFormVars(f.Name)
end if
next
oRS.Update

if oConn.Errors.Count > 0 then
hadErrors=True
strError="<h3>資料庫發生錯誤.</h3>"
for each e in oConn.Errors
strError=strError & e.Description & "<br>"
next
end if
end if

if hadErrors=false then %>

<%

Set JMail=Server.CreateObject("JMail.SMTPMail") 
JMail.ServerAddress ="你的信箱伺服器" 
JMail.ContentTransferEncoding = "8bit"
JMail.Charset = "BIG5"
JMail.ContentType = "text/html"
JMail.Encoding = "base64"
JMail.MimeVersion = "1.0"
JMail.SenderName = "網頁資訊" 
JMail.Subject = "謝謝你加入PC-NET會員"
JMail.AddRecipient "副本傳送之電子郵件信箱"
JMail.Replyto= Request.Form("Re_Email")
JMail.Body = Request.Form("Re_Name")& "你好,感謝你對我們的支持,你的使用者名稱是"& Request.Form("Re_Name")& ",你的密碼是"& Request.Form("Re_Pass")
JMail.Priority=2
JMail.Execute
set JMail=nothing

%>
<h2>信件寄出</h2>
<h1>我們已經將你的資料加入了</h1>
<p>謝謝你對我們的支持.</p>
<%
else %>
<h1>輸入資料錯誤了 !!</h1>
<p><% =strError %></p>
<%
end if
%>

副本傳送之電子郵件信箱可以不加,但為了確保每一個加入會員的客戶都能收到回覆信,筆者

還是建議最好加上,你覺得這個程式夠簡單吧!!讓我們再來看看Jmail的另一個應用,
推薦信函

的使用,基本上推薦信函的用法在國外非常普遍.但在國內比較少見,可能是中文筆劃較多,大

家比較懶得打吧!!但筆者強烈建議一個好的網站都應該有這項功能,不但能夠讓自己的網站

擴大知名度,讓更多人知道,同時也照顧到對那些有好東西就一定要跟好朋友分享的網友,而

且這個程式比上面的程式來的還要簡單,只要修修改改,就讓自己的網站又多了一項功能,何

樂而不為呢!!馬上就讓我們來看看推薦信函程式的原始碼:

recommend.asp

<html>

<head>
<title>PC-NET推薦信函</title>
</head>


<body bgcolor=6666f0>

<form method="POST" action="sendit.asp">
<div align="center"><center><p><br><br><br></p>
</center></div><div align="center"><center><table border="1" height=278 width=500 border color=ffffff><td height=14>
文章標題&nbsp;: <font color="#0000FF"><input type="text" name="SUBJECT" size="30"><br> 
<br> 
</font>推薦者&nbsp;&nbsp;&nbsp;&nbsp; : <font color="#0000FF"><input type="text" name="NAME" size="30"></font><br> 
<br> 
被推薦者 : <font color="#0000FF"><input type="text" name="EMAIL" size="30"></font> 
<br> <tr> 
<td height=256> 

推薦內容: 
<br> 

<font color="#3399FF"><strong><textarea rows="10" name="MESSAGE" cols="50"></textarea> 
</strong></font> 
</tr> 
</table> 
</center></div><div align="center"><center><p><input type="submit" 
value="推薦"></p> 

</center></div><center><a href=/default.asp>回首頁</a> 
</form> 

<br> 

</center> 

</body> 
</html> 

這是讓使用者輸入推薦資料的畫面:

                               

 跟上面的程式比較起來這個程式簡單的太多了,因為你不必將資料寫入資料庫,少了一堆開

啟資料庫,寫入資料庫,關閉資料庫的動作,你只要單純的將輸入的資料丟給sendit.asp,其他的

事就交給Sendit.asp去處理啦  !!

sendit.asp        

<head>
<title>PC-NET推薦信函</title>
</head>
<body bgcolor="white" text="black">
<%

Set JMail=Server.CreateObject("JMail.SMTPMail") 
JMail.ServerAddress ="你的E-Mail伺服器" 
JMail.ContentTransferEncoding = "8bit"
JMail.Charset = "BIG5"
JMail.ContentType = "text/html"
JMail.Encoding = "base64"
JMail.MimeVersion = "1.0"
JMail.SenderName = Request.Form("NAME") 
JMail.Subject = Request.Form("SUBJECT")
JMail.AddRecipient Request.Form("EMAIL")
JMail.Body = Request.Form("MESSAGE")
JMail.Priority=2
JMail.Execute
set JMail=nothing

%>
<h2>信件寄出</h2>
<p>這封信已成功寄給所選會員. <a href="index.asp">按這裡</a> 繼續.</p>

下載原始程式

即時回覆系統

推薦信函

 

 

 

 

 

 

 

 

  GASP專區

FPC-NET 首頁