How to receive emails in Grails?

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

How to receive emails in Grails?

gullet
Hi,

I can't figure out whether it's possible or not to receive emails with the Mail Plugin. I'm using the Mail Plugin currently to send emails, but I really would like to know how to receive them (responses to the originally sent emails) as well. If it's not possible with the Mail Plugin only, what are the requirements?

Any hints are appreciated.
Reply | Threaded
Open this post in threaded view
|

Re: How to receive emails in Grails?

Göran Ehrsson
Hi,

It's not possible to read email with the mail plugin.
I don't know if there exist an IMAP or POP grails plugin.
But the basic code to read an IMAP mailbox i quite simple.
I have an example here
http://www.technipelago.se/content/technipelago/blog/29

Regards,

/Göran

gullet wrote2010-08-18 10:25:

> Hi,
>
> I can't figure out whether it's possible or not to receive emails with the
> Mail Plugin. I'm using the Mail Plugin currently to send emails, but I
> really would like to know how to receive them (responses to the originally
> sent emails) as well. If it's not possible with the Mail Plugin only, what
> are the requirements?
>
> Any hints are appreciated.
>    


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: How to receive emails in Grails?

Ian Roberts
In reply to this post by gullet
gullet wrote:
> Hi,
>
> I can't figure out whether it's possible or not to receive emails with the
> Mail Plugin. I'm using the Mail Plugin currently to send emails, but I
> really would like to know how to receive them (responses to the originally
> sent emails) as well. If it's not possible with the Mail Plugin only, what
> are the requirements?

Grails doesn't know anything about receiving mail by SMTP.  The approach
you'd need to use depends on many factors.

If the emails are arriving on a server where you can do something like
procmail or an Exim filter to process them as they arrive, then you
could set this up to use something simple like curl or lwp-request to
HTTP POST the incoming mail message to a controller in your Grails app
and handle it there.

