mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-10-26 08:06:15 +01:00
Merge pull request #1711 from gitbucket/improve-mail-api
Make Mailer API more general
This commit is contained in:
@@ -174,11 +174,12 @@ trait SystemSettingsControllerBase extends AccountManagementControllerBase {
|
||||
|
||||
post("/admin/system/sendmail", sendMailForm)(adminOnly { form =>
|
||||
try {
|
||||
new Mailer(form.smtp).send(form.testAddress,
|
||||
"Test message from GitBucket",
|
||||
"This is a test message from GitBucket.",
|
||||
None,
|
||||
context.loginAccount
|
||||
new Mailer(context.settings.copy(smtp = Some(form.smtp), notification = true)).send(
|
||||
to = form.testAddress,
|
||||
subject = "Test message from GitBucket",
|
||||
textMsg = "This is a test message from GitBucket.",
|
||||
htmlMsg = None,
|
||||
loginAccount = context.loginAccount
|
||||
)
|
||||
|
||||
"Test mail has been sent to: " + form.testAddress
|
||||
|
||||
@@ -3,18 +3,20 @@ package gitbucket.core.plugin
|
||||
import gitbucket.core.controller.Context
|
||||
import gitbucket.core.model.Issue
|
||||
import gitbucket.core.service.RepositoryService.RepositoryInfo
|
||||
import gitbucket.core.model.Profile._
|
||||
import profile.api._
|
||||
|
||||
trait IssueHook {
|
||||
|
||||
def created(issue: Issue, repository: RepositoryInfo)(implicit context: Context): Unit = ()
|
||||
def addedComment(commentId: Int, content: String, issue: Issue, repository: RepositoryInfo)(implicit context: Context): Unit = ()
|
||||
def closed(issue: Issue, repository: RepositoryInfo)(implicit context: Context): Unit = ()
|
||||
def reopened(issue: Issue, repository: RepositoryInfo)(implicit context: Context): Unit = ()
|
||||
def created(issue: Issue, repository: RepositoryInfo)(implicit session: Session, context: Context): Unit = ()
|
||||
def addedComment(commentId: Int, content: String, issue: Issue, repository: RepositoryInfo)(implicit session: Session, context: Context): Unit = ()
|
||||
def closed(issue: Issue, repository: RepositoryInfo)(implicit session: Session, context: Context): Unit = ()
|
||||
def reopened(issue: Issue, repository: RepositoryInfo)(implicit session: Session, context: Context): Unit = ()
|
||||
|
||||
}
|
||||
|
||||
trait PullRequestHook extends IssueHook {
|
||||
|
||||
def merged(issue: Issue, repository: RepositoryInfo)(implicit context: Context): Unit = ()
|
||||
def merged(issue: Issue, repository: RepositoryInfo)(implicit session: Session, context: Context): Unit = ()
|
||||
|
||||
}
|
||||
|
||||
68
src/main/scala/gitbucket/core/util/Mailer.scala
Normal file
68
src/main/scala/gitbucket/core/util/Mailer.scala
Normal file
@@ -0,0 +1,68 @@
|
||||
package gitbucket.core.util
|
||||
|
||||
import gitbucket.core.model.Account
|
||||
import gitbucket.core.service.SystemSettingsService
|
||||
|
||||
import org.apache.commons.mail.{DefaultAuthenticator, HtmlEmail}
|
||||
import SystemSettingsService.SystemSettings
|
||||
|
||||
class Mailer(settings: SystemSettings){
|
||||
|
||||
def send(to: String, subject: String, textMsg: String, htmlMsg: Option[String] = None, loginAccount: Option[Account] = None): Unit = {
|
||||
createMail(subject, textMsg, htmlMsg, loginAccount).foreach { email =>
|
||||
email.addTo(to).send
|
||||
}
|
||||
}
|
||||
|
||||
def sendBcc(bcc: Seq[String], subject: String, textMsg: String, htmlMsg: Option[String] = None, loginAccount: Option[Account] = None): Unit = {
|
||||
createMail(subject, textMsg, htmlMsg, loginAccount).foreach { email =>
|
||||
bcc.foreach { address =>
|
||||
email.addBcc(address)
|
||||
}
|
||||
email.send()
|
||||
}
|
||||
}
|
||||
|
||||
def createMail(subject: String, textMsg: String, htmlMsg: Option[String] = None, loginAccount: Option[Account] = None): Option[HtmlEmail] = {
|
||||
if(settings.notification == true){
|
||||
settings.smtp.map { smtp =>
|
||||
val email = new HtmlEmail
|
||||
email.setHostName(smtp.host)
|
||||
email.setSmtpPort(smtp.port.get)
|
||||
smtp.user.foreach { user =>
|
||||
email.setAuthenticator(new DefaultAuthenticator(user, smtp.password.getOrElse("")))
|
||||
}
|
||||
smtp.ssl.foreach { ssl =>
|
||||
email.setSSLOnConnect(ssl)
|
||||
if(ssl == true) {
|
||||
email.setSslSmtpPort(smtp.port.get.toString)
|
||||
}
|
||||
}
|
||||
smtp.starttls.foreach { starttls =>
|
||||
email.setStartTLSEnabled(starttls)
|
||||
email.setStartTLSRequired(starttls)
|
||||
}
|
||||
smtp.fromAddress
|
||||
.map (_ -> smtp.fromName.getOrElse(loginAccount.map(_.userName).getOrElse("GitBucket")))
|
||||
.orElse (Some("notifications@gitbucket.com" -> loginAccount.map(_.userName).getOrElse("GitBucket")))
|
||||
.foreach { case (address, name) =>
|
||||
email.setFrom(address, name)
|
||||
}
|
||||
email.setCharset("UTF-8")
|
||||
email.setSubject(subject)
|
||||
email.setTextMsg(textMsg)
|
||||
htmlMsg.foreach { msg =>
|
||||
email.setHtmlMsg(msg)
|
||||
}
|
||||
|
||||
email
|
||||
}
|
||||
} else None
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//class MockMailer extends Notifier {
|
||||
// def toNotify(subject: String, textMsg: String, htmlMsg: Option[String] = None)
|
||||
// (recipients: Account => Session => Seq[String])(implicit context: Context): Unit = ()
|
||||
//}
|
||||
@@ -1,99 +0,0 @@
|
||||
package gitbucket.core.util
|
||||
|
||||
import gitbucket.core.model.{Session, Account}
|
||||
import gitbucket.core.model.Profile.profile.blockingApi._
|
||||
import gitbucket.core.service.SystemSettingsService
|
||||
import gitbucket.core.servlet.Database
|
||||
|
||||
import scala.concurrent._
|
||||
import scala.util.{Success, Failure}
|
||||
import ExecutionContext.Implicits.global
|
||||
import org.apache.commons.mail.{DefaultAuthenticator, HtmlEmail}
|
||||
import org.slf4j.LoggerFactory
|
||||
import gitbucket.core.controller.Context
|
||||
import SystemSettingsService.Smtp
|
||||
|
||||
/**
|
||||
* The trait for notifications.
|
||||
* This is used by notifications plugin, which provides notifications feature on GitBucket.
|
||||
* Please see the plugin for details.
|
||||
*/
|
||||
trait Notifier {
|
||||
def toNotify(subject: String, textMsg: String)
|
||||
(recipients: Account => Session => Seq[String])(implicit context: Context): Unit = {
|
||||
toNotify(subject, textMsg, None)(recipients)
|
||||
}
|
||||
|
||||
def toNotify(subject: String, textMsg: String, htmlMsg: Option[String])
|
||||
(recipients: Account => Session => Seq[String])(implicit context: Context): Unit
|
||||
|
||||
}
|
||||
|
||||
object Notifier {
|
||||
def apply(): Notifier = new SystemSettingsService {}.loadSystemSettings match {
|
||||
case settings if (settings.notification && settings.useSMTP) => new Mailer(settings.smtp.get)
|
||||
case _ => new MockMailer
|
||||
}
|
||||
}
|
||||
|
||||
class Mailer(private val smtp: Smtp) extends Notifier {
|
||||
private val logger = LoggerFactory.getLogger(classOf[Mailer])
|
||||
|
||||
def toNotify(subject: String, textMsg: String, htmlMsg: Option[String] = None)
|
||||
(recipients: Account => Session => Seq[String])(implicit context: Context): Unit = {
|
||||
context.loginAccount.foreach { loginAccount =>
|
||||
val database = Database()
|
||||
|
||||
val f = Future {
|
||||
database withSession { session =>
|
||||
recipients(loginAccount)(session) foreach { to =>
|
||||
send(to, subject, textMsg, htmlMsg, Some(loginAccount))
|
||||
}
|
||||
}
|
||||
"Notifications Successful."
|
||||
}
|
||||
f.onComplete {
|
||||
case Success(s) => logger.debug(s)
|
||||
case Failure(t) => logger.error("Notifications Failed.", t)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def send(to: String, subject: String, textMsg: String, htmlMsg: Option[String] = None, loginAccount: Option[Account] = None): Unit = {
|
||||
val email = new HtmlEmail
|
||||
email.setHostName(smtp.host)
|
||||
email.setSmtpPort(smtp.port.get)
|
||||
smtp.user.foreach { user =>
|
||||
email.setAuthenticator(new DefaultAuthenticator(user, smtp.password.getOrElse("")))
|
||||
}
|
||||
smtp.ssl.foreach { ssl =>
|
||||
email.setSSLOnConnect(ssl)
|
||||
if(ssl == true) {
|
||||
email.setSslSmtpPort(smtp.port.get.toString)
|
||||
}
|
||||
}
|
||||
smtp.starttls.foreach { starttls =>
|
||||
email.setStartTLSEnabled(starttls)
|
||||
email.setStartTLSRequired(starttls)
|
||||
}
|
||||
smtp.fromAddress
|
||||
.map (_ -> smtp.fromName.getOrElse(loginAccount.map(_.userName).getOrElse("GitBucket")))
|
||||
.orElse (Some("notifications@gitbucket.com" -> loginAccount.map(_.userName).getOrElse("GitBucket")))
|
||||
.foreach { case (address, name) =>
|
||||
email.setFrom(address, name)
|
||||
}
|
||||
email.setCharset("UTF-8")
|
||||
email.setSubject(subject)
|
||||
email.setTextMsg(textMsg)
|
||||
htmlMsg.foreach { msg =>
|
||||
email.setHtmlMsg(msg)
|
||||
}
|
||||
|
||||
email.addTo(to).send
|
||||
}
|
||||
|
||||
}
|
||||
class MockMailer extends Notifier {
|
||||
def toNotify(subject: String, textMsg: String, htmlMsg: Option[String] = None)
|
||||
(recipients: Account => Session => Seq[String])(implicit context: Context): Unit = ()
|
||||
}
|
||||
Reference in New Issue
Block a user