如何验证表单和发送电子邮件

时间:2013-05-17 10:05:14

标签: javascript asp-classic vbscript

我有一个表单用javascript验证表单字段并使用VBscript发送电子邮件。如何验证工作正常,但电子邮件没有发送到电子邮件帐户。

发送电子邮件的VBScript:

<%
posted = request.form ("submit")
if posted = "Submit" then

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' Customize the following 5 lines with your own information. ''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
vtoaddress =   "___________" ' Change this to the email address you will be receiving your notices.
vmailhost =    "smtp.gmail.com" ' Change this to your actual Domain name.
vfromaddress = "___________" ' Change this to the email address you will use to send and authenticate with.
vfrompwd =     "___________" ' Change this to the above email addresses password.

'''''''''''''''''''''''''''''''''''''''''''
'' DO NOT CHANGE ANYTHING PAST THIS LINE ''
'''''''''''''''''''''''''''''''''''''''''''
vsubject =  request.form ("subject")   
vfromname = request.form ("fname")
vbody = request.form ("message")
vrplyto = request.form ("email")
vrcity = request.form ("city")
vrmono = request.form ("phone")
vmsgbody = "<b>Name:</b> "& vfromname & "<br><b>Email:</b> "& vrplyto &"<br><b>Mobile No:</b> "& vrmono &"<br><b>City:</b> "& vrcity &"<br><b>Subject:</b> "& vsubject&"<br><b>Message:</b> "& vbody

Set objEmail = Server.CreateObject("CDO.Message")

objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = vmailhost
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = 1
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = vfromaddress
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = vfrompwd
objEmail.Configuration.Fields.Update

objEmail.Subject = vsubject
objEmail.From = vfromaddress
objEmail.To=vfromaddress
objEmail.HTMLBody = vmsgbody
objEmail.Send
    vErr = Err.Description
    if vErr <> "" then
        response.write vErr & "<br><br>There was an error on this page."
        'MsgBox("There was an error on this page.")
    else
        response.write "Thank you, your message has been sent."
        'MsgBox("Thank you, your message has been sent.")
    End If
Set objEmail = Nothing
response.write "Thank you, your message has been sent."
'MsgBox("Thank you, your message has been sent.")
end if
%>

Javascript验证表单:

<script language="JavaScript">
<!--
    function validate()
    {
        var count_bug=0;    
        if(document.form1.fname.value=="")
        {
            document.getElementById("alertMsgfname").innerHTML="&nbsp;Enter Your First Name.&nbsp;";
            document.getElementById("alertMsgfname").style.visibility="visible";
            if(eval(count_bug)==0) 
                document.form1.fname.focus();
            count_bug+=1;
        }
        if(document.form1.email.value=="")
        {
            document.getElementById("alertMsgemail").innerHTML="&nbsp;Enter Your E-mail.&nbsp;";
            document.getElementById("alertMsgemail").style.visibility="visible";
            if(eval(count_bug)==0) 
                document.form1.email.focus();
            count_bug+=1;
        }
        else if(!isEmail(document.form1.email.value))
            {    
            document.getElementById("alertMsgemail").innerHTML="&nbsp;Enter Valid E-mail!&nbsp;";
            document.getElementById("alertMsgemail").style.visibility="visible";
            if(eval(count_bug)==0) 
                document.form1.email.focus();
            count_bug+=1; 
            }
        if(document.form1.phone.value=="")
        {
            document.getElementById("alertMsgphone").innerHTML="&nbsp;Your Phone No.&nbsp;";
            document.getElementById("alertMsgphone").style.visibility="visible";
            if(eval(count_bug)==0) 
                document.form1.phone.focus();
            count_bug+=1;
        }

        else if(!ValidateNo(document.form1.phone.value," 1234567890,-/+"))
        {
            document.getElementById("alertMsgphone").innerHTML="&nbsp;Invalid Phone No.&nbsp;";
            document.getElementById("alertMsgphone").style.visibility="visible";
            if(eval(count_bug)==0) 
                document.form1.phone.focus();
            count_bug+=1;
        }
        else if(document.form1.phone.value.length < 5)
        {
            document.getElementById("alertMsgphone").innerHTML="&nbsp;Invalid Phone No.&nbsp;";
            document.getElementById("alertMsgphone").style.visibility="visible";
            if(eval(count_bug)==0) 
                document.form1.phone.focus();
            count_bug+=1;
        }
        if(document.form1.city.value=="")
        {
            document.getElementById("alertMsgCity").innerHTML="&nbsp;Enter Your City Name.&nbsp;";
            document.getElementById("alertMsgCity").style.visibility="visible";
            if(eval(count_bug)==0) 
                document.form1.city.focus();
            count_bug+=1;
        }
        if(document.form1.subject.value=="")
        {
            document.getElementById("alertMsgSubject").innerHTML="&nbsp;Enter Your Subject.&nbsp;";
            document.getElementById("alertMsgSubject").style.visibility="visible";
            if(eval(count_bug)==0) 
                document.form1.subject.focus();
            count_bug+=1;
        }
        if(document.form1.message.value=="")
        {
            document.getElementById("alertMsgMessage").innerHTML="&nbsp;Enter Your Message.&nbsp;";
            document.getElementById("alertMsgMessage").style.visibility="visible";
            if(eval(count_bug)==0) 
                document.form1.message.focus();
            count_bug+=1;
        }
    if(count_bug>0)
        return false;
    else
        return true;
    }
