Link to home
Start Free TrialLog in
Avatar of shamskm
shamskm

asked on

submitting a form and sending an email (using JSP)

Hi there,

I am completley new to JSP (my background is in PHP).

I am trying to write a JSP script that will take the values of a html <form> and append them to an email.

For example, here are two sample <form>'s

<form name="test_form_1" id="test_form_1" method="post" action="email_form.jsp">
Full Name: <input type="text" name="full_name" id="full_name"  />
Address: <input type="text" name="address" id="address"  />
Telephone: <input type="text" name="telephone" id="telephone"  />
</form>

<form name="test_form_2" id="test_form_2" method="post" action="email_form.jsp">
Appointment Day: <input type="text" name="appointment_day" id="appointment_day"  />
Appointment Date: <input type="text" name="appointment_date" id="appointment_date"  />
Appointment Time: <input type="text" name="appointment_time" id="appointment_time"  />
</form>

I would like the email_form.jsp script to handle form elements that are not predetermined.  i.e. a script that will iterate through any submitted form elements, apend them to an email, and then send the email.

I would expect the resulting email for the above two processed forms to resemble something like this:

full_name: value
address: value
telephone:value

and

appointment_day: value
appointment_date: value
appointment_time: value

Here is my email_form.jsp script so far

Obviously lots are missing, how do i get all the varibales from a submitted form and make string out of it?

I'd also like to pass the "from" email address as a hidden value in the form.

==============================================================

String to = "";

String subject = "Feedback received from form name: ";

String from = "";


try
{
      mail.postMail(to,subject,message,from);
      confmessage = "<strong>Thank you, email sent</strong> ";
}
catch (Exception e)
{
      confmessage = "<strong>Unfortunatly your message could not be sent.</strong>";  
}

==============================================================

Thanks for looking! Your help is much appreciated.

ShamzZ
ASKER CERTIFIED SOLUTION
Avatar of Peter Kwan
Peter Kwan
Flag of Hong Kong image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shamskm
shamskm

ASKER

Hi Peter,

Thats wonderful and works like a charm, thanks.

Is it possible to make a couple of small requests to develop the script further:

Firstly, instead of displaying the submitted form values with:

<%= paramName %> : <%= paramValue[0] %><br/>

instead, I have appended them to a string using:

String email_body = "";
email_body += paramName + " : " + paramValue[0] + "\n";

But because <br> won't be recognised as a line break in text-only email applications, is there something else I can use as a linebreak.  I've tried "\n" but that doesn't seem to work?

Secondly the form values get appended in alphabetical order (e.g. address, name, telephone).  Is it possible to append the form values in the order they appear in the original HTML form ? (I'm guessing not?).

Thanks for your help,

ShamzZ

For the second question, since a map is not sorted by its nature. It is not possible to sort according to the form order without looping original form input fields.

For the first question,  what is your content type of the message ? Is it text/html or text/plain?
Avatar of shamskm

ASKER

Well, I am sending the email using:

try
{
      mail.postMail(to,emailsubject,test,emailfrom);
      confmessage = "Thank you. Your email has been sent successfully";  
}
catch (Exception e)
{
      confmessage = "Unfortunatly your message could not be sent..";
}

How would I set the content type? I would like to send as text/plain...

Thanks,
ShamzZ
What is your type of "mail" object?
Avatar of shamskm

ASKER


hmm, now you got me. i'm not sure.

Does this help clarify:

<jsp:useBean id="mail" class="tdcbeans.MailExt" scope="page" />

?
Is this MailExt your own class?
Avatar of shamskm

ASKER

I'm not sure if its a custom class (as there is a lot of code left over from previous developers), but here is the corresponding code for MailExt:

===================================================

package tdcbeans;

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

public class MailExt
{
      public void postMail( String recipients[ ], String subject, String message , String from)
      throws MessagingException
      {
    boolean debug = false;

     //Set the host smtp address
     Properties props = new Properties();
       //smtp address
     //props.put("mail.smtp.host", "10.1.70.12");
     props.put("mail.smtp.host", "195.195.26.248");
       //props.put("mail.smtp.host", "10.101.10.254");
      
    // create some properties and get the default Session
    Session session = Session.getDefaultInstance(props, null);
    session.setDebug(debug);

    // create a message
    Message msg = new MimeMessage(session);

    // set the from and to address
    InternetAddress addressFrom = new InternetAddress(from);
    msg.setFrom(addressFrom);

    InternetAddress[] addressTo = new InternetAddress[recipients.length];
    for (int i = 0; i < recipients.length; i++)
          {
        addressTo[i] = new InternetAddress(recipients[i]);
          }
    msg.setRecipients(Message.RecipientType.TO, addressTo);
   

    // Optional : You can also set your custom headers in the Email if you Want
    msg.addHeader("MyHeaderName", "myHeaderValue");

    // Setting the Subject and Content Type
    msg.setSubject(subject);
    msg.setContent(message, "text/html");
    Transport.send(msg);
      }
}

You have set the message type to "text/html". If you need to set it as "text/plain", you can modify the line:

 msg.setContent(message, "text/html");

to

 msg.setContent(message, "text/plain");

And "\n" should work
Avatar of shamskm

ASKER


Brilliant, thanks for all your help Peter.