<commit_before>package main

import (
	"crypto/tls"

	"github.com/aymerick/douceur/inliner"
	"github.com/drone/drone-go/drone"
	"github.com/drone/drone-go/template"
	"github.com/go-gomail/gomail"
	"github.com/jaytaylor/html2text"
)

func Send(context *Context) error {
	payload := &drone.Payload{
		System: &context.System,
		Repo:   &context.Repo,
		Build:  &context.Build,
	}

	subject, plain, html, err := build(
		payload,
		context,
	)

	if err != nil {
		return err
	}

	return send(
		subject,
		plain,
		html,
		context,
	)
}

func build(payload *drone.Payload, context *Context) (string, string, string, error) {
	subject, err := template.RenderTrim(
		context.Vargs.Subject,
		payload)

	if err != nil {
		return "", "", "", err
	}

	body, err := template.RenderTrim(
		context.Vargs.Template,
		payload,
	)

	if err != nil {
		return "", "", "", err
	}

	html, err := inliner.Inline(body)

	if err != nil {
		return "", "", "", err
	}

	plain, err := html2text.FromString(
		html,
	)

	if err != nil {
		return "", "", "", err
	}

	return subject, plain, html, nil
}

func send(subject, plainBody, htmlBody string, c *Context) error {
	if len(c.Vargs.Recipients) == 0 {
		c.Vargs.Recipients = []string{
			c.Build.Email,
		}
	}

	m := gomail.NewMessage()

	m.SetHeader(
		"To",
		c.Vargs.Recipients...,
	)

	m.SetHeader(
		"From",
		c.Vargs.From,
	)

	m.SetHeader(
		"Subject",
		subject,
	)

	m.AddAlternative(
		"text/plain",
		plainBody,
	)

	m.AddAlternative(
		"text/html",
		htmlBody,
	)

	d := gomail.NewPlainDialer(
		c.Vargs.Host,
		c.Vargs.Port,
		c.Vargs.Username,
		c.Vargs.Password,
	)

	if c.Vargs.SkipVerify {
		d.TLSConfig = &tls.Config{
			InsecureSkipVerify: true,
		}
	}

	return d.DialAndSend(m)
}<commit_msg>Support email notification without SMTP auth.<commit_after>package main

import (
	"crypto/tls"

	"github.com/aymerick/douceur/inliner"
	"github.com/drone/drone-go/drone"
	"github.com/drone/drone-go/template"
	"github.com/go-gomail/gomail"
	"github.com/jaytaylor/html2text"
)

func Send(context *Context) error {
	payload := &drone.Payload{
		System: &context.System,
		Repo:   &context.Repo,
		Build:  &context.Build,
	}

	subject, plain, html, err := build(
		payload,
		context,
	)

	if err != nil {
		return err
	}

	return send(
		subject,
		plain,
		html,
		context,
	)
}

func build(payload *drone.Payload, context *Context) (string, string, string, error) {
	subject, err := template.RenderTrim(
		context.Vargs.Subject,
		payload)

	if err != nil {
		return "", "", "", err
	}

	body, err := template.RenderTrim(
		context.Vargs.Template,
		payload,
	)

	if err != nil {
		return "", "", "", err
	}

	html, err := inliner.Inline(body)

	if err != nil {
		return "", "", "", err
	}

	plain, err := html2text.FromString(
		html,
	)

	if err != nil {
		return "", "", "", err
	}

	return subject, plain, html, nil
}

func send(subject, plainBody, htmlBody string, c *Context) error {
	if len(c.Vargs.Recipients) == 0 {
		c.Vargs.Recipients = []string{
			c.Build.Email,
		}
	}

	m := gomail.NewMessage()

	m.SetHeader(
		"To",
		c.Vargs.Recipients...,
	)

	m.SetHeader(
		"From",
		c.Vargs.From,
	)

	m.SetHeader(
		"Subject",
		subject,
	)

	m.AddAlternative(
		"text/plain",
		plainBody,
	)

	m.AddAlternative(
		"text/html",
		htmlBody,
	)

	var d *gomail.Dialer
	if c.Vargs.Username == "" {
		d = &gomail.Dialer{
			Host: c.Vargs.Host,
			Port: c.Vargs.Port,
			SSL:  c.Vargs.Port == 465,
		}
	} else {
		d = gomail.NewDialer(
			c.Vargs.Host,
			c.Vargs.Port,
			c.Vargs.Username,
			c.Vargs.Password,
		)
	}

	if c.Vargs.SkipVerify {
		d.TLSConfig = &tls.Config{
			InsecureSkipVerify: true,
		}
	}

	return d.DialAndSend(m)
}
{'fraction_non_alphanumeric': 0.16503534529635672, 'fraction_numerical': 0.0024469820554649264, 'mean_word_length': 3.0473047304730474, 'pattern_counts': {'":': 0, '<': 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': '7601962', 'n_tokens_mistral': 1666, 'n_tokens_neox': 1505, 'n_words': 290}