예제 #1
0
// inlines CSS into HTML and display result
func inlineCSS(filePath string) {
	input := readFile(filePath)

	output, err := inliner.Inline(string(input))
	if err != nil {
		fmt.Println("Inlining error: ", err)
		os.Exit(1)
	}

	fmt.Println(output)
}
예제 #2
0
파일: template.go 프로젝트: noblehng/bosun
func (s *Schedule) ExecuteBody(rh *RunHistory, a *conf.Alert, st *models.IncidentState, isEmail bool) ([]byte, []*models.Attachment, error) {
	t := a.Template
	if t == nil || t.Body == nil {
		return nil, nil, nil
	}
	c := s.Data(rh, st, a, isEmail)
	buf := new(bytes.Buffer)
	if err := t.Body.Execute(buf, c); err != nil {
		return nil, nil, err
	}
	if inline, err := inliner.Inline(buf.String()); err == nil {
		buf = bytes.NewBufferString(inline)
	} else {
		slog.Errorln(err)
	}
	return buf.Bytes(), c.Attachments, nil
}
예제 #3
0
// Send sends email message using mailgun
func (s *Service) Send(m *Message) error {
	// Format recipients
	var formattedRecipients []string
	for _, recipient := range m.Recipients {
		formattedAddress, err := recipient.Format()
		if err != nil {
			return err
		}
		formattedRecipients = append(formattedRecipients, formattedAddress)
	}

	// Format the sender
	formattedSender, err := m.From.Format()
	if err != nil {
		return err
	}

	// Create email message
	message := mailgun.NewMessage(
		formattedSender,
		m.Subject,
		m.Text,
		formattedRecipients...,
	)

	// Optionally set HTML body
	if m.HTML != "" {
		htmlWithInlineCSS, err := inliner.Inline(m.HTML)
		if err != nil {
			return fmt.Errorf("CSS inliner error: %s", err.Error())
		}
		message.SetHtml(htmlWithInlineCSS)
	}

	// TODO - do we need to return other values than error here?
	mg := mailgun.NewMailgun(
		s.cnf.Mailgun.Domain,
		s.cnf.Mailgun.APIKey,
		s.cnf.Mailgun.PublicAPIKey,
	)
	_, _, err = mg.Send(message)
	return err
}
예제 #4
0
func renderHtmlText(model *EmailModel) (string, error) {
	t, err := html.New("").Funcs(funcMap).Parse(htmlTextTemplate)
	if err != nil {
		return "", err
	}

	var b bytes.Buffer
	err = t.Execute(&b, model)
	if err != nil {
		return "", err
	}

	emailHtml, err := inliner.Inline(b.String())
	if err != nil {
		return "", err
	}

	return emailHtml, nil
}