w3JMail Examples

Examples 4.5

Automated Forms

This is a different way to handle forms. Instead of specifyingeach field as in the forms example, we now read all form fields and paste their values into our e-mail.


First, a sample form. Any form will do as long as it posts to our JMail page.

   JmailForm.asp

<html>
<head>
<title>emailform</title>
</head>
<body>
<form method="post" action="SendMail.asp">
    Name <INPUT name="name" type="text"><br>
Email<INPUT name="Email" type="text"><br>
Company<INPUT name="company" type="text"><br>
Occupation <SELECT name="state">
  <OPTION value="coder">Coder
  <OPTION value="coder">Hacker
  <OPTION value="coder">Developer
  <OPTION value="coder">Guru
 </SELECT><BR>
Favorite TV-show<TEXTAREA name="tv-show"></TEXTAREA>
 <br>
 <INPUT type="submit" value="Send">
</form>
</body>
</html>

Now we use the forms collection to get all the fields and add their values to the e-mail. Unfortunately there is no way for us to control the order of the values.

   SendMail.asp


<%@LANGUAGE = VBSCRIPT%> <html>
<body>
<%

' Create the JMail message Object
set msg = Server.CreateOBject( "JMail.Message" )

' Set logging to true to ease any potential debugging
' And set silent to true as we wish to handle our errors ourself

msg.Logging = true
msg.silent = true

' Most mailservers require a valid email address
' for the sender

msg.From = "test@mydomain.com"
msg.FromName = "My Realname"

' Note that as addRecipient is method and not
' a property, we do not use an equals ( = ) sign

msg.AddRecipient "recipient@hisDomain.com", "His Name"


' The subject of the message
msg.Subject = "How you doin?"

'add every form element and its value to the email
FOR EACH el IN Request.Form
    msg.appendtext( el & ": " & Request.form(el) & vbcrlf )
NEXT
' Now send the message, using the indicated mailserver
if not msg.Send("mail.myDomain.net" ) then
    Response.write "<pre>" & msg.log & "</pre>"
else
    Response.write "Message sent succesfully!"
end if


' And we're done! the message has been sent.

%>
</body>
</html>