I am trying to write HTML with some CSS to send it in an email. The email is sent through Go command line execution. But its returning errors regarding CSS properties while sending email.
I am getting errors for properties like, background: rgb(255, 255, 255)
or padding
for some of its properties it returns "not found" error in terminal.
<div style="margin: 0; padding: 0; background: #ffffff; font-family: Montserrat, sans-serif; font-size: 16px; color: #313131">
Above is a chunk of html and css properties I am using.
Following is the code to send email through command line:
package utils
import(
"bytes"
"html/template"
"os/exec"
"fmt"
)
type EmailRequest struct{
EmailTo string
EmailSubject string
EmailBody string
}
func (request *EmailRequest) EmailSend(notificationTemplateData interface{}) (bool, error) {
subject, errParse := ParseTemplate(request.EmailSubject, notificationTemplateData)
body, errParse := ParseTemplate(request.EmailBody, notificationTemplateData)
if errParse != nil{
return false, errParse
}
err := ExecuteCommand("echo \""+body+" \" | mail -s \"$(echo \" "+subject+"
MIME-version: 1.0;
Content-Type: text/html;charset=\"UTF-8\";
\")\" "+request.EmailTo)
if err != nil {
return false, err
}
return true, nil
}
func ParseTemplate(templateHtml string, data interface{}) (string, error) {
var body string
t, err := template.New("test").Parse(templateHtml)
if err != nil {
return body, err
}
buf := new(bytes.Buffer)
if err = t.Execute(buf, data); err != nil {
return body, err
}
body = buf.String()
return body, nil
}
func ExecuteCommand(command string) error{
cmd := exec.Command("sh", "-c",command)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
fmt.Println(fmt.Sprint(err) + ": " + stderr.String())
return err
}
fmt.Println("Result: " + out.String())
return nil
}
I have an editor where a customer can configure its custom template. Obviously, the customer can add any CSS properties. So with random properties it will lead to an error.
What I have understood from the above error is that it throws errors for random CSS properties or is there any CSS rules to send email through go command line?
Edited:
Following is the Error that I am getting in terminal while running EmailSend() function.
sh:
padding: not found
sh:
background: not found
As Adrian mentioned in above comments, I was using go command itself to parse html and css properties. Following is a much simplified solution to send email through command line with html and css.
func (request *EmailRequest) EmailSend(notificationTemplateData interface{}) (bool, error) {
subject, errParse := ParseTemplate(request.EmailSubject, notificationTemplateData)
body, errParse := ParseTemplate(request.EmailBody, notificationTemplateData)
if errParse != nil{
return false, errParse
}
err := ExecuteMailCommand("mail -s \"$(echo \" "+subject+"
MIME-version: 1.0;
Content-Type: text/html;charset=\"UTF-8\";
\")\" "+request.EmailTo, body)
if err != nil {
return false, err
}
return true, nil
}
func ExecuteMailCommand(command string, body string) error {
cmd := exec.Command("sh", "-c", command)
cmd.Stdin = bytes.NewBufferString(body)
stdout, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("%v
", err)
return err
}
fmt.Printf("%s
", stdout)
return nil
}
In the above code the function ParseTemplate will remain the same.
Thanks!
You are getting errors because emails don't accept CSS properties. You need to use only properties that are valid for mailing clients.
You can find a pretty good list here
If I can give you a piece of advice from my own personal experience, you should use tables, they're the one containing the most accepted rules. You just have to style your cells to make your layout.
(Yes I know, this is tiring, but there's no other choice).