Skip to content

Commit

Permalink
feat(tls): add InsecureSkipVerify
Browse files Browse the repository at this point in the history
Also fix typo in go.mod
  • Loading branch information
folago committed Apr 30, 2024
1 parent 9a05a86 commit d768a25
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module cycloid.io/cy-smpt
module cycloid.io/cy-smtp

go 1.22.2

Expand Down
28 changes: 23 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"crypto/tls"
"fmt"
"log"
"os"
Expand All @@ -14,6 +15,7 @@ import (

const (
cfg = "config-file"
skiptls = "email-tls-skip-verify"
server = "email-smtp-svr-addr"
username = "email-smtp-username"
password = "email-smtp-password"
Expand All @@ -26,13 +28,13 @@ var RootCmd = &cobra.Command{
Short: "Send an email using Cycloid config",
Long: "Send an email using Cycloid config file in order to test different SMTP servers integration",
RunE: func(_ *cobra.Command, _ []string) error {
//c := config.New(viper.GetViper())
viper.SetConfigFile(viper.GetString("cfg"))
err := viper.ReadInConfig()
if err != nil {
return fmt.Errorf("error reading config file: %w", err)
}
cfg := getConfig()
fmt.Printf("CONFIG: %+v\n", cfg)
err = sendEmail(cfg)
if err != nil {
return err
Expand All @@ -59,6 +61,9 @@ func init() {
RootCmd.Flags().StringP(from, "f", "", "From email address to use for any sent email")
viper.BindPFlag(from, RootCmd.Flags().Lookup(from))

RootCmd.Flags().Bool(skiptls, true, "Skip client TLS certs verification")
viper.BindPFlag(from, RootCmd.Flags().Lookup(from))

RootCmd.Flags().StringP(to, "t", "", "send test email to this address")
}

Expand All @@ -71,6 +76,7 @@ func main() {

type config struct {
cfgFile string
skiptls bool
server string
username string
password string
Expand All @@ -81,6 +87,7 @@ type config struct {
func getConfig() config {
return config{
cfgFile: viper.GetString(cfg),
skiptls: viper.GetBool(skiptls),
server: viper.GetString(server),
username: viper.GetString(username),
password: viper.GetString(password),
Expand All @@ -90,13 +97,24 @@ func getConfig() config {
}

func sendEmail(cfg config) error {
// fmt.Println(cfg)
msg := strings.NewReader("Hello from cy-smtp!\nThis is a test message.")

auth := sasl.NewPlainClient("", cfg.username, cfg.password)

log.Println("Sending test email ")
err := smtp.SendMail(cfg.server, auth, cfg.from, []string{cfg.to}, msg)
tlsCfg := &tls.Config{
InsecureSkipVerify: cfg.skiptls,
}
client, err := smtp.DialStartTLS(cfg.server, tlsCfg)
if err != nil {
return fmt.Errorf("error connecting to server: %w", err)
}
auth := sasl.NewPlainClient("", cfg.username, cfg.password)
if ok, _ := client.Extension("AUTH"); !ok {
return fmt.Errorf("smtp: server doesn't support AUTH")
}
if err = client.Auth(auth); err != nil {
return fmt.Errorf("error authenticating to server: %w", err)
}
err = client.SendMail(cfg.from, []string{cfg.to}, msg)
if err != nil {
return fmt.Errorf("error sending email: %w", err)
}
Expand Down

0 comments on commit d768a25

Please sign in to comment.