code.gitea.io/gitea@v1.21.7/cmd/mailer.go (about)

     1  // Copyright 2020 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package cmd
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"code.gitea.io/gitea/modules/private"
    10  	"code.gitea.io/gitea/modules/setting"
    11  
    12  	"github.com/urfave/cli/v2"
    13  )
    14  
    15  func runSendMail(c *cli.Context) error {
    16  	ctx, cancel := installSignals()
    17  	defer cancel()
    18  
    19  	setting.MustInstalled()
    20  
    21  	if err := argsSet(c, "title"); err != nil {
    22  		return err
    23  	}
    24  
    25  	subject := c.String("title")
    26  	confirmSkiped := c.Bool("force")
    27  	body := c.String("content")
    28  
    29  	if !confirmSkiped {
    30  		if len(body) == 0 {
    31  			fmt.Print("warning: Content is empty")
    32  		}
    33  
    34  		fmt.Print("Proceed with sending email? [Y/n] ")
    35  		isConfirmed, err := confirm()
    36  		if err != nil {
    37  			return err
    38  		} else if !isConfirmed {
    39  			fmt.Println("The mail was not sent")
    40  			return nil
    41  		}
    42  	}
    43  
    44  	respText, extra := private.SendEmail(ctx, subject, body, nil)
    45  	if extra.HasError() {
    46  		return handleCliResponseExtra(extra)
    47  	}
    48  	_, _ = fmt.Printf("Sent %s email(s) to all users\n", respText)
    49  	return nil
    50  }