Resources

Sending E-Mail with ASP 3.0 and CDONTS

Sending e-mail from a web page can be useful for a number of reasons. It doesnt reveal your email address for one, plus it means you can get the information that you want from users and all of this without the end user having to use an e-mail client, nor have an e-mail account or SMTP server to send through.

ASP 3.0 provides a new set of objects, referred to as CDONTS, which stands for Collaboration data objects for NT server. They can be used in conjunction with an HTML form and some text fields to create a nice e-mail form for a website to use, for some sort of contact facility.

Please note that SMTP and the capacity to send e-mail is required in order for this script to work as intended. If SMTP is not setup, the email files will sit within your "Pickup" directory under the mailroot directory.

So, without further ado, fire up a text editor or web development program. Create a new page and call it contact.asp (Either copy the code, or download the files for this script)

<%@ Language=VBScript %>

 <%
    Option Explicit
 %>

  <%
    ' -----------------------------------------------------
    ' CDONTS Email send script
    ' © http://www.designplace.org/
    ' Comments must remain intact for re-use of this code
    ' This page contains the HTML form and select case statements...
    ' ...which show any errors.
    ' -----------------------------------------------------

' -- declare our variables --
dim strErrText, strAction

' -- place the action parameter into the strAction variable --
strAction = Request.QueryString("action")

'  -- check the querystring for err=value parameters and show the required error --

Select Case strAction

   Case "err1" 
      strErrText = "<span style='color: red; font-weight: bold;'>Error: 
      No name input.</span>"
   Case "err2"
      strErrText = strErrText & "<span style='color: red; font-weight: 
      bold;'>Error: No email input.</span>"
   Case "err3"
      strErrText = strErrText & "<span style='color: red; font-weight: 
      bold;'>Error: No age input.</span>"
   Case "err4"
      strErrText = strErrText & "<span style='color: red; font-weight: 
      bold;'>Error: No gender input.</span>"
   Case "err5"
      strErrText = strErrText & "<span style='color: red; font-weight: 
      bold;'>Error: No message input.</span>" 

End Select
%>
  
    <html>
    <head>
    <title>Contact us!</title>
    </head>
  <body bgcolor="#FFFFFF" text="#000000">

  <%
   ' -- if our querystring contains an action= parameter, it means theres an error and it will be shown --

    if strAction > "" then
         response.write strErrText
    end if
  %>

  <p>Please fill out the form and click submit. All fields are required.</p>
    <form name="form1" method="post" action="send_it.asp">
    <p>Name: <br>
    <input name="name" type="text" size="30" 
    maxlength="30">
    </p>
    <p>Email address: <br>
    <input name="email" type="text" size="30" 
    maxlength="40">
    </p>
    <p>Age: <br>
    <input name="age" type="text" size="5" maxlength="2">
    </p>
    <p>Gender: <br>
    <select name="gender">
    <option selected value="">Choose....</option>
    <option value="male">Male</option>
    <option value="female">Female</option>
    </select>
    </p>
    <p>Message: <br>
    <textarea name="message" cols="30" rows="5"></textarea>
    </p>
    <input type="submit" value="submit">
    <input type="reset" value="reset">

    </form>
  </body>
</html>

The next ASP page is called send_it.asp, which is the value of "action", within the above form. Again, either copy the code into a text editor, or download the script files

<%@ Language=VBScript %>
  <%
    Option Explicit
    %>
  
    <%
    ' -----------------------------------------------------
    ' CDONTS Email send script
    ' © http://www.designplace.org/
    ' Comments must remain intact for re-use of this code
    ' -----------------------------------------------------
  
    ' -- declare our variables --

    dim strName, strEmail, strMessage, strAge, optGender

    '  -- attach our variables to the form elements held within the forms collection --

    strName = Request.Form("name") ' holds inputted name
    strEmail = Request.Form("email") ' holds inputted email address
    strMessage = Request.Form("message") ' holds inputted message
    strAge = Request.Form("age") ' holds inputted age
    optGender = Request.Form("gender").Item ' drop down list selection
    
    ' -- check all fields for empty values --
    ' -- remove and add new as required --

  if strName = "" then
    Response.Redirect "contact.asp?action=err1"
  else if strEmail = "" then
    Response.Redirect "contact.asp?action=err2"
  else if strMessage = "" then
    Response.Redirect "contact.asp?action=err5"
  else if strAge = "" then
    Response.Redirect "contact.asp?action=err3"
  else if optGender = "" then
    Response.Redirect "contact.asp?action=err4"
       end if
      end if
     end if
    end if
  end if

  ' -- begin email send process --
	' -- declare our main variable --

  dim objMail

  '  -- set our email object variable as the required object type --

  Set objMail = CreateObject("CDONTS.NewMail")

   ' -- email variables --
   ' -- change the to field to the email address you want to send the email to.
   ' -- change the subject and bodyformat if necessary

    objMail.To = "user@domain.com"
    objMail.From = Trim(strEmail)
    objMail.Subject = "Feedback"
    objMail.BodyFormat = "0" ' -- HTML format
    objMail.Body = "Name: " & Trim(strName) & vbCrLf _
    & "Gender: " & optGender & vbCrLf _
    & "Age: " & Trim(strAge) & vbCrLf _
    & "E-Mail Address: " & Trim(strEmail) & vbCrLf _
    & "Message: " & Trim(strMessage)
    
    ' -- send the email -- 
    objMail.Send

  ' -- clean up object
    Set objMail = Nothing

  ' -- execute confirmation page
    Response.Redirect "thanks.html"
%>

Our final page, is the confirmation page, which will thank the user for their input and confirm that it has been sent. I've called this page thanks.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- cdonts email send script, © http://www.designplace.org/ -->
<html>
<head>
<title>Confirmation</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

  <body>

    <h1>Feedback sent</h1>
    <p>Thanks for filling out the form!</p>
    <p>Your message has been sent.</p>
    <p><em><font size="2">Webmaster at http://domain.com</font></em></p>

  </body>
</html>

That concludes this script. Happy e-mailing!

Go to: Home | Download script files | Discuss in forums | ASP Links | Top



Remember, if you get stuck or need to ask any questions, register with the forums and tell us about it!


Warning: include(includes/ads.php) [function.include]: failed to open stream: No such file or directory in /home1/design/public_html/scripts.php on line 130

Warning: include(includes/ads.php) [function.include]: failed to open stream: No such file or directory in /home1/design/public_html/scripts.php on line 130

Warning: include() [function.include]: Failed opening 'includes/ads.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home1/design/public_html/scripts.php on line 130


le>