appcore/email/transport.go

41 lines
884 B
Go
Raw Permalink Normal View History

2025-06-16 22:26:47 +05:30
// Copyright 2024 Patial Tech (Ankit Patial).
//
// This file is part of code.patial.tech/go/appcore, which is MIT licensed.
// See http://opensource.org/licenses/MIT
2025-06-16 22:19:00 +05:30
package email
import (
"os"
"path/filepath"
"time"
"code.patial.tech/go/appcore/open"
)
type Transport interface {
Send(*Message) error
}
// DumpToTemp transport is for development environment to ensure emails are renderd as HTML ok
//
// once dump operation is done it will try to open the html with default app for html
type DumpToTemp struct{}
func (DumpToTemp) Send(msg *Message) error {
// validate msg first
if err := msg.Validate(); err != nil {
return err
}
dir := os.TempDir()
id := time.Now().Format("20060102T150405999")
file := filepath.Join(dir, id+".html")
if err := os.WriteFile(file, []byte(msg.HtmlBody), 0440); err != nil {
return err
}
return open.WithDefaultApp(file)
}