Beispiel #1
0
func (w *SmtpWriter) Write(level LLevel, flags int, b []byte) (int, error) {
	if w.server == "" || len(w.to) == 0 {
		return 0, nil
	}

	hostname, _ := os.Hostname()
	subject := fmt.Sprintf("%s message on %s", level.String(), hostname)
	err := mail.Send(&mail.Message{
		To:       w.to,
		Subject:  subject,
		TextBody: string(b),
	})
	if err != nil {
		return 0, err
	}
	return len(b), nil
}
Beispiel #2
0
// SendMail is a shorthand function for sending an email from a template.
// If the loaded gnd.la/template.Template.ContentType() returns a string
// containing "html", the gnd.la/net/mail.Message HTMLBody field is set, other
// the TextBody field is used. Note that if template is empty, the msg is
// passed unmodified to mail.Send(). Other Message fields are never altered.
//
// Note: mail.Send does not work on App Engine, users must always use this function instead.
func (c *Context) SendMail(template string, data interface{}, msg *mail.Message) error {
	if template != "" {
		t, err := c.app.LoadTemplate(template)
		if err != nil {
			return err
		}
		if msg == nil {
			msg = &mail.Message{}
		}
		var buf bytes.Buffer
		if err := t.ExecuteTo(&buf, c, data); err != nil {
			return err
		}
		if strings.Contains(t.tmpl.ContentType(), "/html") {
			msg.HTMLBody = buf.String()
		} else {
			msg.TextBody = buf.String()
		}
	}
	c.prepareMessage(msg)
	return mail.Send(msg)
}