Merge pull request #1672 from kounoike/pr-support-textmsg-in-email

Add support to MIME Text part in Email notification.
This commit is contained in:
Naoki Takezoe
2017-08-21 01:04:00 +09:00
committed by GitHub
2 changed files with 16 additions and 8 deletions

View File

@@ -175,8 +175,9 @@ 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.",
context.loginAccount.get)
"Test message from GitBucket", context.loginAccount.get,
"This is a test message from GitBucket.", None
)
"Test mail has been sent to: " + form.testAddress

View File

@@ -19,8 +19,12 @@ import SystemSettingsService.Smtp
* 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, msg: String)
def toNotify(subject: String, textMsg: String, htmlMsg: Option[String])
(recipients: Account => Session => Seq[String])(implicit context: Context): Unit
}
@@ -35,7 +39,7 @@ object Notifier {
class Mailer(private val smtp: Smtp) extends Notifier {
private val logger = LoggerFactory.getLogger(classOf[Mailer])
def toNotify(subject: String, msg: String)
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()
@@ -43,7 +47,7 @@ class Mailer(private val smtp: Smtp) extends Notifier {
val f = Future {
database withSession { session =>
recipients(loginAccount)(session) foreach { to =>
send(to, subject, msg, loginAccount)
send(to, subject, loginAccount, textMsg, htmlMsg)
}
}
"Notifications Successful."
@@ -55,7 +59,7 @@ class Mailer(private val smtp: Smtp) extends Notifier {
}
}
def send(to: String, subject: String, msg: String, loginAccount: Account): Unit = {
def send(to: String, subject: String, loginAccount: Account, textMsg: String, htmlMsg: Option[String] = None): Unit = {
val email = new HtmlEmail
email.setHostName(smtp.host)
email.setSmtpPort(smtp.port.get)
@@ -80,13 +84,16 @@ class Mailer(private val smtp: Smtp) extends Notifier {
}
email.setCharset("UTF-8")
email.setSubject(subject)
email.setHtmlMsg(msg)
email.setTextMsg(textMsg)
htmlMsg.foreach { msg =>
email.setHtmlMsg(msg)
}
email.addTo(to).send
}
}
class MockMailer extends Notifier {
def toNotify(subject: String, msg: String)
def toNotify(subject: String, textMsg: String, htmlMsg: Option[String] = None)
(recipients: Account => Session => Seq[String])(implicit context: Context): Unit = ()
}