github.com/blend/go-sdk@v1.20220411.3/examples/email/smtp/main.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package main
     9  
    10  import (
    11  	"context"
    12  	"flag"
    13  	"fmt"
    14  
    15  	"github.com/blend/go-sdk/configutil"
    16  	"github.com/blend/go-sdk/email"
    17  	"github.com/blend/go-sdk/logger"
    18  	"github.com/blend/go-sdk/stringutil"
    19  )
    20  
    21  type flagStrings []string
    22  
    23  func (fs *flagStrings) Values() []string {
    24  	return []string(*fs)
    25  }
    26  
    27  func (fs *flagStrings) String() string {
    28  	return "an array of strings"
    29  }
    30  
    31  func (fs *flagStrings) Set(value string) error {
    32  	*fs = append(*fs, value)
    33  	return nil
    34  }
    35  
    36  func main() {
    37  	var to flagStrings
    38  	var (
    39  		from    = flag.String("from", "noreply@example.org", "The message `from` address")
    40  		subject = flag.String("subject", "", "The message `subject`")
    41  		body    = flag.String(`body`, "", "The message `body`")
    42  	)
    43  	flag.Var(&to, "to", "The message `to` address(es), can be more than one.")
    44  	flag.Parse()
    45  
    46  	log := logger.Prod()
    47  
    48  	var sender email.SMTPSender
    49  	if _, err := configutil.Read(&sender); !configutil.IsIgnored(err) {
    50  		logger.FatalExit(err)
    51  	}
    52  
    53  	message := email.Message{
    54  		From:     *from,
    55  		To:       to.Values(),
    56  		Subject:  *subject,
    57  		TextBody: *body,
    58  	}
    59  
    60  	log.Infof("using smtp host:     %s", sender.Host)
    61  	log.Infof("using smtp username: %s", sender.PlainAuth.Username)
    62  	log.Infof("using smtp port:     %s", sender.PortOrDefault())
    63  	log.Infof("using message from address:  %s", *from)
    64  	log.Infof("using message to addresses:  %s", stringutil.CSV(to.Values()))
    65  	log.Infof("using message subject:       %s", *subject)
    66  
    67  	if err := sender.Send(context.Background(), message); err != nil {
    68  		logger.FatalExit(err)
    69  	}
    70  	fmt.Println("message sent!")
    71  }