2022-07-17 18:16:00 +00:00
|
|
|
// Copyright (C) 2019-2022 Nicola Murino
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2021-09-26 18:25:37 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/config"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/logger"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/smtp"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/util"
|
2021-09-26 18:25:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
smtpTestRecipient string
|
|
|
|
smtpTestCmd = &cobra.Command{
|
|
|
|
Use: "smtptest",
|
|
|
|
Short: "Test the SMTP configuration",
|
|
|
|
Long: `SFTPGo will try to send a test email to the specified recipient.
|
|
|
|
If the SMTP configuration is correct you should receive this email.`,
|
2022-07-19 21:28:33 +00:00
|
|
|
Run: func(_ *cobra.Command, _ []string) {
|
2021-09-26 18:25:37 +00:00
|
|
|
logger.DisableLogger()
|
|
|
|
logger.EnableConsoleLogger(zerolog.DebugLevel)
|
|
|
|
configDir = util.CleanDirInput(configDir)
|
|
|
|
err := config.LoadConfig(configDir, configFile)
|
|
|
|
if err != nil {
|
|
|
|
logger.WarnToConsole("Unable to initialize data provider, config load error: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
smtpConfig := config.GetSMTPConfig()
|
2021-10-02 20:25:41 +00:00
|
|
|
err = smtpConfig.Initialize(configDir)
|
2021-09-26 18:25:37 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.ErrorToConsole("unable to initialize SMTP configuration: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2022-07-11 06:17:36 +00:00
|
|
|
err = smtp.SendEmail([]string{smtpTestRecipient}, "SFTPGo - Testing Email Settings", "It appears your SFTPGo email is setup correctly!",
|
2021-09-26 18:25:37 +00:00
|
|
|
smtp.EmailContentTypeTextPlain)
|
|
|
|
if err != nil {
|
|
|
|
logger.WarnToConsole("Error sending email: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
logger.InfoToConsole("No errors were reported while sending an email. Please check your inbox to make sure.")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
addConfigFlags(smtpTestCmd)
|
|
|
|
smtpTestCmd.Flags().StringVar(&smtpTestRecipient, "recipient", "", `email address to send the test e-mail to`)
|
|
|
|
smtpTestCmd.MarkFlagRequired("recipient") //nolint:errcheck
|
|
|
|
|
|
|
|
rootCmd.AddCommand(smtpTestCmd)
|
|
|
|
}
|