mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-03 12:05:59 +01:00
Improve Mailer API
This commit is contained in:
@@ -174,11 +174,12 @@ trait SystemSettingsControllerBase extends AccountManagementControllerBase {
|
|||||||
|
|
||||||
post("/admin/system/sendmail", sendMailForm)(adminOnly { form =>
|
post("/admin/system/sendmail", sendMailForm)(adminOnly { form =>
|
||||||
try {
|
try {
|
||||||
new Mailer(form.smtp).send(form.testAddress,
|
new Mailer(context.settings.copy(smtp = Some(form.smtp), notification = true)).send(
|
||||||
"Test message from GitBucket",
|
to = form.testAddress,
|
||||||
"This is a test message from GitBucket.",
|
subject = "Test message from GitBucket",
|
||||||
None,
|
textMsg = "This is a test message from GitBucket.",
|
||||||
context.loginAccount
|
htmlMsg = None,
|
||||||
|
loginAccount = context.loginAccount
|
||||||
)
|
)
|
||||||
|
|
||||||
"Test mail has been sent to: " + form.testAddress
|
"Test mail has been sent to: " + form.testAddress
|
||||||
|
|||||||
@@ -3,18 +3,20 @@ package gitbucket.core.plugin
|
|||||||
import gitbucket.core.controller.Context
|
import gitbucket.core.controller.Context
|
||||||
import gitbucket.core.model.Issue
|
import gitbucket.core.model.Issue
|
||||||
import gitbucket.core.service.RepositoryService.RepositoryInfo
|
import gitbucket.core.service.RepositoryService.RepositoryInfo
|
||||||
|
import gitbucket.core.model.Profile._
|
||||||
|
import profile.api._
|
||||||
|
|
||||||
trait IssueHook {
|
trait IssueHook {
|
||||||
|
|
||||||
def created(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 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 context: Context): Unit = ()
|
def closed(issue: Issue, repository: RepositoryInfo)(implicit session: Session, context: Context): Unit = ()
|
||||||
def reopened(issue: Issue, repository: RepositoryInfo)(implicit context: Context): Unit = ()
|
def reopened(issue: Issue, repository: RepositoryInfo)(implicit session: Session, context: Context): Unit = ()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
trait PullRequestHook extends IssueHook {
|
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