ASP Send Mail

PHTech

Senior Member
Joined
Aug 21, 2006
Messages
588
Reaction score
0
Location
Witbank
Hi there... Can someone please help me with this one. I have the following send mail thing done in an ASP page, but when I click on submit, it only writes the data to the database, and not to the mail. However, if I remove the first lines of the "SUBMIT" action, then it sends fine, but sends only pre-defined info, not the ones in the form. Below is the code for the send mail function. Thanx.

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

EmailTo = request.form("Email")
SenderName = request.form("FirstName")
SenderSurname = request.form("Surname")
FeedBody = request.form("FeedbackNote")

Dim objEmail

Set objEmail = CreateObject("CDO.Message")

objEmail.From = "[email protected]"
objEmail.ReplyTo = "[email protected]"
objEmail.To = EmailTo
objEmail.Subject = SenderName & " " & SenderSurname & " - " & "XCSA - Intranet Feedback form"
objEmail.TextBody = FeedBody

With objEmail.Configuration.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.vodamail.co.za"
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "[email protected]"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "xxxxx"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Update
End With

objEmail.Send
Set objEmail = Nothing

end if
%>
 
Last edited:
I assume you removed the code that writes to the database in this instance? Which wouldn't help much if the error was somewhere in or after that.

I don't rely on the submit button's text (I assume that's what you're using) for a form post.

Usually what I do is the following:

<input type="hidden" name="posted" value="true" />

And then right at the top of the page:

If Request("posted") = "true" Then
End If

Remember, using Request("value") will look for the value in both the Form and Querystring. It might be that the value you're looking for is only available in the Querystring and not the form. Make sure your form is a POST and not a GET just in case.

Also, good practice is to lower case the values.

If lcase(posted) = "submit" Then

Above will help in case ASP decides that today would be a nice day to be case sensitive (trust me, it happens)
 
a good way to figure out if you're getting the "posted" variable back correctly is to response.write it before the If statement
 
Below is the code for the submit page. When I click on submit, it redirects to the pre-programmed page, and NO mail is sent. I also don't get any funny ASP errors... Can someone please help me as I suck with code...!!!

Code:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!--#include file="Connections/connection1.asp" -->
<%
Dim MM_editAction
MM_editAction = CStr(Request.ServerVariables("SCRIPT_NAME"))
If (Request.QueryString <> "") Then
  MM_editAction = MM_editAction & "?" & Server.HTMLEncode(Request.QueryString)
End If

' boolean to abort record edit
Dim MM_abortEdit
MM_abortEdit = false
%>
<%
' IIf implementation
Function MM_IIf(condition, ifTrue, ifFalse)
  If condition = "" Then
    MM_IIf = ifFalse
  Else
    MM_IIf = ifTrue
  End If
End Function
%>
<%
If (CStr(Request("MM_insert")) = "form1") Then
  If (Not MM_abortEdit) Then
    ' execute the insert
    Dim MM_editCmd

    Set MM_editCmd = Server.CreateObject ("ADODB.Command")
    MM_editCmd.ActiveConnection = MM_connection1_STRING
    MM_editCmd.CommandText = "INSERT INTO feedback (FirstName, Surname, Email, FeedbackNote, DisplayThread) VALUES (?, ?, ?, ?, ?)" 
    MM_editCmd.Prepared = true
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param1", 202, 1, 50, Request.Form("FirstName")) ' adVarWChar
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param2", 202, 1, 50, Request.Form("Surname")) ' adVarWChar
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param3", 202, 1, 50, Request.Form("Email")) ' adVarWChar
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param4", 203, 1, 1073741823, Request.Form("FeedbackNote")) ' adLongVarWChar
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param5", 202, 1, 10, MM_IIF(Request.Form("DisplayThread"), "Y", "N")) ' adVarWChar
    MM_editCmd.Execute
    MM_editCmd.ActiveConnection.Close

    ' append the query string to the redirect URL
    Dim MM_editRedirectUrl
    MM_editRedirectUrl = "index.html"
    If (Request.QueryString <> "") Then
      If (InStr(1, MM_editRedirectUrl, "?", vbTextCompare) = 0) Then
        MM_editRedirectUrl = MM_editRedirectUrl & "?" & Request.QueryString
      Else
        MM_editRedirectUrl = MM_editRedirectUrl & "&" & Request.QueryString
      End If
    End If
    Response.Redirect(MM_editRedirectUrl)
  End If
End If
%>


