Groovy mail shot
A simple mail shot script in groovy...
Runs on Debian Lenny with just a standard openjdk-6-jdk installed, groovy 1.7.2 added by hand and everything else pulled in by Grape.
#!/usr/bin/env groovy
import groovy.sql.Sql
import groovy.grape.Grape
Grape.grab(group: 'mysql', module: 'mysql-connector-java', version: '5.1.6', classLoader: this.class.classLoader.rootLoader)
Grape.grab(group: 'javax.mail', module: 'mail', version: '1.4.3', classLoader: this.class.classLoader.rootLoader)
Grape.grab(group: 'org.apache.ant', module: 'ant-javamail', version: '1.7.1', classLoader: this.class.classLoader.rootLoader)
def sql = Sql.newInstance("jdbc:mysql://localhost:3306/database", "user", "password", "com.mysql.jdbc.Driver")
def content = new StringWriter()
new groovy.xml.MarkupBuilder(content).html {
body {
p('Hi')
p('This is a mail shot!')
p('Best wishes')
p('A groovy mail shot script')
}
}
ant = new AntBuilder()
sql.eachRow("select email from users") { row ->
println "Sending to: ${row.email}"
ant.mail(mailhost: 'localhost', mailport: '25', subject: "Mailshot subject", messagemimetype: 'text/html') {
from(address: 'mailer@demo.domain')
to(address: row.email)
message(content)
}
}