smtptest.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright (C) 2019-2023 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package cmd
  15. import (
  16. "os"
  17. "github.com/rs/zerolog"
  18. "github.com/spf13/cobra"
  19. "github.com/drakkan/sftpgo/v2/internal/config"
  20. "github.com/drakkan/sftpgo/v2/internal/dataprovider"
  21. "github.com/drakkan/sftpgo/v2/internal/logger"
  22. "github.com/drakkan/sftpgo/v2/internal/smtp"
  23. "github.com/drakkan/sftpgo/v2/internal/util"
  24. )
  25. var (
  26. smtpTestRecipient string
  27. smtpTestCmd = &cobra.Command{
  28. Use: "smtptest",
  29. Short: "Test the SMTP configuration",
  30. Long: `SFTPGo will try to send a test email to the specified recipient.
  31. If the SMTP configuration is correct you should receive this email.`,
  32. Run: func(_ *cobra.Command, _ []string) {
  33. logger.DisableLogger()
  34. logger.EnableConsoleLogger(zerolog.DebugLevel)
  35. configDir = util.CleanDirInput(configDir)
  36. err := config.LoadConfig(configDir, configFile)
  37. if err != nil {
  38. logger.ErrorToConsole("Unable to load configuration: %v", err)
  39. os.Exit(1)
  40. }
  41. providerConf := config.GetProviderConf()
  42. err = dataprovider.Initialize(providerConf, configDir, false)
  43. if err != nil {
  44. logger.ErrorToConsole("error initializing data provider: %v", err)
  45. os.Exit(1)
  46. }
  47. smtpConfig := config.GetSMTPConfig()
  48. err = smtpConfig.Initialize(configDir, false)
  49. if err != nil {
  50. logger.ErrorToConsole("unable to initialize SMTP configuration: %v", err)
  51. os.Exit(1)
  52. }
  53. err = smtp.SendEmail([]string{smtpTestRecipient}, "SFTPGo - Testing Email Settings", "It appears your SFTPGo email is setup correctly!",
  54. smtp.EmailContentTypeTextPlain)
  55. if err != nil {
  56. logger.WarnToConsole("Error sending email: %v", err)
  57. os.Exit(1)
  58. }
  59. logger.InfoToConsole("No errors were reported while sending the test email. Please check your inbox to make sure.")
  60. },
  61. }
  62. )
  63. func init() {
  64. addConfigFlags(smtpTestCmd)
  65. smtpTestCmd.Flags().StringVar(&smtpTestRecipient, "recipient", "", `email address to send the test e-mail to`)
  66. smtpTestCmd.MarkFlagRequired("recipient") //nolint:errcheck
  67. rootCmd.AddCommand(smtpTestCmd)
  68. }