问题
I'm currently using "gopkg.in/gomail.v2" to send email with following code found here
package main
import (
"bytes"
"fmt"
"gopkg.in/gomail.v2"
)
func main() {
m := gomail.NewMessage()
m.SetHeader("From", "me@example.com")
m.SetHeader("To", "you@example.com")
m.SetAddressHeader("Bcc", "youbcc@example.com", "Bcc Email")
m.SetHeader("Bcc", "anotherbcc@example.com", "yetbcc@example.com")
m.SetHeader("Reply-To", "me@example.com")
m.SetHeader("Subject", "Subject is to gomail to be discussed")
body := `Why is bcc not getting populated`
m.SetBody("text/html", body)
sendMailViaExim(m)
}
func sendMailViaExim(m *gomail.Message) (err error) {
cmd := exec.Command("/usr/sbin/exim", "-t")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
pw, err := cmd.StdinPipe()
if err != nil {
log.Println(err)
return
}
err = cmd.Start()
if err != nil {
log.Println(err)
return
}
var errs [3]error
_, errs[0] = m.WriteTo(pw)
errs[1] = pw.Close()
errs[2] = cmd.Wait()
for _, err = range errs {
if err != nil {
log.Println(err)
return
}
}
return
}
The Above code doesn't send to Bcc emails;
So I logged the info that was being passed to exim -t
via following code:
buf := new(bytes.Buffer)
m.WriteTo(buf) //should write entire msg with bcc?
fmt.Println(buf.String())
and the console logged :
$ go run main.go
Mime-Version: 1.0
Date: Mon, 23 Apr 2018 11:15:54 +0530
To: you@example.com
Reply-To: me@example.com
Subject: Subject is to gomail to be discussed
From: me@example.com
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Why is bcc not getting populated
bcc is not getting to the exim -t
How do I achieve that without using smtp.
回答1:
I think it's this (and here is the changelog entry for it).
Still, the logic fixed by the commit above should only apply to actually
sending messages—that is, when the package actually speaks with an SMTP
server (because, naturally, Bcc
should not be forwarded past the first MTA processing the message (maybe even the first MDA—I'm lazy to read the RFC)).
The commit being discussed refers to
this part of a relevant RFC which actually offers three different ways to handle Bcc
header
fields.
So I have no idea whether to actually blame the package author for
this change or not.
Yet still, this looks like stripping the Bcc
header away even for the Message.WriteTo
method, which, IMO, is a mistake.
The reason I think this is a mistake is as follows:
- When the package sends a message via SMTP it works as an MTA or an MDA and should obey the rules defined by the relevant RFCs for these types of mail agents.
- When it merely streams the message to an MDA (Exim in your case),
it works as a pure client, and processing of the
Bcc
header fields is clearly not its business—it rather should be delegated to the first "real" mail processing agent.
The only relevant issue/pull-request I was able to find is this but it looks like the code was highly refactored after its inclusion so I have no idea whether it introduced what I supposed is a bug or not.
See also this.
TL;DR
I would file an issue in the package's bug tracker.
(If you will do that, please link it there; thanks.)
回答2:
The problem is caused by the -t
option you pass to exim. From the docs:
7. The Bcc: header line
If Exim is called with the -t option, to take recipient addresses from a message’s header, it removes any Bcc: header line that may exist (after extracting its addresses). If -t is not present on the command line, any existing Bcc: is not removed.
.
来源:https://stackoverflow.com/questions/49974478/send-emails-in-go-through-exim-without-smtp