github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/builtins/pipes/mail/utils.go (about)

     1  package mail
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os"
     7  	"os/user"
     8  	"regexp"
     9  
    10  	"github.com/lmorg/murex/app"
    11  )
    12  
    13  var rxGetDomain = regexp.MustCompile(`@([\.\-a-zA-Z0-9]+)$`)
    14  
    15  func getDomain(email string) (string, error) {
    16  	match := rxGetDomain.FindAllStringSubmatch(email, 1)
    17  
    18  	if len(match) != 1 {
    19  		return "", fmt.Errorf("Could not extract domain from %s: Parsed as %v", email, match)
    20  	}
    21  
    22  	if len(match[0]) != 2 {
    23  		return "", fmt.Errorf("Could not extract domain from %s: Parsed as %v", email, match)
    24  	}
    25  
    26  	return match[0][1], nil
    27  }
    28  
    29  func hostname() string {
    30  	s, err := os.Hostname()
    31  	if err != nil {
    32  		return "localhost"
    33  	}
    34  
    35  	return s
    36  }
    37  
    38  func username() string {
    39  	u, err := user.Current()
    40  	if err != nil {
    41  		return app.Name
    42  	}
    43  
    44  	return u.Username
    45  }
    46  
    47  func setSubject(w io.Writer, subject string) error {
    48  	_, err := fmt.Fprintf(w, "Subject: %s\nMIME-version: 1.0;\nContent-Type: text/text\n\n", subject)
    49  	if err != nil {
    50  		return fmt.Errorf("Unable to write Subject header: %s", err.Error())
    51  	}
    52  	return nil
    53  }