function isEmail (emailIn){
    var isEmailOk = false;
    var filter = /^[a-zA-Z0-9][a-zA-Z0-9._-]*\@[a-zA-Z0-9-]+(\.[a-zA-Z][a-zA-Z-]+)+$/
    //  var filter = /^(([^<>()[\]\\.,;:\s@\”]+(\.[^<>()[\]\\.,;:\s@\”]+)*)|(\”.+\”))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/

    if(emailIn.search(filter) != -1)
        isEmailOk = true;
    if(emailIn.indexOf("..") != -1)
        isEmailOk = false;
    if(emailIn.indexOf(".@") != -1)
        isEmailOk = false;

    return isEmailOk;
}
function ValidateNo( NumStr, String )
{
    for( var Idx = 0; Idx < NumStr.length; Idx ++ )
    {
        var Char = NumStr.charAt( Idx );
        var Match = false;
        for( var Idx1 = 0; Idx1 < String.length; Idx1 ++)
        {
            if( Char == String.charAt( Idx1 ) )
            Match = true;
        }
        if ( !Match )
        return false;
    }
    return true;
}
    </script>

html表单代码:

<form name="form1" method="post" onsubmit="return validate();"> 
          <table border="0" width="100%" cellspacing="0" cellpadding="0" class="table-format">
          <tr valign="middle">
            <td align="left" class="text-fm" width="23%" style="padding-top:8px;">
             <b>Name<font color="#C70017">*</font></b></td>
            <td align="left" class="text-fm" width="5%">
            <b>:</b></td>
            <td class="text-fm" align="left"> 
              <input type="text" name="fname" id="fname" size="43" maxlength="40" class="inp">
              <span id="alertMsgfname" class="valfrm" style='line-height:8px;'></span></td>
          </tr>
         <tr valign="middle">
            <td align="left" class="text-fm" style="padding-top:8px;">
             <b>Email Id<font color="#C70017">*</font></b></td>
            <td align="left" class="text-fm">
            <b>:</b></td>
            <td class="text-fm" align="left"> 
            <input type="text" name="email" id="email" size="43" maxlength="76" class="inp">
            <span id="alertMsgemail" class="valfrm" style='line-height:8px;'></span></td>
          </tr>
         <tr valign="middle">
              <td align="left" class="text-fm" style="padding-top:8px;">
                <b>Mobile No.<font color="#C70017">*</font></b></td>
              <td align="left" class="text-fm">
                <b>:</b></td>
              <td class="text-fm" align="left"> 
                  <input type="text" name="phone" id="phone" size="43" maxlength="16" class="inp">
                  <span id="alertMsgphone" class="valfrm" style='line-height:8px;'></span></td>
              </tr>
              <tr valign="middle">
              <td align="left" class="text-fm" style="padding-top:8px;">
                <b>City<font color="#C70017">*</font></b></td>
              <td align="left" class="text-fm">
                <b>:</b></td>
              <td class="text-fm" align="left"> 
               <input type="text" name="city" id="city" size="43" maxlength="76" class="inp">
               <span id="alertMsgCity" class="valfrm" style='line-height:8px;'></span></td>
              </tr>
              <tr valign="middle">
              <td align="left" class="text-fm" style="padding-top:8px;">
                <b>Subject<font color="#C70017">*</font></b></td>
              <td align="left" class="text-fm">
                <b>:</b></td>
              <td class="text-fm" align="left"> 
                  <input type="text" name="subject" id="subject" size="43" maxlength="76" class="inp">
                  <span id="alertMsgSubject" class="valfrm" style='line-height:8px;'></span></td>
              </tr>
              <tr valign="middle">
              <td align="left" class="text-fm" style="padding-top:8px;">
                <b>Message<font color="#C70017">*</font></b></td>
              <td align="left" class="text-fm">
                <b>:</b></td>
              <td class="text-fm" align="left"> 
                 <textarea name="message" id="message" size="43" maxlength="16" class="inp" rows="6" cols="30"></textarea>
                 <span id="alertMsgMessage" class="valfrm" style='line-height:8px;'></span></td>
              </tr>
               <tr>
            <td align="left" colspan="3">
              <div align="center"><br>
                <input type="submit" value="" name="Submit" class="imgClass"/>
                <br>
                </td>
          </tr>
              </table>
          </form>

1 个答案:

答案 0 :(得分:0)

缺少错误描述并不意味着没有错误。将您的错误处理代码更改为:

If Err Then
    response.write Err.Number & "<br><br>There was an error on this page."
Else
    response.write "Thank you, your message has been sent."
End If

您是否可以通过端口465从Web服务器连接到邮件服务器?

telnet smtp.gmail.com 465

您使用的是正确的凭据吗?

您是否可以访问邮件服务器日志?他们对该页面的连接有什么看法?如果您无法访问实际的邮件服务器,是否可以设置虚拟服务器并暂时将该页面指向该服务器,以便检查代码是否正常工作?