Last active
November 3, 2015 15:16
-
-
Save fmnb0516/888d6714818626e4ba63 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var $ipaddr_push = (function(conf, template) { | |
var interfaces = java.net.NetworkInterface.getNetworkInterfaces(); | |
var list = []; | |
while(interfaces.hasMoreElements() === true) { | |
var nic = interfaces.nextElement(); | |
var addresses = nic.getInetAddresses(); | |
while(addresses.hasMoreElements() === true) { | |
var ip = addresses.nextElement(); | |
if(ip instanceof java.net.Inet6Address) { | |
continue; | |
} | |
if(ip.isLoopbackAddress() === true) { | |
continue; | |
} | |
list.push(ip.getHostAddress()); | |
}; | |
}; | |
var modules = []; | |
modules.push(new java.net.URL("http://central.maven.org/maven2/javax/mail/mail/1.4.7/mail-1.4.7.jar")); | |
modules.push(new java.net.URL("http://central.maven.org/maven2/javax/activation/activation/1.0.2/activation-1.0.2.jar")); | |
var classloader = new java.net.URLClassLoader(Java.to(modules, "java.net.URL[]")); | |
var jsmodules = ["http://builds.handlebarsjs.com.s3.amazonaws.com/handlebars-v4.0.4.js"]; | |
jsmodules.forEach(function(path) { | |
load(path); | |
}); | |
var engine = (function() { | |
var NashornScriptEngineFactory = Java.type("jdk.nashorn.api.scripting.NashornScriptEngineFactory"); | |
var factory = new NashornScriptEngineFactory(); | |
var e = factory.getScriptEngine(Java.to(["-scripting"], "java.lang.String[]"), classloader); | |
return e; | |
})(); | |
var tpl = Handlebars.compile(template); | |
var text = tpl(list); | |
var MAIL_SCRIPT = <<EOF | |
var property = new java.util.Properties(); | |
property.put("mail.smtp.host","smtp.gmail.com"); | |
property.put("mail.smtp.auth", "true"); | |
property.put("mail.smtp.starttls.enable", "true"); | |
property.put("mail.smtp.host", "smtp.gmail.com"); | |
property.put("mail.smtp.port", "587"); | |
property.put("mail.smtp.debug", "true"); | |
var Authenticator = Java.extend(javax.mail.Authenticator, { | |
getPasswordAuthentication : function() { | |
return new javax.mail.PasswordAuthentication($smtpId, $smtpPass); | |
} | |
}); | |
var session = javax.mail.Session.getInstance(property, new Authenticator()); | |
var mimeMessage = new javax.mail.internet.MimeMessage(session); | |
var toAddress = new javax.mail.internet.InternetAddress($toAddr, $toName); | |
var fromAddress = new javax.mail.internet.InternetAddress($fromAddr,$fromName); | |
mimeMessage.setRecipient(javax.mail.Message.RecipientType.TO, toAddress); | |
mimeMessage.setFrom(fromAddress); | |
mimeMessage.setSubject($title, "ISO-2022-JP"); | |
mimeMessage.setText($message, "ISO-2022-JP"); | |
javax.mail.Transport.send(mimeMessage); | |
EOF | |
engine.put("$message", text); | |
engine.put("$title", conf.title); | |
engine.put("$toAddr", conf.to.address); | |
engine.put("$toName", conf.to.name); | |
engine.put("$fromAddr", conf.from.address); | |
engine.put("$fromName", conf.from.name); | |
engine.put("$smtpId", conf.auth.user); | |
engine.put("$smtpPass", conf.auth.password); | |
return function() { | |
engine.eval(MAIL_SCRIPT); | |
}; | |
})($conf, $template); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment