Here is the code snippet to send the email via a local postfix server:
from := r.FormValue("from")
to := strings.Split(r.FormValue("to"), ";")
body := r.FormValue("body")
mime := "MIME-version:1.0;
Content-Type:text/html;charset=\"UTF-8\";
"
subject := fmt.Sprintf("Subject: %s
", r.FormValue("subject"))
msg := []byte(subject + mime + body)
err := smtp.SendMail("localhost:25", nil, from, to, msg)
The email was sent/received fine. However, it is missing the receipt address in the To field of received email. I also tried it on an exchange server. The receipt addresses are missing as well. Here is what it shows in the email source.
To: Undisclosed recipients:;
Any suggestions to fix it? thanks!
You're setting the values for the mail envelope, but you haven't put any headers in the email itself except for Subject:
. You should also be using as a newline for email.
A minimal example might look like:
headers := make(map[string]string)
headers["Subject"] = "this is a test"
headers["From"] = "me@example.com"
headers["To"] = "you@example.com"
body := "hello,
this is a test"
var msg bytes.Buffer
for k, v := range headers {
msg.WriteString(k + ": " + v + "
")
}
msg.WriteString("
")
msg.WriteString(body)
Some other helpful stdlib packages:
net/textproto
for MIME header handlingnet/mail
for address handling (though the package is really only for parsing email)