2

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.

Community
  • 1
  • 1
Garvit Jain
  • 1,862
  • 2
  • 19
  • 27

2 Answers2

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.)

Community
  • 1
  • 1
kostix
  • 51,517
  • 14
  • 93
  • 176
  • JFTR [the RFC on MDAs](https://tools.ietf.org/html/rfc6409) contains no mention of the `Bcc` header field so I think it's purely an MTA thing. – kostix Apr 23 '18 at 10:51
  • so maybe any other library ? or should I loop the contacts (will it be spam/ junk?) – Garvit Jain Apr 23 '18 at 16:12
  • 1
    If you opt to talk to a locally running Exim instance via its SMTP or main submission (587) port, the problem goes away completely as you're free to issue any (sane) number of the `RCPT TO` SMTP commands during the single message submission session. Looks like making yourself a bit more aware about how e-mail works would not hurt you. – kostix Apr 23 '18 at 16:28
  • BTW, have you filed an issue with gomail? – kostix Apr 23 '18 at 16:29
  • To make my previous commit more clear, I *think* that's exactly what `gomail` does with the contents of the `Bcc` header field when it's present *and when* it's talking to an MTA/MDA via the SMTP protocol (as opposed to passing the message to the stdin of `/usr/sbin/sendmail -t` or the like): it takes all the addresses from the `Bcc` field and generates an `RCPT TO` command for each of them. – kostix Apr 23 '18 at 16:31
  • I've opened a issue on gomail repo https://github.com/go-gomail/gomail/issues/115 . How do I **opt to talk to a locally running Exim instance via its SMTP** I've tried `d := gomail.NewDialer("localhost", 587, "", "")` with error ___panic: gomail: could not send email 1: 550 Verification failed for Unrouteable address Sender verify failed___ while using `d.TLSConfig = &tls.Config{InsecureSkipVerify: true}` – Garvit Jain Apr 24 '18 at 07:33
  • Or should I custom implement `gomsg.WriteTo()` that sends data to `exim -t` ? – Garvit Jain Apr 24 '18 at 07:46
  • That `550 Verification failed for … Unrouteable address Sender verify failed` (may) have nothing to do with TLS. It means Exim failed to verify your `From` address *for some reason.* What exactly reason depends on how Exim is configured. You might get a hunch on this by enabling debugging output for Exim, retry mail submission and studying Exim log afterwards. All in all, this is outside of the scope of this discussion. As I've already suggested elsewhere, try asking about configuring Exim over there at https://superuser.com – kostix Apr 24 '18 at 10:29
  • Thank you for your time; I've asked it on SuperUser as you have advised : https://superuser.com/q/1316842/555998 – Garvit Jain Apr 24 '18 at 16:47
-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.

.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182