Secciones

Artículos para tus primeros pasos

Si estás empezando a introducirte en el mundo de Groovy y Grails, no te pierdas nuestros artículos básicos: 

Entrevistas con los expertos
 

Los protagonistas te cuentan de qué van los proyectos más importantes del mundo Groovy:


Un proyecto de:
ImaginaWorks
Campus Escuela de Groovy

Grails

Envío de mails en Grails

Luis Angel - martes 28/04/2009
Hola, aparte del plugin de mails, hay alguna forma implícita dentro de Grails para enviar mails con attachs? O conviene hacerlo a mano?

Re: Envío de mails en Grails

Nacho - martes 28/04/2009

Hola,

no hay nada "oficial" en Grails, aunque es muy fácil hacerlo con un servicio. Te dejo la implementación que usamos nosotros:

/* =============================================================================

  Copyright 2008 ImaginaWorks Software Factory, S.L.

 

  Licensed under the Apache License, Version 2.0 (the "License");

  you may not use this file except in compliance with the License.

  You may obtain a copy of the License at

 

      http://www.apache.org/licenses/LICENSE-2.0

 

  Unless required by applicable law or agreed to in writing, software

  distributed under the License is distributed on an "AS IS" BASIS,

  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

  See the License for the specific language governing permissions and

  limitations under the License.

================================================================================= */

 

import org.apache.commons.logging.LogFactory

import com.imaginaworks.srvc.email.Email

 

 

class MailService {

 

boolean transactional = false

def sendTXT(String smtp, String from, String toaddress, String content, String msgSubject) {

   log.info("Enviando correo desde ${from} a ${toaddress}. Asunto: ${msgSubject}, Cuerpo:\n${content}")

   def usr = null

   def pwd = null

   if(IWConfig.list()[0].mailAuth){

    usr = IWConfig.list()[0].mailUsr

    pwd = IWConfig.list()[0].mailPwd

   }

   try{

   Email.sendEmail(

           smtp,

           usr,

           pwd,

           from,

           toaddress,

           msgSubject,

           content

   );    

   }

   catch(Exception x){

    log.error("Error al enviar el correo: ${x}")

    x.printStackTrace();

   }

}

 

def sendHTML(String smtp, String from, String toaddress, String content, String msgSubject) {

   log.info("Enviando correo desde ${from} a ${toaddress}. Asunto: ${msgSubject}, Cuerpo:\n${content}")

   def usr = null

   def pwd = null

   if(IWConfig.list()[0].mailAuth){

    usr = IWConfig.list()[0].mailUsr

    pwd = IWConfig.list()[0].mailPwd

   }    

   try{

    Email.sendHTMLEmail(

           smtp,

           usr,

           pwd,

           from,

           toaddress,

           msgSubject,

           wrapWithHtml(content)

   );  

   }

   catch(Exception x){

    log.error("Error al enviar el correo: ${x}")

    x.printStackTrace();

   }

   

}

 

def wrapWithHtml(msg) {

def wrapped = """

<html>

     <head>

     <style>

     body {

     color: #333;

     font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif;

     font-size: 0.8em;

     line-height: 1.8em;

     text-align: left;

     background-off: #E6E6E6;

     }

 

     a {

     color: #005880;

     }

 

     a:hover {

     color: #006B95;

     }

 

     a:visited {

     color: #006B95;

     }

 

     a:visited:hover {

     color: #2C91B2;

     }

 

     h1{

     font-family: "Arial Narrow",Tahoma, Geneva, Arial, Helvetica, sans-serif;

     font-size: 1.6em;

     color: #006B95;

     margin: 15px 0 50px 0;

     padding-left: 15px;

     }

 

     p {

     margin: 10px 15px 5px 15px;

     }

 

     </style>

     </head>

     <body>

     ${msg}

</body>

</html>

"""

return wrapped

}

}

----------------------------

Nota: IWConfig es una clase de entidad donde guardamos la configuración de la aplicación, para tenerla en la base de datos en vez de ficheros de texto.

 

Espero que te sirva! 

 

Responder al hilo | Volver al foro "Grails" | Volver a los foros