<commit_before>package welcemail

import (
	"errors"

	"github.com/go-gomail/gomail"
	"github.com/skygeario/skygear-server/pkg/auth/model"
	authTemplate "github.com/skygeario/skygear-server/pkg/auth/template"
	"github.com/skygeario/skygear-server/pkg/core/config"
	"github.com/skygeario/skygear-server/pkg/core/mail"
	"github.com/skygeario/skygear-server/pkg/core/template"
)

type Sender interface {
	Send(email string, user model.User) error
}

type DefaultSender struct {
	AppName        string
	Config         config.WelcomeEmailConfiguration
	Dialer         *gomail.Dialer
	TemplateEngine *template.Engine
}

func NewDefaultSender(
	config config.TenantConfiguration,
	dialer *gomail.Dialer,
	templateEngine *template.Engine,
) Sender {
	return &DefaultSender{
		AppName:        config.AppName,
		Config:         config.UserConfig.WelcomeEmail,
		Dialer:         dialer,
		TemplateEngine: templateEngine,
	}
}

func (d *DefaultSender) Send(email string, user model.User) (err error) {
	if d.Config.TextURL == "" {
		return errors.New("welcome email text template url is empty")
	}

	context := map[string]interface{}{
		"appname":    d.AppName,
		"email":      email,
		"user":       user,
		"url_prefix": d.Config.URLPrefix,
	}

	var textBody string
	if textBody, err = d.TemplateEngine.ParseTextTemplate(
		authTemplate.TemplateNameWelcomeEmailText,
		context,
		template.ParseOption{Required: true},
	); err != nil {
		return
	}

	var htmlBody string
	if htmlBody, err = d.TemplateEngine.ParseHTMLTemplate(
		authTemplate.TemplateNameWelcomeEmailHTML,
		context,
		template.ParseOption{Required: false},
	); err != nil {
		return
	}

	sendReq := mail.SendRequest{
		Dialer:    d.Dialer,
		Sender:    d.Config.Sender,
		Recipient: email,
		Subject:   d.Config.Subject,
		ReplyTo:   d.Config.ReplyTo,
		TextBody:  textBody,
		HTMLBody:  htmlBody,
	}

	err = sendReq.Execute()
	return
}
<commit_msg>Make welcome email text template url optional<commit_after>package welcemail

import (
	"github.com/go-gomail/gomail"
	"github.com/skygeario/skygear-server/pkg/auth/model"
	authTemplate "github.com/skygeario/skygear-server/pkg/auth/template"
	"github.com/skygeario/skygear-server/pkg/core/config"
	"github.com/skygeario/skygear-server/pkg/core/mail"
	"github.com/skygeario/skygear-server/pkg/core/template"
)

type Sender interface {
	Send(email string, user model.User) error
}

type DefaultSender struct {
	AppName        string
	Config         config.WelcomeEmailConfiguration
	Dialer         *gomail.Dialer
	TemplateEngine *template.Engine
}

func NewDefaultSender(
	config config.TenantConfiguration,
	dialer *gomail.Dialer,
	templateEngine *template.Engine,
) Sender {
	return &DefaultSender{
		AppName:        config.AppName,
		Config:         config.UserConfig.WelcomeEmail,
		Dialer:         dialer,
		TemplateEngine: templateEngine,
	}
}

func (d *DefaultSender) Send(email string, user model.User) (err error) {
	context := map[string]interface{}{
		"appname":    d.AppName,
		"email":      email,
		"user":       user,
		"url_prefix": d.Config.URLPrefix,
	}

	var textBody string
	if textBody, err = d.TemplateEngine.ParseTextTemplate(
		authTemplate.TemplateNameWelcomeEmailText,
		context,
		template.ParseOption{Required: true},
	); err != nil {
		return
	}

	var htmlBody string
	if htmlBody, err = d.TemplateEngine.ParseHTMLTemplate(
		authTemplate.TemplateNameWelcomeEmailHTML,
		context,
		template.ParseOption{Required: false},
	); err != nil {
		return
	}

	sendReq := mail.SendRequest{
		Dialer:    d.Dialer,
		Sender:    d.Config.Sender,
		Recipient: email,
		Subject:   d.Config.Subject,
		ReplyTo:   d.Config.ReplyTo,
		TextBody:  textBody,
		HTMLBody:  htmlBody,
	}

	err = sendReq.Execute()
	return
}

{'fraction_non_alphanumeric': 0.11173633440514469, 'fraction_numerical': 0.0, 'mean_word_length': 3.8606770833333335, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4318647', 'n_tokens_mistral': 1346, 'n_tokens_neox': 1276, 'n_words': 245}