~ghost08/wt

0ee884545a773c64ad1ec4882a8745a4fd9567a8 — Vladimír Magyar 2 years ago 092495a
Use emersion's go-sasl and go-smtp + update go.mod
4 files changed, 31 insertions(+), 12 deletions(-)

M go.mod
M go.sum
M main.go
M send_report.go
M go.mod => go.mod +9 -2
@@ 4,13 4,20 @@ go 1.16

require (
	github.com/360EntSecGroup-Skylar/excelize v1.4.1
	github.com/alecthomas/kong v0.2.16
	github.com/alecthomas/kong v0.2.17
	github.com/emersion/go-message v0.14.1
	github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21
	github.com/emersion/go-smtp v0.15.0
	github.com/emersion/go-textwrapper v0.0.0-20200911093747-65d896831594 // indirect
	github.com/gookit/color v1.4.2
	github.com/hashicorp/errwrap v1.1.0 // indirect
	github.com/hashicorp/go-multierror v1.1.1 // indirect
	github.com/martinlindhe/base36 v1.1.0 // indirect
	github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
	github.com/pkg/errors v0.9.1 // indirect
	github.com/posener/complete/v2 v2.0.1-alpha.13
	github.com/posener/script v1.1.5 // indirect
	github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
	github.com/stretchr/testify v1.6.1 // indirect
	github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
	golang.org/x/sys v0.0.0-20210603125802-9665404d3644 // indirect
)

M go.sum => go.sum +10 -0
@@ 2,11 2,17 @@ github.com/360EntSecGroup-Skylar/excelize v1.4.1 h1:l55mJb6rkkaUzOpSsgEeKYtS6/0g
github.com/360EntSecGroup-Skylar/excelize v1.4.1/go.mod h1:vnax29X2usfl7HHkBrX5EvSCJcmH3dT9luvxzu8iGAE=
github.com/alecthomas/kong v0.2.16 h1:F232CiYSn54Tnl1sJGTeHmx4vJDNLVP2b9yCVMOQwHQ=
github.com/alecthomas/kong v0.2.16/go.mod h1:kQOmtJgV+Lb4aj+I2LEn40cbtawdWJ9Y8QLq+lElKxE=
github.com/alecthomas/kong v0.2.17 h1:URDISCI96MIgcIlQyoCAlhOmrSw6pZScBNkctg8r0W0=
github.com/alecthomas/kong v0.2.17/go.mod h1:ka3VZ8GZNPXv9Ov+j4YNLkI8mTuhXyr/0ktSlqIydQQ=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/emersion/go-message v0.14.1 h1:j3rj9F+7VtXE9c8P5UHBq8FTHLW/AjnmvSRre6AHoYI=
github.com/emersion/go-message v0.14.1/go.mod h1:N1JWdZQ2WRUalmdHAX308CWBq747VJ8oUorFI3VCBwU=
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 h1:OJyUGMJTzHTd1XQp98QTaHernxMYzRaOasRir9hUlFQ=
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
github.com/emersion/go-smtp v0.15.0 h1:3+hMGMGrqP/lqd7qoxZc1hTU8LY8gHV9RFGWlqSDmP8=
github.com/emersion/go-smtp v0.15.0/go.mod h1:qm27SGYgoIPRot6ubfQ/GpiPy/g3PaZAVRxiO/sDUgQ=
github.com/emersion/go-textwrapper v0.0.0-20200911093747-65d896831594 h1:IbFBtwoTQyw0fIM5xv1HF+Y+3ZijDR839WMulgxCcUY=
github.com/emersion/go-textwrapper v0.0.0-20200911093747-65d896831594/go.mod h1:aqO8z8wPrjkscevZJFVE1wXJrLpC5LtJG7fqLOsPb2U=
github.com/gookit/color v1.4.2 h1:tXy44JFSFkKnELV6WaMo/lLfu/meqITX3iAV52do7lk=


@@ 40,13 46,17 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44 h1:Bli41pIlzTzf3KEY06n+xnzK/BESIg2ze4Pgfh/aI8c=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210603125802-9665404d3644 h1:CA1DEQ4NdKphKeL70tvsWNdT5oFh1lOjihRcEDROi0I=
golang.org/x/sys v0.0.0-20210603125802-9665404d3644/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.5-0.20201125200606-c27b9fd57aec/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

M main.go => main.go +1 -1
@@ 31,7 31,7 @@ var CLI struct {
		Output      string   `optional short:"o" default:"report.xlsx" help:"attachment file name"`
		SmtpServer  string   `help:"specifies the outgoing SMTP server to use" env:"SMTP_SERVER"`
		SmtpPort    int      `default:"25" help:"specifies a port different from the default port (SMTP servers typically listen to smtp port 25, but may also listen to submission port 587, or the common SSL smtp port 465)" env:"SMTP_PORT"`
		SmtpAuth    string   `enum:",PLAIN,CRAM-MD5" help:"SMTP authentication mechanism" env:"SMTP_AUTH"`
		SmtpAuth    string   `enum:",PLAIN,LOGIN" help:"SMTP authentication mechanism" env:"SMTP_AUTH"`
		SmtpUser    string   `optional help:"username for SMTP-AUTH. if a username is not specified, then authentication is not attempted." env:"SMTP_USER"`
		SmtpPass    string   `optional help:"password for SMTP-AUTH. If no argument is specified, then the empty string is used as the password" env:"SMTP_PASS"`
		SmtpPassCmd string   `optional help:"command that prints the password for SMTP-AUTH" env:"SMTP_PASS_CMD"`

M send_report.go => send_report.go +11 -9
@@ 4,12 4,14 @@ import (
	"bytes"
	"fmt"
	"io"
	"net/smtp"
	"os"
	"os/exec"
	"strings"
	"time"

	"github.com/emersion/go-message/mail"
	"github.com/emersion/go-sasl"
	"github.com/emersion/go-smtp"
)

func sendreport() error {


@@ 78,7 80,7 @@ func sendreport() error {
	w.Close()
	mw.Close()

	var auth smtp.Auth
	var auth sasl.Client
	if CLI.SendReport.SmtpUser != "" {
		pass, err := getSmtpPass()
		if err != nil {


@@ 86,14 88,14 @@ func sendreport() error {
		}
		switch CLI.SendReport.SmtpAuth {
		case "PLAIN":
			auth = smtp.PlainAuth(
			auth = sasl.NewPlainClient(
				"",
				CLI.SendReport.SmtpUser,
				pass,
				CLI.SendReport.SmtpServer,
				//CLI.SendReport.SmtpServer,
			)
		case "CRAM-MD5":
			auth = smtp.CRAMMD5Auth(CLI.SendReport.SmtpUser, pass)
		case "LOGIN":
			auth = sasl.NewLoginClient(CLI.SendReport.SmtpUser, pass)
		}
	}



@@ 102,7 104,7 @@ func sendreport() error {
		auth,
		CLI.SendReport.From,
		CLI.SendReport.To,
		msg.Bytes(),
		&msg,
	); err != nil {
		return fmt.Errorf("sending email: %w", err)
	}


@@ 113,9 115,9 @@ func getSmtpPass() (string, error) {
	if CLI.SendReport.SmtpPassCmd == "" {
		return CLI.SendReport.SmtpPass, nil
	}
	output, err := exec.Command("sh", "-c", CLI.SendReport.SmtpPassCmd).CombinedOutput()
	output, err := exec.Command("sh", "-c", CLI.SendReport.SmtpPassCmd).Output()
	if err != nil {
		return "", err
	}
	return string(output), nil
	return strings.TrimSpace(string(output)), nil
}