yusuketk 5 лет назад
Родитель
Сommit
a5b480803f
2 измененных файлов с 40 добавлено и 13 удалено
  1. 29 6
      src/server/routes/apiv3/app-settings.js
  2. 11 7
      src/server/service/mail.js

+ 29 - 6
src/server/routes/apiv3/app-settings.js

@@ -314,7 +314,7 @@ module.exports = (crowi) => {
   /**
    * validate mail setting send test mail
    */
-  async function sendTestEmail(destinationAddress) {
+  async function sendTestEmail(mailConfig) {
 
     const { configManager, mailService } = crowi;
 
@@ -351,12 +351,16 @@ module.exports = (crowi) => {
 
     const mailOptions = {
       from: fromAddress,
-      to: destinationAddress,
-      subject: 'Wiki管理設定のアップデートによるメール通知',
-      text: 'このメールは、WikiのSMTP設定のアップデートにより送信されています。',
+      ...mailConfig,
     };
 
-    await sendMailPromiseWrapper(smtpClient, mailOptions);
+    try {
+      await sendMailPromiseWrapper(smtpClient, mailOptions);
+    }
+    catch (err) {
+      mailService.isMailerActive = false;
+      return err;
+    }
   }
 
   const updateMailSettinConfig = async function(requestMailSettingParams) {
@@ -410,6 +414,14 @@ module.exports = (crowi) => {
 
     try {
       const mailSettingParams = await updateMailSettinConfig({ 'mail:from': req.body.fromAddress });
+
+      const mailConfig = {
+        to: req.user.email,
+        subject: 'Wiki管理設定のアップデートによるメール通知',
+        text: 'このメールは、WikiのSMTP設定のアップデートにより送信されています。',
+      };
+      await sendTestEmail(mailConfig);
+
       return res.apiv3({ mailSettingParams });
     }
     catch (err) {
@@ -453,6 +465,12 @@ module.exports = (crowi) => {
 
     try {
       const mailSettingParams = await updateMailSettinConfig(requestMailSettingParams);
+      const mailConfig = {
+        to: req.user.email,
+        subject: 'Wiki管理設定のアップデートによるメール通知',
+        text: 'このメールは、WikiのSMTP設定のアップデートにより送信されています。',
+      };
+      await sendTestEmail(mailConfig);
       return res.apiv3({ mailSettingParams });
     }
     catch (err) {
@@ -477,7 +495,12 @@ module.exports = (crowi) => {
    */
   router.get('/smtp-test', loginRequiredStrictly, adminRequired, async(req, res) => {
     try {
-      await sendTestEmail(req.user.email);
+      const mailConfig = {
+        to: req.user.email,
+        subject: 'Wiki管理設定のアップデートによるメール通知',
+        text: 'このメールは、WikiのSMTP設定のアップデートにより送信されています。',
+      };
+      await sendTestEmail(mailConfig);
       return res.apiv3({});
     }
     catch (err) {

+ 11 - 7
src/server/service/mail.js

@@ -21,9 +21,9 @@ class MailService extends S2sMessageHandlable {
     this.mailer = {};
 
     /**
-     * the flag whether mailer is set up successfully
+     * the flag whether mailer is valid
      */
-    this.isMailerSetup = false;
+    this.isMailerActive = false;
 
     this.initialize();
   }
@@ -70,7 +70,7 @@ class MailService extends S2sMessageHandlable {
   initialize() {
     const { appService, configManager } = this;
 
-    this.isMailerSetup = false;
+    this.isMailerActive = false;
 
     if (!configManager.getConfig('crowi', 'mail:from')) {
       this.mailer = null;
@@ -79,13 +79,12 @@ class MailService extends S2sMessageHandlable {
 
     const transmissionMethod = configManager.getConfig('crowi', 'mail:transmissionMethod');
 
-    if (transmissionMethod === 'smtp') {
+    if (true) {
+    // if (transmissionMethod === 'smtp') {
       this.mailer = this.createSMTPClient();
-      this.isMailerSetup = true;
     }
     else if (transmissionMethod === 'ses') {
       this.mailer = this.createSESClient();
-      this.isMailerSetup = true;
     }
     else {
       this.mailer = null;
@@ -170,7 +169,12 @@ class MailService extends S2sMessageHandlable {
     );
 
     config.text = output;
-    return this.mailer.sendMail(this.setupMailConfig(config));
+    try {
+      return this.mailer.sendMail(this.setupMailConfig(config));
+    }
+    catch (err) {
+      this.isMailerActive = false;
+    }
   }
 
 }