{"id":14923,"date":"2014-07-24T19:29:09","date_gmt":"2014-07-24T13:59:09","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=14923"},"modified":"2024-01-02T17:50:04","modified_gmt":"2024-01-02T12:20:04","slug":"email-tracking-using-mailgun-with-grails-application","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/email-tracking-using-mailgun-with-grails-application\/","title":{"rendered":"Email Tracking using MailGun with Grails application"},"content":{"rendered":"<p>Many times we need to track whether the email is open\/deliver\/read by users, sent from our application. After analyzing, I concluded that MailGun provides email tracking services that i used  with my grails application and thought it worth sharing.<\/p>\n<p>There are two ways to send messages using MailGun:<\/p>\n<p>#1. HTTP API<br \/>\n#2. SMTP<\/p>\n<p>I have used HTTP API and MailGun uses Jersey Request Builder.So, add following dependency:<\/p>\n<p>[java]<br \/>\ncompile &quot;:jersey-request-builder:1.2.1&quot;<br \/>\n[\/java]<\/p>\n<p>The steps involved in using MailGun by Rackspace in our grails application are as:-<\/p>\n<p>#1.SignUp With MailGun:<\/p>\n<p>SignUp with Mailgun <a title=\"Here\" href=\"https:\/\/mailgun.com\/signup\" target=\"_blank\" rel=\"noopener\">HERE<\/a> and you will get the following code.<\/p>\n<p>[java]<br \/>\npublic static ClientResponse SendSimpleMessage() {<br \/>\n    Client client = Client.create();<br \/>\n    client.addFilter(new HTTPBasicAuthFilter(&quot;api&quot;,<br \/>\n                &quot;${YOUR_API_KEY}&quot;));<br \/>\n    WebResource webResource =<br \/>\n        client.resource(&quot;https:\/\/api.mailgun.net\/v2\/${YOUR_MAILGUN_SUB_DOMAIN}\/messages&quot;);<br \/>\n    MultivaluedMapImpl formData = new MultivaluedMapImpl();<br \/>\n    formData.add(&quot;from&quot;, &quot;Mailgun Sandbox &lt;postmaster@${YOUR_MAILGUN_SUB_DOMAIN}&gt;&quot;);<br \/>\n    formData.add(&quot;to&quot;, &quot;${EMAIL_ID_YOU_WANT_TO_SEND_TO}&quot;);<br \/>\n    formData.add(&quot;subject&quot;, &quot;Hello ${ACCOUNT_NAME_YOU_REGISTERED_WITH}&quot;);<br \/>\n    formData.add(&quot;text&quot;, &quot;Congratulations ${ACCOUNT_NAME_YOU_REGISTERED_WITH}, you just sent an email with Mailgun!  You are truly awesome!  You can see a record of this email in your logs: https:\/\/mailgun.com\/cp\/log .  You can send up to 300 emails\/day from this sandbox server.  Next, you should add your own domain so you can send 10,000 emails\/month for free.&quot;);<br \/>\n    return webResource.type(MediaType.APPLICATION_FORM_URLENCODED).<br \/>\n                                                post(ClientResponse.class, formData);<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>#2.Log In to MailGun <a title=\"Here\" href=\"https:\/\/mailgun.com\/sessions\/new\" target=\"_blank\" rel=\"noopener\">here<\/a> and go to Logs section.And enter a call back url on which mailgun let&#8217;s you know about Delivery of messages and also you can a enter a call back for failed messages, as can be seen in following image:<\/p>\n<p><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/MailDelievryCallBack_ljiwea.png\" alt=\"ForMailGunDelivery CallBack\" \/><\/p>\n<p>#3.Similary go to Tracking section.And enter a call back url on which mailgun let&#8217;s you know about Opens event of messages and also you can a enter a call back for Clicks event messages, as can be seen in following image:<\/p>\n<p><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/MailOpenedCallBack_aeuei6.png\" alt=\"ForMailGunOpenedCallBack\" \/><\/p>\n<p>#4.Add following actions to send mails through MailGun and to handle callBacks provided to MailGun.<\/p>\n<p>a) action to send mail-<\/p>\n<p>[java]<br \/>\n  def sendSimpleMessage() {<br \/>\n        log.debug &quot;::::Exceuting mail::::&quot;<br \/>\n        Client client = Client.create();<br \/>\n        client.addFilter(new HTTPBasicAuthFilter(&quot;api&quot;,<br \/>\n                &quot;${grailsApplication.config.mailGun.api}&quot;));<br \/>\n        WebResource webResource =<br \/>\n                client.resource(&quot;${grailsApplication.config.mailGun.webResource}&quot;);<br \/>\n        MultivaluedMapImpl formData = new MultivaluedMapImpl();<br \/>\n        formData.add(&quot;from&quot;, &quot;${grailsApplication.config.mailGun.mail.from}&quot;);<br \/>\n        formData.add(&quot;to&quot;, &quot;${EMAIL_ID_YOU_WANT_TO_SEND_TO}&quot;);<br \/>\n        formData.add(&quot;subject&quot;, &quot;Hello MediaIQ&quot;);<br \/>\n        formData.add(&quot;text&quot;, &quot;Congratulations, you just sent an email with Mailgun!&quot;);<br \/>\n        \/\/use html instead text for sending html content<br \/>\n        formData.add(&quot;o:tracking&quot;, true);<br \/>\n        \/\/Set this to true to track opens event<br \/>\n        webResource.type(MediaType.APPLICATION_FORM_URLENCODED).<br \/>\n                post(ClientResponse.class, formData);<br \/>\n        render &quot;success&quot;<br \/>\n    }<br \/>\n[\/java]<\/p>\n<p>b) action to handle delivery call back-<\/p>\n<p>[java]<br \/>\n    def mailDeliveryCallBack() {<br \/>\n        boolean isMailSent = false<br \/>\n        List headerData = (JSON.parse(params.get(&quot;message-headers&quot;).toString())) as List<br \/>\n        headerData.each { List header -&gt;<br \/>\n            if (header[0] == &quot;Received&quot;) {<br \/>\n                isMailSent = true<br \/>\n              } else if (header[0] == &quot;To&quot;) {<br \/>\n                User user = User.findByEmail(header[1].toString())<br \/>\n                user.isMailSent = true<br \/>\n                user.save()<br \/>\n              }<br \/>\n        }<br \/>\n    }<br \/>\n[\/java]<\/p>\n<p>b) action to handle opens call back-<\/p>\n<p>[java]<br \/>\n    def mailOpenedCallBack() {<br \/>\n        boolean isMailOpened = false<br \/>\n        isMailOpened = params.get(&quot;event&quot;).toString().equals(&quot;opened&quot;)<br \/>\n        User user = User.findByEmail(params.get(&quot;recipient&quot;).toString())<br \/>\n        user.isMailOpened = isMailOpened<br \/>\n        user.save()<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>This worked for me.<br \/>\nHope it helps.<\/p>\n<p>Useful Links:<\/p>\n<p><a title=\"http:\/\/documentation.mailgun.com\/user_manual.html\" href=\"http:\/\/documentation.mailgun.com\/user_manual.html\" target=\"_blank\" rel=\"noopener\">http:\/\/documentation.mailgun.com\/user_manual.html<\/a><\/p>\n<p>Cheers!!!<br \/>\nVivek Garg<br \/>\nvivek.garg@intelligrape.com<br \/>\n<a title=\"www.intelligrape.com\" href=\"http:\/\/www.tothenew.com\/blog\/\" target=\"_blank\" rel=\"noopener\">www.intelligrape.com<\/a><br \/>\nMy LinkedIn<br \/>\n<a title=\"MORE BLOGS BY ME\" href=\"http:\/\/www.tothenew.com\/blog\/author\/vivek-garg\/\" target=\"_blank\" rel=\"noopener\">MORE BLOGS BY ME<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many times we need to track whether the email is open\/deliver\/read by users, sent from our application. After analyzing, I concluded that MailGun provides email tracking services that i used with my grails application and thought it worth sharing. There are two ways to send messages using MailGun: #1. HTTP API #2. SMTP I have [&hellip;]<\/p>\n","protected":false},"author":69,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3},"categories":[7],"tags":[4840,1486,1487],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/14923"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/69"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=14923"}],"version-history":[{"count":1,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/14923\/revisions"}],"predecessor-version":[{"id":59902,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/14923\/revisions\/59902"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=14923"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=14923"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=14923"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}