initial commit
This commit is contained in:
31
cmd/inline.go
Normal file
31
cmd/inline.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// inlineCmd represents the send command
|
||||
var inlineCmd = &cobra.Command{
|
||||
Use: "inline",
|
||||
Short: "Inline styles in template without sending",
|
||||
Long: ``,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Println("send called")
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(inlineCmd)
|
||||
|
||||
// Here you will define your flags and configuration settings.
|
||||
|
||||
// Cobra supports Persistent Flags which will work for this command
|
||||
// and all subcommands, e.g.:
|
||||
// inlineCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||
|
||||
// Cobra supports local flags which will only run when this command
|
||||
// is called directly, e.g.:
|
||||
// inlineCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
}
|
68
cmd/root.go
Normal file
68
cmd/root.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var cfgFile string
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "gomailer",
|
||||
Short: "use it to test email templates",
|
||||
Long: ``,
|
||||
// Uncomment the following line if your bare application
|
||||
// has an action associated with it:
|
||||
// Run: func(cmd *cobra.Command, args []string) { },
|
||||
}
|
||||
|
||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||
func Execute() {
|
||||
err := rootCmd.Execute()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
cobra.OnInitialize(initConfig)
|
||||
|
||||
// Here you will define your flags and configuration settings.
|
||||
// Cobra supports persistent flags, which, if defined here,
|
||||
// will be global for your application.
|
||||
|
||||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.gomailer.yaml)")
|
||||
|
||||
// Cobra also supports local flags, which will only run
|
||||
// when this action is called directly.
|
||||
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
}
|
||||
|
||||
// initConfig reads in config file and ENV variables if set.
|
||||
func initConfig() {
|
||||
if cfgFile != "" {
|
||||
// Use config file from the flag.
|
||||
viper.SetConfigFile(cfgFile)
|
||||
} else {
|
||||
// Find home directory.
|
||||
home, err := os.UserHomeDir()
|
||||
cobra.CheckErr(err)
|
||||
|
||||
// Search config in home directory with name ".gomailer" (without extension).
|
||||
viper.AddConfigPath(home)
|
||||
viper.SetConfigType("yaml")
|
||||
viper.SetConfigName(".gomailer")
|
||||
}
|
||||
|
||||
viper.AutomaticEnv() // read in environment variables that match
|
||||
|
||||
// If a config file is found, read it in.
|
||||
if err := viper.ReadInConfig(); err == nil {
|
||||
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
|
||||
}
|
||||
}
|
94
cmd/sendmail.go
Normal file
94
cmd/sendmail.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
ht "html/template"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/vanng822/go-premailer/premailer"
|
||||
"github.com/wneessen/go-mail"
|
||||
)
|
||||
|
||||
var help bool
|
||||
var inline bool
|
||||
var template string
|
||||
var output string
|
||||
|
||||
// sendmailCmd represents the sendmail command
|
||||
var sendmailCmd = &cobra.Command{
|
||||
Use: "sendmail",
|
||||
Short: "Send mail with the configured server",
|
||||
Long: `Send Emails for testing Mail templates:
|
||||
|
||||
sendmail.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
prem, err := premailer.NewPremailerFromFile(template, premailer.NewOptions())
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
html, err := prem.Transform()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
f, err := os.Create(template + "_inlined.html")
|
||||
defer f.Close()
|
||||
f.WriteString(html)
|
||||
f.Sync()
|
||||
|
||||
// fmt.Println(html)
|
||||
|
||||
m := mail.NewMsg()
|
||||
if err := m.From("sender@domain.local"); err != nil {
|
||||
log.Fatalf("failed to set From address: %s", err)
|
||||
}
|
||||
if err := m.To("recipient@domain.local"); err != nil {
|
||||
log.Fatalf("failed to set To address: %s", err)
|
||||
}
|
||||
m.Subject("Subject")
|
||||
|
||||
htpl, err := ht.New("htmltpl").Parse(html)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to parse text template: %s", err)
|
||||
}
|
||||
|
||||
// embedd images
|
||||
for _, val := range findSrcInHtml(html) {
|
||||
log.Println("embedding: " + val)
|
||||
m.EmbedFile(val)
|
||||
}
|
||||
|
||||
m.SetBodyHTMLTemplate(htpl, nil)
|
||||
|
||||
c, err := mail.NewClient("localhost", mail.WithPort(25), mail.WithTLSPolicy(mail.NoTLS))
|
||||
if err != nil {
|
||||
log.Fatalf("failed to create mail client: %s", err)
|
||||
}
|
||||
if err := c.DialAndSend(m); err != nil {
|
||||
log.Fatalf("failed to send mail: %s", err)
|
||||
}
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(sendmailCmd)
|
||||
|
||||
// Here you will define your flags and configuration settings.
|
||||
|
||||
// Cobra supports Persistent Flags which will work for this command
|
||||
// and all subcommands, e.g.:
|
||||
// sendmailCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||
|
||||
// Cobra supports local flags which will only run when this command
|
||||
// is called directly, e.g.:
|
||||
sendmailCmd.Flags().BoolVar(&help, "test", false, "Help message for sendmail")
|
||||
sendmailCmd.Flags().BoolVarP(&inline, "inline", "i", false, "Inline css")
|
||||
sendmailCmd.Flags().StringVarP(&template, "template", "t", "", "Path to html template")
|
||||
sendmailCmd.Flags().StringVarP(&output, "output", "o", "", "Path to save inlined html")
|
||||
}
|
40
cmd/utils.go
Normal file
40
cmd/utils.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
func findSrcInHtml(doc string) []string {
|
||||
var results []string
|
||||
reader := strings.NewReader(doc)
|
||||
if document, err := html.Parse(reader); err == nil {
|
||||
|
||||
var parser func(*html.Node)
|
||||
parser = func(n *html.Node) {
|
||||
if n.Type == html.ElementNode && n.Data == "img" {
|
||||
|
||||
var imgSrcUrl string
|
||||
|
||||
for _, element := range n.Attr {
|
||||
if element.Key == "src" {
|
||||
imgSrcUrl = element.Val
|
||||
log.Print("found image:" + imgSrcUrl)
|
||||
results = append(results, imgSrcUrl)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||
parser(c)
|
||||
}
|
||||
|
||||
}
|
||||
parser(document)
|
||||
} else {
|
||||
log.Panicln("Parse html error", err)
|
||||
}
|
||||
return results
|
||||
}
|
Reference in New Issue
Block a user