diff --git a/trunk/WEB-INF/classes/siprp/pagina/RequestServlet.java b/trunk/WEB-INF/classes/siprp/pagina/RequestServlet.java index caf4e002..00fe8ce3 100644 --- a/trunk/WEB-INF/classes/siprp/pagina/RequestServlet.java +++ b/trunk/WEB-INF/classes/siprp/pagina/RequestServlet.java @@ -7,9 +7,15 @@ package siprp.pagina; import java.io.*; +import java.util.*; +import javax.mail.*; +import javax.mail.internet.*; import javax.servlet.*; import javax.servlet.http.*; + +import org.apache.velocity.*; +import org.apache.velocity.app.*; /** * * @author psantos @@ -19,8 +25,135 @@ public class RequestServlet extends HttpServlet public void doPost( HttpServletRequest req, HttpServletResponse res ) throws IOException { + ServletOutputStream out = res.getOutputStream(); + res.setContentType( "text/html" ); + + /* + parametros: + request_name + request_phone + request_email + request_details + */ // recebe info de contacto + Hashtable parameters = parseParameters( new Hashtable( req.getParameterMap() ) ); + // manda mail - // mostra pagina de sucesso + boolean success = sendMail( parameters ); + + // mostra pagina correspondente + out.println( showResultPage( success ) ); + } + + private String showResultPage( boolean success ) + { + VelocityContext context = new VelocityContext(); + StringWriter output = new StringWriter(); + String template; + if( success ) + { + template = "pedido_enviado.html"; + } + else + { + template = "pedido_nao_enviado.html"; + } + try + { + Velocity.mergeTemplate( "mail/" + template, Velocity.ENCODING_DEFAULT, context, output ); + return output.toString(); + } + catch( Exception e ) + { + e.printStackTrace(); + } + + return null; + } + + private boolean sendMail( Hashtable parameters ) + { + String smtp_server = "localhost"; + //String from = "psantos@evolute.pt"; + String subject = "Pedido de informação através do site www.siprp.pt"; + String mailer = "Evolute Mailer"; + + // Dados da form de pedido de informacao + String name = ( String ) parameters.get( "request_name" ); + String phone = ( String ) parameters.get( "request_phone" ); + String email = ( String ) parameters.get( "request_email" ); + String details = ( String ) parameters.get( "request_details" ); + + String destination = "psantos@evolute.pt"; + + Properties props = System.getProperties(); + props.put( "mail.smtp.host", smtp_server ); + + Session session = Session.getInstance( props, null ); + + Message msg = new MimeMessage( session ); + try + { + VelocityContext context = new VelocityContext(); + StringWriter content = new StringWriter(); + + msg.setFrom( new InternetAddress( email ) ); + msg.setRecipients( Message.RecipientType.TO, InternetAddress.parse( destination, true ) ); + msg.setSubject( subject ); + + context.put( "name", name ); + context.put( "phone", phone ); + context.put( "email", email ); + context.put( "details", details ); + + Velocity.mergeTemplate( "mail/pedido_informacao.txt", Velocity.ENCODING_DEFAULT, context, content ); + + msg.setContent( content.toString(), "text/plain" ); + + msg.setHeader( "X-Mailer", mailer ); + msg.setSentDate( new Date() ); + Transport.send( msg ); + + return true; + } + catch( Exception e ) + { + e.printStackTrace(); + } + + return false; + } + + private String checkParameter( String parameter ) + { + if( parameter != null && parameter.trim().length() > 0 ) + { + return parameter.trim(); + } + + return null; + } + + public Hashtable parseParameters( Hashtable parameters ) + { + Hashtable tmp = new Hashtable(); + String[] element; + String key; + String new_element; + for( Enumeration e = parameters.keys(); e.hasMoreElements(); ) + { + key = ( String ) e.nextElement(); + element = ( String[] ) parameters.get( key ); + if( element != null ) + { + new_element = checkParameter( element[ 0 ] ); + if( new_element != null ) + { + tmp.put( key, new_element ); + } + } + } + + return tmp; } } diff --git a/trunk/html/conteudos/contactos_text.html b/trunk/html/conteudos/contactos_text.html index a9f76f2e..96f2c423 100644 --- a/trunk/html/conteudos/contactos_text.html +++ b/trunk/html/conteudos/contactos_text.html @@ -4,6 +4,56 @@