doolo00026 2018-01-11 13:53
浏览 97
已采纳

在Go中从命令行发送电子邮件时编写CSS规则的HTML

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 
  • 写回答

2条回答 默认 最新

  • duande1146 2018-01-16 12:32
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计