Fearure:
- 增加收件箱功能(有BUG) - 增加已发送|草稿箱|发送历史 本地记录
This commit is contained in:
87
main.go
87
main.go
@@ -74,6 +74,12 @@ var rootCmd = &cobra.Command{
|
||||
Short: "Send emails from your terminal",
|
||||
Long: `Pop is a tool for sending emails from your terminal.`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if !configExists() && smtpUsername == "" && smtpPassword == "" && resendAPIKey == "" {
|
||||
if err := runOnboarding(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
var deliveryMethod DeliveryMethod
|
||||
switch {
|
||||
case resendAPIKey != "" && smtpUsername != "" && smtpPassword != "":
|
||||
@@ -89,11 +95,11 @@ var rootCmd = &cobra.Command{
|
||||
|
||||
switch deliveryMethod {
|
||||
case None:
|
||||
fmt.Printf("\n %s %s %s\n\n", errorHeaderStyle.String(), inlineCodeStyle.Render(ResendAPIKey), "environment variable is required.")
|
||||
fmt.Printf(" %s %s\n\n", commentStyle.Render("You can grab one at"), linkStyle.Render("https://resend.com/api-keys"))
|
||||
fmt.Printf("\n %s 未找到邮件配置\n\n", errorHeaderStyle.String())
|
||||
fmt.Printf(" %s 请运行 %s 进行首次配置\n\n", commentStyle.Render("提示:"), inlineCodeStyle.Render("pop --config"))
|
||||
cmd.SilenceUsage = true
|
||||
cmd.SilenceErrors = true
|
||||
return errors.New("missing required environment variable")
|
||||
return errors.New("missing mail configuration")
|
||||
case Unknown:
|
||||
fmt.Printf("\n %s Unknown delivery method.\n", errorHeaderStyle.String())
|
||||
fmt.Printf("\n You have set both %s and %s delivery methods.", inlineCodeStyle.Render(ResendAPIKey), inlineCodeStyle.Render("POP_SMPT_*"))
|
||||
@@ -196,39 +202,112 @@ var ManCmd = &cobra.Command{
|
||||
func init() {
|
||||
rootCmd.AddCommand(ManCmd)
|
||||
|
||||
cfg, _ := loadConfig()
|
||||
|
||||
rootCmd.Flags().StringSliceVar(&bcc, "bcc", []string{}, "BCC recipients")
|
||||
rootCmd.Flags().StringSliceVar(&cc, "cc", []string{}, "CC recipients")
|
||||
rootCmd.Flags().StringSliceVarP(&attachments, "attach", "a", []string{}, "Email's attachments")
|
||||
rootCmd.Flags().StringSliceVarP(&to, "to", "t", []string{}, "Recipients")
|
||||
rootCmd.Flags().StringVarP(&body, "body", "b", "", "Email's contents")
|
||||
envFrom := os.Getenv(PopFrom)
|
||||
if envFrom == "" {
|
||||
envFrom = cfg.From
|
||||
}
|
||||
from = envFrom
|
||||
rootCmd.Flags().StringVarP(&from, "from", "f", envFrom, "Email's sender"+commentStyle.Render("($"+PopFrom+")"))
|
||||
rootCmd.Flags().StringVarP(&subject, "subject", "s", "", "Email's subject")
|
||||
rootCmd.Flags().BoolVar(&preview, "preview", false, "Whether to preview the email before sending")
|
||||
envUnsafe := os.Getenv(PopUnsafeHTML) == "true"
|
||||
if !envUnsafe {
|
||||
envUnsafe = cfg.UnsafeHTML
|
||||
}
|
||||
rootCmd.Flags().BoolVarP(&unsafe, "unsafe", "u", envUnsafe, "Whether to allow unsafe HTML in the email body, also enable some extra markdown features (Experimental)")
|
||||
envSignature := os.Getenv(PopSignature)
|
||||
if envSignature == "" {
|
||||
envSignature = cfg.Signature
|
||||
}
|
||||
rootCmd.Flags().StringVarP(&signature, "signature", "x", envSignature, "Signature to display at the end of the email."+commentStyle.Render("($"+PopSignature+")"))
|
||||
envSMTPHost := os.Getenv(PopSMTPHost)
|
||||
if envSMTPHost == "" {
|
||||
envSMTPHost = cfg.SMTP.Host
|
||||
}
|
||||
smtpHost = envSMTPHost
|
||||
rootCmd.Flags().StringVarP(&smtpHost, "smtp.host", "H", envSMTPHost, "Host of the SMTP server"+commentStyle.Render("($"+PopSMTPHost+")"))
|
||||
envSMTPPort, _ := strconv.Atoi(os.Getenv(PopSMTPPort))
|
||||
if envSMTPPort == 0 {
|
||||
envSMTPPort = 587
|
||||
envSMTPPort = cfg.SMTP.Port
|
||||
if envSMTPPort == 0 {
|
||||
envSMTPPort = 587
|
||||
}
|
||||
}
|
||||
smtpPort = envSMTPPort
|
||||
rootCmd.Flags().IntVarP(&smtpPort, "smtp.port", "P", envSMTPPort, "Port of the SMTP server"+commentStyle.Render("($"+PopSMTPPort+")"))
|
||||
envSMTPUsername := os.Getenv(PopSMTPUsername)
|
||||
if envSMTPUsername == "" {
|
||||
envSMTPUsername = cfg.SMTP.Username
|
||||
}
|
||||
smtpUsername = envSMTPUsername
|
||||
rootCmd.Flags().StringVarP(&smtpUsername, "smtp.username", "U", envSMTPUsername, "Username of the SMTP server"+commentStyle.Render("($"+PopSMTPUsername+")"))
|
||||
envSMTPPassword := os.Getenv(PopSMTPPassword)
|
||||
if envSMTPPassword == "" {
|
||||
envSMTPPassword = cfg.SMTP.Password
|
||||
}
|
||||
smtpPassword = envSMTPPassword
|
||||
rootCmd.Flags().StringVarP(&smtpPassword, "smtp.password", "p", envSMTPPassword, "Password of the SMTP server"+commentStyle.Render("($"+PopSMTPPassword+")"))
|
||||
envSMTPEncryption := os.Getenv(PopSMTPEncryption)
|
||||
if envSMTPEncryption == "" {
|
||||
envSMTPEncryption = cfg.SMTP.Encryption
|
||||
if envSMTPEncryption == "" {
|
||||
envSMTPEncryption = "starttls"
|
||||
}
|
||||
}
|
||||
smtpEncryption = envSMTPEncryption
|
||||
rootCmd.Flags().StringVarP(&smtpEncryption, "smtp.encryption", "e", envSMTPEncryption, "Encryption type of the SMTP server (starttls, ssl, or none)"+commentStyle.Render("($"+PopSMTPEncryption+")"))
|
||||
envInsecureSkipVerify := os.Getenv(PopSMTPInsecureSkipVerify) == "true"
|
||||
if !envInsecureSkipVerify {
|
||||
envInsecureSkipVerify = cfg.SMTP.InsecureSkipVerify
|
||||
}
|
||||
smtpInsecureSkipVerify = envInsecureSkipVerify
|
||||
rootCmd.Flags().BoolVarP(&smtpInsecureSkipVerify, "smtp.insecure", "i", envInsecureSkipVerify, "Skip TLS verification with SMTP server"+commentStyle.Render("($"+PopSMTPInsecureSkipVerify+")"))
|
||||
envResendAPIKey := os.Getenv(ResendAPIKey)
|
||||
rootCmd.Flags().StringVarP(&resendAPIKey, "resend.key", "r", envResendAPIKey, "API key for the Resend.com"+commentStyle.Render("($"+ResendAPIKey+")"))
|
||||
|
||||
rootCmd.CompletionOptions.HiddenDefaultCmd = true
|
||||
|
||||
var configCmd = &cobra.Command{
|
||||
Use: "config",
|
||||
Short: "配置或重新配置 Pop",
|
||||
Long: `打开交互式配置向导来设置或更新 Pop 的配置。`,
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return runOnboarding()
|
||||
},
|
||||
}
|
||||
rootCmd.AddCommand(configCmd)
|
||||
|
||||
var inboxCmd = &cobra.Command{
|
||||
Use: "inbox",
|
||||
Short: "打开收件箱",
|
||||
Long: `查看配置邮箱的收件箱,显示未读邮件列表。`,
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return runInbox(7)
|
||||
},
|
||||
}
|
||||
inboxCmd.Flags().IntP("days", "d", 7, "显示最近几天的未读邮件")
|
||||
rootCmd.AddCommand(inboxCmd)
|
||||
|
||||
var historyCmd = &cobra.Command{
|
||||
Use: "history",
|
||||
Short: "查看发送历史",
|
||||
Long: `查看已发送或草稿邮件的历史记录。`,
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return runHistory()
|
||||
},
|
||||
}
|
||||
rootCmd.AddCommand(historyCmd)
|
||||
|
||||
if len(CommitSHA) >= 7 { //nolint:gomnd
|
||||
vt := rootCmd.VersionTemplate()
|
||||
rootCmd.SetVersionTemplate(vt[:len(vt)-1] + " (" + CommitSHA[0:7] + ")\n")
|
||||
|
||||
Reference in New Issue
Block a user