smtptest.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. smtpConfig.Debug = 1
  49. err = smtpConfig.Initialize(configDir, false)
  50. if err != nil {
  51. logger.ErrorToConsole("unable to initialize SMTP configuration: %v", err)
  52. os.Exit(1)
  53. }
  54. err = smtp.SendEmail([]string{smtpTestRecipient}, nil, "SFTPGo - Testing Email Settings", "It appears your SFTPGo email is setup correctly!",
  55. smtp.EmailContentTypeTextPlain)
  56. if err != nil {
  57. logger.WarnToConsole("Error sending email: %v", err)
  58. os.Exit(1)
  59. }
  60. logger.InfoToConsole("No errors were reported while sending the test email. Please check your inbox to make sure.")
  61. },
  62. }
  63. )
  64. func init() {
  65. addConfigFlags(smtpTestCmd)
  66. smtpTestCmd.Flags().StringVar(&smtpTestRecipient, "recipient", "", `email address to send the test e-mail to`)
  67. smtpTestCmd.MarkFlagRequired("recipient") //nolint:errcheck
  68. rootCmd.AddCommand(smtpTestCmd)
  69. }