github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/common/email.go (about)

     1  package common
     2  
     3  import (
     4  	"crypto/tls"
     5  	"fmt"
     6  	"net/mail"
     7  	"net/smtp"
     8  	"strings"
     9  
    10  	p "github.com/Azareal/Gosora/common/phrases"
    11  )
    12  
    13  func SendActivationEmail(username, email, token string) error {
    14  	schema := "http"
    15  	if Config.SslSchema {
    16  		schema += "s"
    17  	}
    18  	// TODO: Move these to the phrase system
    19  	subject := "Account Activation - " + Site.Name
    20  	msg := "Dear " + username + ", to complete your registration on our forums, we need you to validate your email, so that we can confirm that this email actually belongs to you.\n\nClick on the following link to do so. " + schema + "://" + Site.URL + "/user/edit/token/" + token + "\n\nIf you haven't created an account here, then please feel free to ignore this email.\nWe're sorry for the inconvenience this may have caused."
    21  	return SendEmail(email, subject, msg)
    22  }
    23  
    24  func SendValidationEmail(username, email, token string) error {
    25  	schema := "http"
    26  	if Config.SslSchema {
    27  		schema += "s"
    28  	}
    29  	r := func(body *string) func(name, val string) {
    30  		return func(name, val string) {
    31  			*body = strings.Replace(*body, "{{"+name+"}}", val, -1)
    32  		}
    33  	}
    34  	subject := p.GetAccountPhrase("ValidateEmailSubject")
    35  	r1 := r(&subject)
    36  	r1("name", Site.Name)
    37  	body := p.GetAccountPhrase("ValidateEmailBody")
    38  	r2 := r(&body)
    39  	r2("username", username)
    40  	r2("schema", schema)
    41  	r2("url", Site.URL)
    42  	r2("token", token)
    43  	return SendEmail(email, subject, body)
    44  }
    45  
    46  // TODO: Refactor this
    47  func SendEmail(email, subject, msg string) (err error) {
    48  	// This hook is useful for plugin_sendmail or for testing tools. Possibly to hook it into some sort of mail server?
    49  	ret, hasHook := GetHookTable().VhookNeedHook("email_send_intercept", email, subject, msg)
    50  	if hasHook {
    51  		return ret.(error)
    52  	}
    53  
    54  	from := mail.Address{"", Site.Email}
    55  	to := mail.Address{"", email}
    56  	headers := make(map[string]string)
    57  	headers["From"] = from.String()
    58  	headers["To"] = to.String()
    59  	headers["Subject"] = subject
    60  
    61  	body := ""
    62  	for k, v := range headers {
    63  		body += fmt.Sprintf("%s: %s\r\n", k, v)
    64  	}
    65  	body += "\r\n" + msg
    66  
    67  	var c *smtp.Client
    68  	var conn *tls.Conn
    69  	if Config.SMTPEnableTLS {
    70  		tlsconfig := &tls.Config{
    71  			InsecureSkipVerify: true,
    72  			ServerName:         Config.SMTPServer,
    73  		}
    74  		conn, err = tls.Dial("tcp", Config.SMTPServer+":"+Config.SMTPPort, tlsconfig)
    75  		if err != nil {
    76  			LogWarning(err)
    77  			return err
    78  		}
    79  		c, err = smtp.NewClient(conn, Config.SMTPServer)
    80  	} else {
    81  		c, err = smtp.Dial(Config.SMTPServer + ":" + Config.SMTPPort)
    82  	}
    83  	if err != nil {
    84  		LogWarning(err)
    85  		return err
    86  	}
    87  
    88  	if Config.SMTPUsername != "" {
    89  		auth := smtp.PlainAuth("", Config.SMTPUsername, Config.SMTPPassword, Config.SMTPServer)
    90  		err = c.Auth(auth)
    91  		if err != nil {
    92  			LogWarning(err)
    93  			return err
    94  		}
    95  	}
    96  	if err = c.Mail(from.Address); err != nil {
    97  		LogWarning(err)
    98  		return err
    99  	}
   100  	if err = c.Rcpt(to.Address); err != nil {
   101  		LogWarning(err)
   102  		return err
   103  	}
   104  
   105  	w, err := c.Data()
   106  	if err != nil {
   107  		LogWarning(err)
   108  		return err
   109  	}
   110  	_, err = w.Write([]byte(body))
   111  	if err != nil {
   112  		LogWarning(err)
   113  		return err
   114  	}
   115  	if err = w.Close(); err != nil {
   116  		LogWarning(err)
   117  		return err
   118  	}
   119  	if err = c.Quit(); err != nil {
   120  		LogWarning(err)
   121  		return err
   122  	}
   123  
   124  	return nil
   125  }