Send notification emails when a developer registers for api keys

This commit is contained in:
Everett Sochowski 2014-04-28 15:55:11 +02:00
parent 7e5cff4c4e
commit c37aa35e57
2 changed files with 48 additions and 1 deletions

View File

@ -55,6 +55,7 @@ import java.io.FileInputStream
import java.io.File
import code.model.dataAccess.BankAccountCreationListener
import code.snippet.OAuthWorkedThanks
import javax.mail.internet.MimeMessage
/**
* A class that's instantiated early and run. It allows the application
* to modify lift's environment
@ -244,5 +245,10 @@ class Boot extends Loggable{
val useMessageQueue = Props.getBool("messageQueue.createBankAccounts", false)
if(useMessageQueue)
BankAccountCreationListener.startListen
Mailer.devModeSend.default.set( (m : MimeMessage) => {
logger.info("Would have sent email if not in dev mode: " + m.getContent)
})
}
}

View File

@ -39,8 +39,10 @@ import net.liftweb.http.SHtml
import net.liftweb.http.RequestVar
import net.liftweb.util.FieldError
import net.liftweb.util.Helpers
import net.liftweb.util.Props
import net.liftweb.common.Loggable
class ConsumerRegistration {
class ConsumerRegistration extends Loggable {
//TODO: for security reasons this snippet and the template must be re-factored
//to use the lift build in form function(SHtml._) so we can hide to what
@ -101,6 +103,9 @@ class ConsumerRegistration {
key(Helpers.randomString(40).toLowerCase).
secret(Helpers.randomString(40).toLowerCase).
save
notifyRegistrationOccurred(consumer)
showResults(consumer)
}
@ -151,5 +156,41 @@ class ConsumerRegistration {
else registerWithoutWarnings
}
def notifyRegistrationOccurred(registered : Consumer) = {
import net.liftweb.util.Mailer
import net.liftweb.util.Mailer._
val mailSent = for {
// e.g mail.api.consumer.registered.sender.address=no-reply@example.com
from <- Props.get("mail.api.consumer.registered.sender.address") ?~ "Could not send mail: Missing props param for 'from'"
// no spaces, comma separated e.g. mail.api.consumer.registered.notification.addresses=notify@example.com,notify2@example.com,notify3@example.com
toAddressesString <- Props.get("mail.api.consumer.registered.notification.addresses") ?~ "Could not send mail: Missing props param for 'to'"
} yield {
val thisApiInstance = Props.get("hostname", "unknown host")
val registrationMessage = s"New user signed up for API keys on $thisApiInstance. \n" +
s"Email: ${registered.developerEmail.get} \n" +
s"App name: ${registered.name.get} \n" +
s"App type: ${registered.appType.get.toString} \n" +
s"App description: ${registered.description.get}"
//technically doesn't work for all valid email addresses so this will mess up if someone tries to send emails to "foo,bar"@example.com
val to = toAddressesString.split(",").toList
val toParams = to.map(To(_))
val params = PlainMailBodyType(registrationMessage) :: toParams
//this is an async call
Mailer.sendMail(
From(from),
Subject(s"New API user registered on $thisApiInstance"),
params :_*)
}
//if Mailer.sendMail wasn't called (note: this actually isn't checking if the mail failed to send as that is being done asynchronously)
if(!mailSent.isDefined) this.logger.warn(s"API consumer registration failed: $mailSent")
}
}