0

好的,所以我的问题是这个。我有一个简单的 vbscript,可以在提交表单时发送电子邮件。当它在如下代码中时,如果发送电子邮件(我在收件箱中收到电子邮件)不可取两次:页面加载时一次,提交时一次:

<% Sub sendEmail(mailFrom, mailTo, mailSubject, mailMessage, mailServer, mailUsername, mailPassword)
Set MyMail = CreateObject("cdo.message")
MyMail.From = mailFrom
MyMail.To = mailTo
MyMail.Subject = mailSubject
MyMail.HTMLBody = mailMessage
MyMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
MyMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = mailServer
MyMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = mailUsername
MyMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = mailPassword
MyMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
MyMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
MyMail.Configuration.Fields.Update
MyMail.Send
Set MyMail = nothing
End Sub

Dim mailFrom, mailTo, mailSubject, mailMessage, mailServer, mailUsername, mailPassword

mailFrom = "example"
mailTo = "example"
mailSubject = "Email test"
mailMessage = "<html><body>example</body><html>"
mailServer = "example"
mailUsername = "example"
mailPassword = "example"

Call sendEmail(mailFrom, mailTo, mailSubject, mailMessage, mailServer, mailUsername, mailPassword)%>

所以我想我必须做这样的事情,所以它只发送一次。在提交按钮下的表单中添加一个隐藏字段,并将代码编辑为此。唯一的问题是,当我这样做时,表单似乎可以正确提交并且没有发生错误,但我的收件箱中从来没有收到电子邮件!任何帮助将不胜感激。我在这方面不是最好的,我还在学习!:

<% if request.form("isSubmitted") = "yes" then
Sub sendEmail(mailFrom, mailTo, mailSubject, mailMessage, mailServer, mailUsername, mailPassword)
Set MyMail = CreateObject("cdo.message")
MyMail.From = mailFrom
MyMail.To = mailTo
MyMail.Subject = mailSubject
MyMail.HTMLBody = mailMessage
MyMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
MyMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = mailServer
MyMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = mailUsername
MyMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = mailPassword
MyMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
MyMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
MyMail.Configuration.Fields.Update
MyMail.Send
Set MyMail = nothing
End Sub

Dim mailFrom, mailTo, mailSubject, mailMessage, mailServer, mailUsername, mailPassword

mailFrom = "example"
mailTo = "example"
mailSubject = "Email test"
mailMessage = "<html><body>example</body><html>"
mailServer = "example"
mailUsername = "example"
mailPassword = "example"

Call sendEmail(mailFrom, mailTo, mailSubject, mailMessage, mailServer, mailUsername, mailPassword)
end if %>

编辑#1:这是我的隐藏字段(我也将隐藏字段放在提交按钮后无济于事。)

<input type="hidden" name="isSubmitted" id="isSubmitted" value="yes" /> 
<input type="submit" name="btnSubmit" id="btnSubmit" value="Submit" class="submit" />
4

1 回答 1

1

您确定您的表单正在执行 POST 而不是 GET 吗?如果是 GET 请求,则需要 request.querystring("isSubmitted")

我还将检查放在一个函数中。

Function IsSubmit()
    'IsSubmit= (request.form("isSubmitted") = "yes") 'Edited for more generic solution
    IsSubmit = (Request.ServerVariables("REQUEST_METHOD") = "POST")
End Function

然后你可以在你的代码中使用 __ if IsSubmit() then __

于 2012-09-17T14:21:17.303 回答