Knowledgebase

Using CDOSys to Create an ASP Mail Form that Uses SMTP Authentication

Posted by on November 23 2013 07:46 PM

Authentication is required at Hostmysite. You can use the mail server assigned to your account as long as you use an email address and password that you've setup on that server. Alternatively, you can use any valid email address and password combination that exists on an external mail server as long as that server supports SMTP connections and you obtain the server or host name and port information for it. The below example consists of two files. One that is the actual form your customers will fill out and the second is the action file that will authenticate and send the email.

**This form can be configured to your liking**

I. Create the Form: Copy the below code into your editor of choice and save it as contactform.asp

________________________________________

<html>
<form name="contactform" method="post" action="send_form_email.asp">
<table width="450px">
<tr>
<td valign="top">
<label for="first_name">First Name *</label>
</td>
<td valign="top">
<input type="text" name="first_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top"">
<label for="last_name">Last Name *</label>
</td>
<td valign="top">
<input type="text" name="last_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top"">
<label for="home_address">Home Address</label>
</td>
<td valign="top">
<textarea name="home_address" maxlength="100" cols="25 Rows="6"></textarea>
</td>
</tr>
<tr>
<td valign="top">
<label for="email">Email Address *</label>
</td>
<td valign="top">
<input type="text" name="email" maxlength="80" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="telephone">Telephone Number</label>
</td>
<td valign="top">
<input type="text" name="telephone" maxlength="30" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="comments">Comments *</label>
</td>
<td valign="top">
<textarea name="comments" maxlength="1000" cols="25" rows="6"></textarea>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center"> * - Denotes a required field
<input type="submit" value="Submit">
</td>
</tr>
</table>
</form>
</html>

________________________________________

 

II. Create the Send Form Script: Copy the below code and save it as send_form_email.asp (do not change the file name unless you changed it in the contactform.asp code above)

________________________________________

<%
Dim email_to, email_subject, host, username, password, reply_to, port, from_address
Dim first_name, last_name, home_address, email_from, telephone, comments, error_message
Dim ObjSendMail, email_message

email_to = "name@yourdomain.xyz"  'Enter the email you want to send the form to
email_subject = "This is a test"  'You can put whatever subject here
host = "mail.yourdomain.xyz"   'The mail server name. (Commonly mail.yourdomain.xyz if your mail is hosted with HostMySite) 
username = "name@yourdomain.xyz"  'A valid email address you have setup
from_address = "name@yourdomain.xyz" 'If your mail is hosted with HostMySite this has to match the email address above
password = "your_password"  'Password for the above email address
reply_to = "name@yourdomain.xyz"  'Enter the email you want customers to reply to
port = "25" 'This is the default port. Try port 50 if this port gives you issues and your mail is hosted with HostMySite

Sub Died(errors)
    'Your error code can go here
    Response.write("We are very sorry, but there were error(s) found with the form you submitted. These errors appear below.<br /><br />")
    Response.write(errors & "<br /><br />")
    Response.write("Please go back and fix these errors.<br /><br />")
    Response.End
End Sub

'Validate expected data exists
If Request.Form("first_name") = "" Or Request.Form("last_name") = ""  Or Request.Form("email") = ""  Or Request.Form("comments") = "" Then
    Call Died("Required field/s have not been entered")
End If

first_name = Request.Form("first_name")  'required
last_name = Request.Form("last_name")  'required
home_address = Request.Form("home_address")  'not required
email_from = Request.Form("email")  'required
telephone = Request.Form("telephone")  'not required
comments = Request.Form("comments")  'required

Dim rg
Set rg = New RegExp
rg.Global = True

rg.Pattern = "^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$"
If Not rg.Test(Request.Form("email")) Then
    error_message = error_message & "The Email Address you entered does not appear to be valid.<br />"
End If

rg.Pattern = "^[A-Za-z .'-]+$"
If Not rg.Test(Request.Form("first_name")) Then
    error_message = error_message & "The First Name you entered does not appear to be valid.<br />"
End If

If Not rg.Test(Request.Form("last_name")) Then
    error_message = error_message & "The Last Name you entered does not appear to be valid.<br />"
End If

If Len(comments) < 2 Then
    error_message = error_message & "The Comments you entered do not appear to be valid.<br />"
End If

If Not error_message = "" Then
    Call Died(error_message)
End If

Function CleanString(str)
    Dim bad(5)
    bad(0) = "content-type"
    bad(1) = "bcc:"
    bad(2) = "to:"
    bad(3) = "cc:"
    bad(4) = "href"
    For Each Item in bad
        str = Replace(str, Item, "")
    Next
    CleanString = str
End Function

email_message = "Form details below.<br /><br />"
email_message = email_message & "First Name: " & CleanString(first_name) & "<br />"
email_message = email_message & "Last Name: " & CleanString(last_name) & "<br />"
email_message = email_message & "Home Address: " & CleanString(home_address) & "<br />"
email_message = email_message & "Email: " & CleanString(email_from) & "<br />"
email_message = email_message & "Telephone: " & CleanString(telephone) & "<br />"
email_message = email_message & "Comments: " & CleanString(comments) & "<br />"

Set ObjSendMail = CreateObject("CDO.Message")
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = host
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = port
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = username
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = password
ObjSendMail.Configuration.Fields.Update

ObjSendMail.To = email_to
ObjSendMail.Subject = email_subject
ObjSendMail.From = from_address

ObjSendMail.HTMLBody = email_message

'This section sends the email
On Error Resume Next
ObjSendMail.Send

If err.number <> 0 Then
    'Include your own failure message html here
    Response.write("Unfortunately, the message could not be sent at this time. Please try again later.")
    
    'Uncomment the line below to see errors with sending the message
    'Response.write("<br />Error: " & err.description)
Else
    'Include your own success message html here
    Response.write("Thank you for contacting us. We will be in touch with you very soon.")
End If

set ObjSendMail = Nothing

%>

________________________________________


III. Upload the Files: To test the files you created, FTP to your domain and create a test folder (i.e. formtest). Upload the 2 files to that directory. Once you have uploaded the files, browse to http://www.yourdomain.xyz/formtest/contactform.asp (or whatever you named the contact form from Step 1)

As long as you used an email address that you are hosting with HostMySite along with the proper host mail server, the email message should have arrived without error.

(20 vote(s))
This article was helpful
This article was not helpful

Comments (0)
Copyright © 2016 HostMySite