<%
If request.form("submit") = "submit" Then


EmailTo = request.form("Email")
SenderName = request.form("FirstName")
SenderSurname = request.form("Surname")
FeedBody = request.form("FeedbackNote")

Dim objEmail

Set objEmail = CreateObject("CDO.Message")

objEmail.From = "[email protected]"
objEmail.ReplyTo = "[email protected]"
objEmail.To = EmailTo
'objEmail.To = "[email protected]"
objEmail.Subject = SenderName '& " " & SenderSurname &  " - " & "XCSA - Intranet Feedback form"
objEmail.TextBody  = FeedBody

With objEmail.Configuration.Fields
            .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.vodamail.co.za"
            .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "[email protected]"
            .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "xxxx"
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
            .Update
End With

objEmail.Send
Set objEmail = Nothing

end if
%>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Feedback</title>
<link href="assets/css/SiteGlobal.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
body,td,th {
	font-family: Tahoma, Geneva, sans-serif;
}
-->
</style></head>

<body leftmargin="0" topmargin="0">
<div class="HomeHeader"></div>
<div class="DivWrapper">
  <div class="MainTable">
    <p class="Date">comments &amp; feedback</p>
    <div class="SideNav">
      <p class="SideNavHeads"><a href="index.html" class="SideNavLinks">&raquo; home</a> <a href="#" class="SideNavLinks">&raquo; viewable threads</a><a href="witbank/index.html" class="SideNavLinks"></a></p>
      <p class="SideNavHeads">Related Items</p>
      <p class="SideNavHeads"><a href="links.html" class="SideNavLinks">&raquo; links</a> <a href="#" class="SideNavLinks">&raquo; latest updates</a><a href="http://chhzrhsrv650/EN/Pages/default.aspx" class="SideNavLinks">&raquo; xstrata plc</a> <a href="search.html" class="SideNavLinks">&raquo; search</a></p>
      <p class="SideNavHeads">Administration</p>
      <p class="SideNavHeads"><a href="pages/admin/adm_login.asp" class="SideNavLinks">&raquo; it administration</a></p>
      <p class="SideNavHeads"><a href="#"></a></p>
    </div>
    <p class="Heading1">Feedback</p>
    <div class="MainInfo">
      <p>Please give us your feedback on this intranet site. You can add suggestions, comments, something you want us to add or anything related to this intranet site.    </p>
    </div>
    <div class="NormalText1">
      <form action="<%=MM_editAction%>" method="post" name="form1" id="form1">
        <table align="center">
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">First Name:</td>
            <td><input type="text" name="FirstName" value="" size="32" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">Surname:</td>
            <td><input type="text" name="Surname" value="" size="32" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">E-mail:</td>
            <td><input type="text" name="Email" value="" size="32" /></td>
          </tr>
          <tr>
            <td nowrap="nowrap" align="right" valign="top">Feedback / Comments:</td>
            <td valign="baseline"><textarea name="FeedbackNote" cols="50" rows="5"></textarea></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">Add to Public Threads:</td>
            <td><input type="checkbox" name="DisplayThread" value="1" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">&nbsp;</td>
            <td><input type="submit" name="submit" value="submit" /></td>
          </tr>
        </table>
        <input type="hidden" name="MM_insert" value="form1" />
      </form>
      <p>&nbsp;</p>
    </div>
    <p>&nbsp;</p>
</div>
  <p class="Footer">Copyright &copy; 2009 Xstrata Coal South Africa. All rights reserved | <a href="#" class="FooterLinks">Internet Usage Policy</a> | <a href="#" class="FooterLinks">IT Department</a> | <a href="#" class="FooterLinks">Credits</a></p>
</div>
</body>
</html>
 
As I suspected

You redirect and post the data via the querystring, but you check a form submission request with your "posted" variable

Change:
posted = request.form ("submit")

To:
posted = request("submit")
 
Funny how we all make the same mistakes to begin with. First thing I thought was "GET or POST?". :D Good on ya, AcidRaZor.

[EDIT]
Wait, hold on a second. On posting, you do the DB stuff, then redirect. Why? You do this before any of the email stuff - that's why it's not being sent. Also, I don't see where any of the form data is being sent to the redirect either.
 
Last edited:
Thanx for the replies... Have managed to get this working by submitting to the database AND send a mail in one operation (on one page). Just placed the sendmail script at the right place in the add to db script, and placed the end if's at the right places...
 
Top
Sign up to the MyBroadband newsletter
X