1.) สร้างแบบฟอร์ม สำหรับกรอกข้อมูล เพื่อส่งอีเมล์ผ่านหน้าเว็บ ตั้งชื่อไฟล์เป็น formmail.html
<html>
<head>
<title>Your Title Here</title>
</head>
<body bgcolor="#FFFFFF">
<form method="post" action="sendmailresult.asp">
<table width="60%" border="1" cellspacing="0" cellpadding="3" align="center" bordercolor="#CCCCCC">
<tr bgcolor="#CCFF00">
<td colspan="2"><b>Send Mail with CDONTS component</b></td>
</tr>
<tr>
<td width="27%">From</td>
<td width="73%"><input type="text" name="From"></td>
</tr>
<tr>
<td width="27%">To</td>
<td width="73%">
<input type="text" name="To">
</td>
</tr>
<tr>
<td width="27%">CC</td><td width="73%">
<input type="text" name="CC">
</td>
</tr>
<tr>
<td width="27%">Subject</td><td width="73%">
<input type="text" name="Subject">
</td>
</tr>
<tr>
<td width="27%">Body</td>
<td width="73%">
<textarea name="Body" cols="50" rows="5" style="font-size: 14pt"></textarea>
</td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="Submit">
</td>
</tr>
</table>
</form>
</body>
</html>
ตัวอย่างการแสดงผลหน้า form2mail.html
2.) สร้าง script ASP เพื่อรับค่า input จาก formmail.html แล้วส่งข้อมูลไปให้ผู้รับตามที่กำหนด โดยตั้งชื่อไฟล์เป็น sendmailresult.asp
<%@ LANGUAGE="VBSCRIPT" %>
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type"
content="text/html;charset=tis-620">
<title>Your Title Here</title>
<%
Sub Write(strWriteThis)
'This subroutine just writes out whatever is
'passed to it.
response.write(strWriteThis & "<br>")
end sub
%>
</HEAD>
<BODY>
<%
Dim myCDONTSMail
Dim strFrom
Dim strTo
Dim strSubject
Dim strMessage
Dim lngImportance
'The following variable assignments are not required
'they are just here to make interpretation of the
'myCDONTSMail.Send line easier. You could put the
'Request.Form statements in the .Send line to cut down
'on the amount of code in the file.
strFrom=request.form("txtFrom")
strTo=request.form("txtTo")
strSubject = request.form("txtSubject")
strBody=request.form("txtMessage")
lngImportance = request.form("optImportance")
'The following four lines of code are just here for test
'purposes to see what variables have been pulled in from the
'HTM form.
'Write("strFrom = " & strFrom)
'Write("strTo = " & strTo)
'Write("strSubject = " & strSubject)
'Write("strMessage = " & strBody)
'Write("Importance = " & lngImportance)
Set myCDONTSMail = CreateObject("CDONTS.NewMail")
myCDONTSMail.Send strFrom,strTo,strSubject,strBody,lngImportance
Set myCDONTSMail = Nothing
Write "Mail has been sent."
Write "อีเมล์ถูกส่งออกไปให้ ผู้รับ : " &strTo &" เรียบร้อยแล้วครับ<br>"
Write "<a href='#' onclick='history.back()'>Back</a>"
%>
</BODY>
</HTML>
3.) สร้าง Function สำหรับส่งอีเมล์ไปให้ผู้รับตามที่กำหนด โดยตั้งชื่อไฟล์เป็น sendmail.asp
<%
Sub SendMail(sFrom,sTo,sCC,sSubject,sBody)
' รับค่าที่ส่งจาก Form ได้แก่ From, To, CC, Subject, Body
Dim sm
Set sm = Server.CreateObject("CDONTS.Newmail")
sm.SetLocaleIDs "874" ' กำหนด Code Page ให้แสดงผลภาษาไทย
sm.MailFormat = 0 ' กำหนดให้ส่งอีเมล์ในรูปแบบ MIME
sm.BodyFormat = 0 ' กำหนดให้ข้อความในอีเมล์เป็น HTML
sm.From = sFrom
sm.To = sTo
sm.CC = sCC
sm.Subject = sSubject
sm.Body = sBody
sm.Send
'--------------------------------------------------------------------
' การส่งอีเมล์อีกวิธีหนึ่ง
' sm.Send(sFrom,sTo,sCC,sSubject,sBody)
'--------------------------------------------------------------------
Set sm = Nothing
End Sub
%>
3) ท่านสามารถ Download ASP Script ทั้ง 3 ไฟล์นี้ได้ โดย
คลิกตรงนี้
หรือ Save เก็บไว้ใน Google Drive ของคุณ
4.) Upload ทั้ง 3 ไฟล์ ขึ้นไปไว้บน Server
5.) ทดสอบการใช้งานโดยเปิดหน้า http://www.yourdomain.com/formmail.asp กรอกข้อมูลให้ครบทุกช่อง แล้วคลิกปุ่ม "Submit"
6.) เข้าไปเช็ค inbox ของอีเมล์ผู้รับ ว่าได้รับอีเมล์หรือไม่ อย่าลืมตรวจสอบใน Junk box หรือ ถังขยะด้วย
หมายเหตุ
เนื่องจาก Mail Server มีระบบป้องการ SPAM ดังนั้นกรุณาตั้งค่าการ รับ-ส่ง อีเมล์ใน Script ของท่านดังต่อไปนี้
1. การรับอีเมล์จากหน้าแบบฟอร์มในเว็บไซต์ (Form Mail) ตรงอีเมล์ผู้รับ (To:) ต้องเป็นอีเมล์ @yourdomain.com เท่านั้น
2. การส่งอีเมล์ไปถึงผู้รับปลายทาง ตรงอีเมล์ของผู้ส่ง (From:) ต้องเป็นอีเมล์ @yourdomain.com เท่านั้น