I am sending emails in one of my projects through command line. The project 
back end is written in Golang. I am using following code to send emails:



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 := ExecuteMailCommand("mail -s \"$(echo \" "+subject+" \nMIME-version: 
1.0;\nContent-Type: text/html;charset=\"UTF-8\";\n\")\"  "+request.EmailTo, 
body)
    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 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\n", err)
        return err
    }

    fmt.Printf("%s\n", stdout)
    return nil
}


But while sending email through this code, strange thing is happening. 
Sometimes the email is delivered immediately. But sometimes it is not 
delivered even upto somehours. The server is running on AWS.

I am not getting the reason why it is behaving like this. Do I need to set 
some server mail configurations or there is some problem in the code ?/

Thanks!

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to