Sending emails with groovy from xml file

If you have an xml file containing people’s emails like:

1
2
3
4
5
6
7
<!--?xml version='1.0' encoding='UTF-8'?-->
 
    Mikko Mallikas
    mikko@null
 
    Minni Muodikas
    minni@null

You can easily send simple email messages to them with Groovy:

1
2
3
4
5
6
7
8
9
10
11
def doc = new XmlParser().parse("/tmp/emails.xml")
 
doc.person.email.each {
  doCommand("echo \"Body text here \" | mail -s \"Subject here\" " + it.text())
}
 
def doCommand(command) {
  String[] cmd = ["/bin/sh", "-c", command]
  def proc = Runtime.getRuntime().exec(cmd)
  proc.waitFor()
}

Note that this works only on linux, due the mail program used not found in Windows OS.