Search code examples
grailsgrails-2.0grails-controller

Grail mail plugin - unable to add in multiple TO addresses?


New to grails world. I am having issues passing the comma seperated form values from a form into the 'TO' properties of the grails mail plugin. no matter what I try I can seem to get it to an array or accept multiple email addresses.

Im getting the following error; I have removed the domain names so please disregard them.

Could not parse mail; nested exception is javax.mail.internet.AddressException:
Illegal address in string ``"ian@email.com","ian@otheremail.com"''

Scratching head now, any ideas of where I am going wrong with the below code. I'm completely stumped.!!

in emailpublication GSP form I have. these fields are automatically populated from the database and id of document.

<g:form action="emailpublication">

<g:field type="text" name="whogetsemail" value="${publicationInstance?.portfolio?.emailtemplates?.toemailtemplate}" /> 
<g:field type="text" name="publicationName" value="${publicationInstance?.publicationName}" />
<g:textArea type="text" name="publicationContent" value="${publicationInstance?.publicationContent}" cols="20" rows="20"/> 
<g:hiddenField name="id" value="${publicationInstance?.id}" />

<input type="submit" value="Send Email"/>
</g:form>

In Publication controller I have the following; I need to send TO on one or more email address populated from the whogetsemail field in the emailpublication gsp form

def emailpublication(){

  List<String> recipients = request.getParameterValues("whogetsemail")
    try {
     sendMail{
             to  (recipients.toArray())
             from "ineilsen@emailaddress.com"
             subject params.publicationName
             text params.emailbodyheader + "\n"+"\n" + params.publicationContent + "\n"+"\n" + params.footeremailtemplate
         }

    }
    catch (MailException e) {
        log.error "Failed to send emails: $e.message", e
    }
    catch (MessagingException e) {
        log.error "Failed to send emails: $e.message", e
    }
    redirect(uri: "/publication/show/${params}")
            flash.message = "${params.publicationName} sent to ${params.emailto}"
}

Thanks all, look forward to replies, Im frustrated with


Solution

  • The plugin supports multiple addresses, and they can be in an Object[] array or a List. But you're getting a single comma-delimited string since there's only one whogetsemail field. request.getParameterValues() only returns multiple values if there are multiple inputs with the same name.

    It should work if you change it to this:

    List<String> recipients = params.whogetsemail.split(',').collect { it.trim() }
    

    and

    to recipients