Email from survey

To send a email from your survey is easy to implement as long as your network allows. In this example we will use IIS and SMTP but you should be able to use other email servers.  Once the software is setup you can start. The first thing we need in our survey is a text box to collect the email address, so create a mdd with the following code in it.

	' Metadata section
	Email "Q. Please enter your Email Address" text [..50];

	' Routing Section

Now we could just leave it at that, however the user could type in anything and not a valid email address. So what we really need to do is to add some validation code into the script. To do this in dimensions you can use regular expressions , for this example we will be using a simple regular expression so some invalid email address’s may get through, but if you want some stronger checking , just Google “Regular expressions” there are many sites out there providing many complex expressions.  So , what code do we need to add into the script to do the validation. Enter the following into your routing section.

Dim bValid
  bValid = Validate(Email,,,"[-.w]+@[-.w]+")
  If bValid = false then
   Email.Errors.AddNew("Email", "Invalid Email address")
   Goto TryEmail
  end if

Now when we run the script and we try and enter a invalid email address the survey will loop back and ask the respondent to enter a new one. When we have a valid email address we can go ahead and ask the rest of the survey. When we are at the stage we need to send the email address we need to add the following code into the script.

 Dim oEmail
 Set oEmail = CreateObject("CDO.Message")
  oEmail.From = ""
  oEmail.To = Email.Response.Value
  oEmail.Subject = "This is the subject"
  oEmail.HTMLBody = "This is the body of the email"
 Set oEmail = null

The above code is very simple and if you look at the oEmail methods and properties in studio you will see that there is allot more you can do, for example you might want to attach a file or just send a plain text email. So lets explain the code .

Dim oEmail declare a variable to hold the email object
Set oEmail = CreateObject(“CDO.Message”) create the email object using the collaboration do object 
oEmail.From = “” set the value of the FROM property. This will show who the email is from.
oEmail.To = Email.Response.Value set the value of the TO property with the value stored in the email question.
oEmail.Subject = “This is the subject” set the subject of the email
oEmail.HTMLBody = “This is the body of the email” set the bodytext of the email using the html body property. Using this properties allows you to send an html email
oEmail.Send() Send the email.
Set oEmail = null close the email object and release it from memory

Leave a Comment

%d bloggers like this: