I am trying to build a simple form that the action would be mailto:emailaddress then once the person clicks the submit button, they will be directed to a thank you page.
I have 2 questions:
1. Every time I create a form with the action being mailto:emailaddress , when I click submit, my email program open and it wants to send an email with none of the information that was filled out on the form. Why does this happen and how do I set it up so that the form contents are emailed to the person who the form is supposed to be emailed to?
2. How do I redirect the person to a thank you page when clicking the submit button while at the same time the information that was filled out on the form goes to the email that is noted in the action?
Thank you in advance for your help.
Scott LyslePosted Jan 20, 2009, 2:28 AM
You'd have to define a form that would capture that information and then you could use a class like this (you did not state a language so this in VB) to send the message: (and you have to setup the smtp server (one that permits relays))
Imports System.Collections
Imports System.Net.Mail
Imports System.Net.Mime
Imports System.Text.RegularExpressions
Public Class Emailer
'''
''' Transmit an email message
'''
''' Recipient Email Address
''' Sender Email Address
''' Subject Line Describing Message
''' The Email Message Body
''' A string array pointing to the location of each attachment
''' Status Message as String
Public Shared Function SendEmailMessage(ByVal sendTo As String, _
ByVal sendFrom As String, _
ByVal sendSubject As String, _
ByVal sendMessage As String, _
Optional ByVal toBcc As ArrayList = Nothing, _
Optional ByVal attachments As ArrayList = Nothing) As String
Try
' validate recipient email address
Dim bTest As Boolean = ValidateEmailAddress(sendTo)
If (bTest = False) Then
Return "Invalid recipient email address: " + sendTo
End If
' validate sender email address
bTest = ValidateEmailAddress(sendFrom)
If (bTest = False) Then
Return "Invalid sender email address: " + sendTo
End If
' Create the basic message
Dim message As New MailMessage( _
sendFrom, _
sendTo, _
sendSubject, _
sendMessage)
' add blind copy recipients
If (Not toBcc Is Nothing) Then
If (toBcc.Count > 0) Then
Dim sBcc As String
For Each sBcc In toBcc
If (sBcc <> "") Then
bTest = ValidateEmailAddress(sBcc)
If (bTest = False) Then
Return "Invalid Blind Copy email address: " + sBcc
Else
message.Bcc.Add(sBcc)
End If
End If
Next
End If
End If
' The attachments arraylist should point to a file location where
' the attachment resides - add the attachments to the message
If (Not attachments Is Nothing) Then
Dim attach As String
For Each attach In attachments
Dim attached As New Attachment(attach, MediaTypeNames.Application.Octet)
message.Attachments.Add(attached)
Next
End If
' create smtp client at mail server location
Dim client As New SmtpClient(My.MySettings.Default.SMTPAddress)
' Add credentials
client.UseDefaultCredentials = True
' send message
client.Send(message)
Return "Message sent to " + sendTo + " at " + DateTime.Now.ToString() + "."
Catch ex As Exception
Return ex.Message.ToString()
End Try
End Function
'''
''' Confirm that an email address is valid
''' in format
'''
''' Full email address to validate
''' True if email address is valid
Public Shared Function ValidateEmailAddress(ByVal emailAddress As String) As Boolean
Try
Dim TextToValidate As String = emailAddress
Dim expression As New Regex("\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}")
' test email address with expression
If (expression.IsMatch(TextToValidate)) Then
' is valid email address
Return True
Else
' is not valid email address
Return False
End If
Catch ex As Exception
Throw ex
End Try
End Function
End Class