Exemple #1
0
func (ec *EmailConfig) Get(cfg *ServerConfig) (*templates.Templater, emails.Deliverer, error) {
	proto.DefaultCommonEmailParams = *cfg.CommonEmailParams
	localDomain := cfg.CommonEmailParams.EmailDomain
	cfg.CommonEmailParams.CommonData.LocalDomain = localDomain

	// Load templates and configure email sender.
	templater := &templates.Templater{}
	// TODO: replace -static with a better sense of a static root
	if errs := templater.Load(filepath.Join(cfg.StaticPath, "..", "email")); errs != nil {
		return nil, nil, errs[0]
	}

	// Verify templates.
	if errs := proto.ValidateEmailTemplates(templater); errs != nil {
		for _, err := range errs {
			fmt.Printf("error: %s\n", err)
		}
		return nil, nil, fmt.Errorf("template validation failed: %s...", errs[0].Error())
	}

	// Set up deliverer.
	fmt.Printf("setting up deliverer for %#v\n", ec)
	switch ec.Server {
	case "":
		return templater, nil, nil
	case "$stdout":
		return templater, &mockDeliverer{Writer: os.Stdout}, nil
	default:
		var sslHost string
		if ec.UseTLS {
			var err error
			sslHost, _, err = net.SplitHostPort(ec.Server)
			if err != nil {
				return nil, nil, err
			}
		}

		var auth smtp.Auth
		switch strings.ToUpper(ec.AuthMethod) {
		case "":
		case "CRAM-MD5":
			auth = smtp.CRAMMD5Auth(ec.Username, ec.Password)
		case "PLAIN":
			if !ec.UseTLS {
				return nil, nil, fmt.Errorf("PLAIN authentication requires TLS")
			}
			auth = smtp.PlainAuth(ec.Identity, ec.Username, ec.Password, sslHost)
		}

		deliverer := emails.NewSMTPDeliverer(localDomain, ec.Server, sslHost, auth)
		return templater, deliverer, nil
	}
}
Exemple #2
0
func (ec *EmailConfig) Get(cfg *ServerConfig) (emails.Emailer, error) {
	proto.DefaultCommonEmailParams = cfg.CommonEmailParams
	localDomain := cfg.CommonEmailParams.EmailDomain
	cfg.CommonEmailParams.CommonData.LocalDomain = localDomain

	if ec.Server == "" {
		return &emails.TestEmailer{}, nil
	}

	var sslHost string
	if ec.UseTLS {
		var err error
		sslHost, _, err = net.SplitHostPort(ec.Server)
		if err != nil {
			return nil, err
		}
	}

	var auth smtp.Auth
	switch strings.ToUpper(ec.AuthMethod) {
	case "":
	case "CRAM-MD5":
		auth = smtp.CRAMMD5Auth(ec.Username, ec.Password)
	case "PLAIN":
		if !ec.UseTLS {
			return nil, fmt.Errorf("PLAIN authentication requires TLS")
		}
		auth = smtp.PlainAuth(ec.Identity, ec.Username, ec.Password, sslHost)
	}

	// Load templates and configure email sender.
	emailer, err := emails.NewSMTPEmailer(filepath.Join(cfg.StaticPath, "email"), localDomain, ec.Server, sslHost, auth)
	if err != nil {
		return nil, err
	}

	// Verify templates.
	if errs := proto.ValidateEmailTemplates(emailer.Templater); errs != nil {
		for _, err := range errs {
			fmt.Printf("error: %s\n", err)
		}
		return nil, fmt.Errorf("template validation failed: %s...", errs[0].Error())
	}

	return emailer, nil
}