12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- package smtp
- import (
- "crypto/tls"
- "fmt"
- "net/smtp"
- "github.com/pkg/errors"
- )
- type Config struct {
- Auth string
- Host string
- Port int
- AllowedDomains string
- TLS bool `ini:"tls"`
- SkipVerify bool
- }
- func (c *Config) doAuth(auth smtp.Auth) error {
- client, err := smtp.Dial(fmt.Sprintf("%s:%d", c.Host, c.Port))
- if err != nil {
- return err
- }
- defer client.Close()
- if err = client.Hello("gogs"); err != nil {
- return err
- }
- if c.TLS {
- if ok, _ := client.Extension("STARTTLS"); ok {
- if err = client.StartTLS(&tls.Config{
- InsecureSkipVerify: c.SkipVerify,
- ServerName: c.Host,
- }); err != nil {
- return err
- }
- } else {
- return errors.New("SMTP server does not support TLS")
- }
- }
- if ok, _ := client.Extension("AUTH"); ok {
- if err = client.Auth(auth); err != nil {
- return err
- }
- return nil
- }
- return errors.New("unsupported SMTP authentication method")
- }
|