Form to Mail Using CDO

posted by Scudyen date: Thursday, May 22, 2008 category: ASP

In this article, we will use CDO instead of CDONTS. CDO comes within windows XP and windows 2000/3 and has more control over mailing system. Following asp source contains a simple webform and client side validation that controls unvalid characters. Web form uses CDO to deliver form results to your email account.


To use the sample code all you need to change "YOUR_MAIL_SERVER" and "YOUR_EMAIL" variables in the code.

http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
http://www.w3.org/1999/xhtml">




<!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=iso-8859-1" />

  <title>Form Mail Using CDO </title>


<SCRIPT language=javascript>


<!-- hide JavaScript code from browsers that are not JavaScript enabled

  var whitespace = " \\t\\n\\r";

  var legalChars = "abcdefghijklmnopqrstuvwxyz0123456789_-."


function isEmpty(s)

  { return ((s == null) || (s.length == 0)) }


function isWhitespace (s)

  {

  var i;


if (isEmpty(s)) return true;


for (i = 0; i < s.length; i++)

  {


var c = s.charAt(i);


if (whitespace.indexOf(c) == -1) return false;

  }


return true;

  }


function isEmail(s) {


if (isWhitespace(s)) {

  return false;

  }


var sLength = s.length;

  if (sLength < 6) return false;

  var i = 0;


while ((i < sLength) && (s.charAt(i) != "@")) {

  if (legalChars.indexOf (s.charAt(i)) == -1) return false;

  i++;

  }


if ((i < 2) || (i > (sLength - 3)) || (s.charAt(i) != "@")) {

  return false;

  }


Index = i;

  i++;


while ((i < sLength) && (s.charAt(i) != ".")) {

  if (legalChars.indexOf (s.charAt(i)) == -1) return false;

  i++;

  }


 


if ((i < (Index + 3)) || (i >= sLength - 2) || (s.charAt(i) != ".")) {

  return false;

  }


while (i < sLength) {

  if (legalChars.indexOf (s.charAt(i)) == -1) return false;

  i++;

  }

  return true;


}


function checkform() {


var message = ""; // create error message string variable, with nothing in it


if (document.newForm.name.value.length == 0) {

  message = message + "- Please, enter your name.\\n\\n";

  }


if (document.newForm.subject.value.length == 0) {

  message = message + "- Please, enter your subject.\\n\\n";

  }


if (document.newForm.message.value.length == 0) {

  message = message + "- Please, enter your message.\\n\\n";

  }


if (!(isEmail(document.newForm.email.value))) {

  message = message + "- Your email is not valid.\\n\\n";

  }


if ( message.length > 0 ) { // is there an error message?

  message = "Please, fill the form completely :\\n\\n" + message ;

  alert( message ); // display error message

  return false; // return bad, not ok to process

  }

  else {

  alert( "Thank you for your message." );

  return true; // no error message to display, return ok to process


}


} // end of the function checkform()


//end hiding of JavaScript code -->

  </SCRIPT>

  </head>

  <body>


<%

  if request.form("func") = "Send Message" then

 

  name = trim(request.form("name"))

  email = trim(request.form("email"))

  subject = trim(request.form("subject"))

  message = trim(request.form("message"))


msg = "Web Form " & chr(10) & chr(10)

  msg = msg & "Name  : " & name & chr(10)

  msg = msg & "Email  : " & email & chr(10)

  msg = msg & "Message  : " & message & chr(10)


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

  Set objConf = Server.CreateObject("CDO.Configuration")

  Set objFields = objConf.Fields

 

  With objFields

  .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

  .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver")  = "YOUR_MAIL_SERVER"

  .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 10

  .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

  .Update

  End With


With objMail

  Set .Configuration = objConf

  .From = email

  .To = "YOUR_EMAIL"

  .Subject = "Contact Form - " & subject

  .TextBody = msg

  End With

  Err.Clear

  objMail.Send


Set objFields = Nothing

  Set objConf = Nothing

  Set objMail = Nothing


response.write "<b>Thank you very much for your post.</b>"

  end if

  %>

  <form method="POST" action="post_email.asp" name="newForm" onsubmit="return checkform()">

  <center>

  <table border="0" width="96%" cellpadding="0" >

  <tr>

  <td width="100%" bgcolor="#f4f4f4">


<table border="0" width="100%" cellpadding="0" bgcolor="#FFFFFF">

  <tr>

  <td width="30%"><b><font size="1" face="Verdana">Name

  :</font></b></td>

  <td width="70%"><input type="text" name="name" size="35"/></td>

  </tr>

  <tr>

  <td width="30%"><b><font size="1" face="Verdana">Email

  :</font></b></td>

  <td width="70%"><input type="text" name="email" size="30"/></td>

  </tr>

  <tr>

  <td width="30%"><b><font size="1" face="Verdana">Subject

  :</font></b></td>

  <td width="70%"><input type="text" name="subject" size="40"/></td>

  </tr>

  <tr>

  <td width="30%" valign="top"><b><font size="1" face="Verdana">Your

  Message :</font></b></td>

  <td width="70%"><textarea rows="8" name="message" cols="40"></textarea></td>

  </tr>

  </table>


</td>

  </tr>

  </table>

  <p><input type="submit" value="Send Message" name="func"> <input type="reset" value="Clear All" name="func"></p>

  </form>


</center>


</body>

  </html>




 

Read More

Custom Search



Gazatem Technologies Active News Manager


page counter

Listed with 411asp.net Listed with 4aspin.com