...

Source file src/github.com/docker/distribution/registry/handlers/mail.go

Documentation: github.com/docker/distribution/registry/handlers

     1  package handlers
     2  
     3  import (
     4  	"errors"
     5  	"net/smtp"
     6  	"strings"
     7  )
     8  
     9  // mailer provides fields of email configuration for sending.
    10  type mailer struct {
    11  	Addr, Username, Password, From string
    12  	Insecure                       bool
    13  	To                             []string
    14  }
    15  
    16  // sendMail allows users to send email, only if mail parameters is configured correctly.
    17  func (mail *mailer) sendMail(subject, message string) error {
    18  	addr := strings.Split(mail.Addr, ":")
    19  	if len(addr) != 2 {
    20  		return errors.New("invalid Mail Address")
    21  	}
    22  	host := addr[0]
    23  	msg := []byte("To:" + strings.Join(mail.To, ";") +
    24  		"\r\nFrom: " + mail.From +
    25  		"\r\nSubject: " + subject +
    26  		"\r\nContent-Type: text/plain\r\n\r\n" +
    27  		message)
    28  	auth := smtp.PlainAuth(
    29  		"",
    30  		mail.Username,
    31  		mail.Password,
    32  		host,
    33  	)
    34  	err := smtp.SendMail(
    35  		mail.Addr,
    36  		auth,
    37  		mail.From,
    38  		mail.To,
    39  		msg,
    40  	)
    41  	if err != nil {
    42  		return err
    43  	}
    44  	return nil
    45  }
    46  

View as plain text