// Send sends the email with the given template name and data to the specified recipient. If recipient is blank, the user's email is used as the recipient.
Send sends the email with the given template name and data to the specified recipient. If recipient is blank, the user's email is used as the recipient.
// Send sends the email with the given template name and data to the specified recipient . If recipient is blank the user ' s email is used as the recipient .
func Send(recipient string, user *data.User, templateName string, data map[string]interface{}) error {
	emailConfig := config.GetCurrent().Email
	if !emailConfig.Enabled {
		return ErrDisabled
	}
	toAddress := recipient
	toFull := recipient
	if toAddress == "" {
		toAddress = user.Email
		if user.Name != "" {
			toFull = fmt.Sprintf("%s <%s>", user.Name, user.Email)
		} else {
			toFull = user.Email
		}
	}
	dot := templateData{user, toAddress, data}
	templateLocation := "templates/"
	basePath := filepath.Join(templateLocation, "base.html")
	baseFolder := filepath.Join(templateLocation, templateName)
	subject, err := renderTextTemplate(filepath.Join(baseFolder, "subject.txt"), dot)
	if err != nil {
		return err
	}
	textMessage, err := renderTextTemplate(filepath.Join(baseFolder, "template.txt"), dot)
	if err != nil {
		return err
	}
	htmlMessage, err := renderHTMLTemplate(basePath, filepath.Join(baseFolder, "template.html"), dot)
	if err != nil {
		return err
	}
	textEncodedBuf := bytes.NewBufferString("")
	textEncodedWriter := quotedprintable.NewWriter(textEncodedBuf)
	textEncodedWriter.Write([]byte(textMessage))
	textEncodedWriter.Close()
	htmlEncodedBuf := bytes.NewBufferString("")
	htmlEncodedWriter := quotedprintable.NewWriter(htmlEncodedBuf)
	htmlEncodedWriter.Write([]byte(htmlMessage))
	htmlEncodedWriter.Close()
	messageIDFirst := strconv.FormatInt(time.Now().Unix(), 10) + "." + strconv.Itoa(rand.Intn(999999))
	messageIDDomain := strings.Split(emailConfig.SMTPUsername, "@")[1]
	rawMessage := []byte(
		"From: " + emailConfig.FromDisplay + "\r\n" +
			"To: " + toFull + "\r\n" +
			"Subject: " + subject + "\r\n" +
			"Sender: " + emailConfig.FromDisplay + "\r\n" +
			"Message-ID: <" + messageIDFirst + "@" + messageIDDomain + ">\r\n" +
			"Date: " + time.Now().Format("Mon, 02 Jan 2006 15:04:05 -0700") + "\r\n" +
			"MIME-Version: 1.0\r\n" +
			"Content-Type: multipart/alternative; boundary=\"mimeboundary\"\r\n\r\n" +
			"--mimeboundary\r\n" +
			"Content-Type: text/plain; charset=\"UTF-8\"\r\n" +
			"Content-Transfer-Encoding: quoted-printable\r\n\r\n" +
			textEncodedBuf.String() + "\r\n" +
			"--mimeboundary\r\n" +
			"Content-Type: text/html; charset=\"UTF-8\"\r\n" +
			"Content-Transfer-Encoding: quoted-printable\r\n\r\n" +
			htmlEncodedBuf.String() + "\r\n" +
			"--mimeboundary--")
	auth := smtp.PlainAuth("", emailConfig.SMTPUsername, emailConfig.SMTPPassword, emailConfig.SMTPHost)
	err = smtp.SendMail(emailConfig.SMTPHost+":"+strconv.Itoa(emailConfig.SMTPPort), auth, emailConfig.FromAddress, []string{toAddress}, rawMessage)
	if err != nil {
		return err
	}
	return nil
}
func Send ( recipient string , user * data . User , templateName string , data map [ string ] interface { } ) error { emailConfig := config . GetCurrent ( ) . Email if ! emailConfig . Enabled { return ErrDisabled } toAddress := recipient toFull := recipient if toAddress == " " { toAddress = user . Email if user . Name != " " { toFull = fmt . Sprintf ( " " , user . Name , user . Email ) } else { toFull = user . Email } } dot := templateData { user , toAddress , data } templateLocation := " " basePath := filepath . Join ( templateLocation , " " ) baseFolder := filepath . Join ( templateLocation , templateName ) subject , err := renderTextTemplate ( filepath . Join ( baseFolder , " " ) , dot ) if err != nil { return err } textMessage , err := renderTextTemplate ( filepath . Join ( baseFolder , " " ) , dot ) if err != nil { return err } htmlMessage , err := renderHTMLTemplate ( basePath , filepath . Join ( baseFolder , " " ) , dot ) if err != nil { return err } textEncodedBuf := bytes . NewBufferString ( " " ) textEncodedWriter := quotedprintable . NewWriter ( textEncodedBuf ) textEncodedWriter . Write ( [ ] byte ( textMessage ) ) textEncodedWriter . Close ( ) htmlEncodedBuf := bytes . NewBufferString ( " " ) htmlEncodedWriter := quotedprintable . NewWriter ( htmlEncodedBuf ) htmlEncodedWriter . Write ( [ ] byte ( htmlMessage ) ) htmlEncodedWriter . Close ( ) messageIDFirst := strconv . FormatInt ( time . Now ( ) . Unix ( ) , 10 ) + " " + strconv . Itoa ( rand . Intn ( 999999 ) ) messageIDDomain := strings . Split ( emailConfig . SMTPUsername , " " ) [ 1 ] rawMessage := [ ] byte ( " " + emailConfig . FromDisplay + " \r \n " + " " + toFull + " \r \n " + " " + subject + " \r \n " + " " + emailConfig . FromDisplay + " \r \n " + " " + messageIDFirst + " " + messageIDDomain + " \r \n " + " " + time . Now ( ) . Format ( " " ) + " \r \n " + " \r \n " + " \" \" \r \n \r \n " + " \r \n " + " \" \" \r \n " + " \r \n \r \n " + textEncodedBuf . String ( ) + " \r \n " + " \r \n " + " \" \" \r \n " + " \r \n \r \n " + htmlEncodedBuf . String ( ) + " \r \n " + " " ) auth := smtp . PlainAuth ( " " , emailConfig . SMTPUsername , emailConfig . SMTPPassword , emailConfig . SMTPHost ) err = smtp . SendMail ( emailConfig . SMTPHost + " " + strconv . Itoa ( emailConfig . SMTPPort ) , auth , emailConfig . FromAddress , [ ] string { toAddress } , rawMessage ) if err != nil { return err } return nil }
Send sends the email with the given template name and data to the specified recipient.
Send sends the email with the given template name and data to the specified recipient .
// use templates // encode as quotedprintable // generate message id