If you can't insert a process like that at the point of delivery but you
do have direct access to the mailbox on disk then you could process that
on a regular basis using a quartz job (with Maildir this is easy, with
mbox you need to be very careful with file locking to make sure you
don't corrupt the mailbox or lose mail).  If you can only access the
messages over IMAP/POP then you'd have to use something like the
JavaMail API to poll the mailbox (again from a quartz job or similar).

Ian

--
Ian Roberts               | Department of Computer Science
[hidden email]  | University of Sheffield, UK

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: How to receive emails in Grails?

smaldini
Take also a look at Camel Plugin, as far as I know there is a mail poller component (imap or smtp).

On Wed, Aug 18, 2010 at 12:17 PM, Ian Roberts <[hidden email]> wrote:
gullet wrote:
> Hi,
>
> I can't figure out whether it's possible or not to receive emails with the
> Mail Plugin. I'm using the Mail Plugin currently to send emails, but I
> really would like to know how to receive them (responses to the originally
> sent emails) as well. If it's not possible with the Mail Plugin only, what
> are the requirements?

Grails doesn't know anything about receiving mail by SMTP.  The approach
you'd need to use depends on many factors.

If the emails are arriving on a server where you can do something like
procmail or an Exim filter to process them as they arrive, then you
could set this up to use something simple like curl or lwp-request to
HTTP POST the incoming mail message to a controller in your Grails app
and handle it there.

If you can't insert a process like that at the point of delivery but you
do have direct access to the mailbox on disk then you could process that
on a regular basis using a quartz job (with Maildir this is easy, with
mbox you need to be very careful with file locking to make sure you
don't corrupt the mailbox or lose mail).  If you can only access the
messages over IMAP/POP then you'd have to use something like the
JavaMail API to poll the mailbox (again from a quartz job or similar).

Ian

--
Ian Roberts               | Department of Computer Science
[hidden email]  | University of Sheffield, UK

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email





--
Stéphane MALDINI
doc4web consultant
[hidden email]
--
http://fr.linkedin.com/in/smaldini
Reply | Threaded
Open this post in threaded view
|

Re: How to receive emails in Grails?

gullet
In reply to this post by Göran Ehrsson
Goran,

Thanks for your help and the link!

Best regards, gullet
Reply | Threaded
Open this post in threaded view
|

RE: How to receive emails in Grails?

wwwclaes
In reply to this post by gullet
Hi, here's another recent thread on the subject (with some code):

http://grails.1312388.n4.nabble.com/Advice-on-bounced-noreply-mails-td2315429.html#a2315429

If I'm lucky this could be outsourced to Postmarkapp...

http://help.postmarkapp.com/discussions/suggestions/36-webhook-to-parse-replies
http://help.postmarkapp.com/discussions/suggestions/23-feature-request-webhook-to-parse-replies

...otherwise I'll probably give it a go myself in a few weeks/months from now.
I will let you know if I come up with anything useful and would also be
interested to hear if you do as well.

/Claes

> Date: Wed, 18 Aug 2010 01:25:58 -0700
> From: [hidden email]
> To: [hidden email]
> Subject: [grails-user] How to receive emails in Grails?
>
>
> Hi,
>
> I can't figure out whether it's possible or not to receive emails with the
> Mail Plugin. I'm using the Mail Plugin currently to send emails, but I
> really would like to know how to receive them (responses to the originally
> sent emails) as well. If it's not possible with the Mail Plugin only, what
> are the requirements?
>
> Any hints are appreciated.
> --
> View this message in context: http://grails.1312388.n4.nabble.com/How-to-receive-emails-in-Grails-tp2329500p2329500.html
> Sent from the Grails - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
> http://xircles.codehaus.org/manage_email
>
>
Reply | Threaded
Open this post in threaded view
|

Re: How to receive emails in Grails?

ishe
In reply to this post by gullet
-> Exim (postfix) -> dbmail -> and then read database
Reply | Threaded
Open this post in threaded view
|

Re: How to receive emails in Grails?

Roshan Shrestha
In reply to this post by gullet
I would look at the Camel plugin, easy to setup.
Reply | Threaded
Open this post in threaded view
|

Re: How to receive emails in Grails?

pgampel
In reply to this post by gullet
You can use javax.mail

Here is an example of a class function that returns a collection of messages and does some string parsing:

def getPOP(qualifier, parselines, multilinefields, delete) {
                //function will return a list of maps. Each map is an array of message lines or just one large copy of the body
                //if using with parselines then the caller must assign each map to a field
                //if not using parsefields than just use body
                def pmsgs = []
                java.util.Properties properties = new java.util.Properties()
                properties.setProperty("mail.pop3.timeout", Setting.findByName('poptimeout').setting)
                properties.setProperty("mail.pop3.connectiontimeout", Setting.findByName('poptimeout').setting)
                Session pop3Session = javax.mail.Session.getInstance(properties, null)
                Store store = pop3Session.getStore("pop3");
                Folder inbox
                try {
                        store.connect(Setting.findByName('popserver').setting, Setting.findByName('popaccount').setting,Setting.findByName('poppassword').setting)
                        inbox = store.getFolder("INBOX")
                        inbox.open(Folder.READ_WRITE)
                        Message[] msgs=inbox.getMessages()
                    println "total emails:" + msgs.size()
                        msgs.each {
                                try {
                                        if (qualifier.contains(it.getSubject())) {
                                                println "trying email:" + it.getSubject()
                                                if (it.getContentType().contains("multipart")) {
                                                        for ( i in 0..it.getContent().getCount()-1 ) {    
                                                            Multipart mp = (Multipart)it.getContent()
                                                                BodyPart body_part = mp.getBodyPart(i)
                                                                if (body_part.getContentType().contains("text/plain")) {
                                                                        def edata = ['msgsubject':it.getSubject()]
                                                                  edata += ['msgfrom':it.getFrom()[0].toString()]
                                                                        if (parselines) {
                                                                                def lastkey
                                                                                body_part.getContent().toString().split('\n').each {
                                                                                        def eline = it.split(':')
                                                                                        if (eline.size()>1) {
                                                                                                edata[eline[0].trim().toLowerCase()]  = eline[1].trim()
                                                                                                lastkey=eline[0].trim().toLowerCase()
                                                                                        } else if (eline[0].trim() !='' && lastkey && multilinefields) {
                                                                                                edata[lastkey]  += '\n' + eline[0].trim().toLowerCase()
                                                                                        }
                                                                                }
                                                                                //if (lastkey) pmsgs += edata
                                                                        }
                                                                        edata['body'] = body_part.getContent()
                                                                        pmsgs += edata
                                                                }
                                                                else throw new Exception("email has no text/plain multipart:" + it.getSubject())
                                                        }
                                                }
                                                else {
                                                        def edata = ['msgsubject':it.getSubject()]
                                                        edata += ['msgfrom':it.getFrom()[0].toString()]
                                               
                                                        if (it.getContent() instanceof javax.mail.util.SharedByteArrayInputStream) content = new String(it.getContent().buf)
                                                        else content = it.getContent().toString()
                                                                               
                                                        if (parselines) {
                                                                def lastkey
                                                                content.split('\n').each {
                                                                        def eline = it.split(':')
                                                                        if (eline.size()>1) {
                                                                                edata[eline[0].trim().toLowerCase()]  = eline[1].trim()
                                                                                lastkey=eline[0].trim().toLowerCase()
                                                                        } else if (eline[0].trim() !='' && lastkey) {
                                                                                edata[lastkey]  += '\n' + eline[0].trim().toLowerCase()
                                                                        }
                                                                }
                                                                //if (lastkey) pmsgs += edata
                                                        }
                                                        edata['body'] = content
                                                        pmsgs += edata
                                                }
                                        }
                                        else throw new Exception("skipping email:" + it.getSubject())
                                }
                                catch (Exception e) {
                                                throw new Exception(e.getMessage())
                                }
                                finally {
                                        if (delete) it.setFlag(Flags.Flag.DELETED, true)
                                }
                        }
                }
                catch (Exception e) {
                                println e.toString()
                                pmsgs += ["MailHandlerException" : e.getMessage()]
                }
                finally {
                        if (inbox && inbox.mode>-1) inbox.close(true)
                        store.close()
                }
                return pmsgs
        }