9

I'm trying to send unix mail (heirloom-mailx) via bcc.

$ mail -V
12.5 6/20/10

My MTA is exim, which sends via Gmail SMTP.

I can execute the following:

echo -e "body" | mail -s 'subject' -b [email protected],[email protected] [email protected]

All addresses receive the email, however the bcc is not hidden. i.e. in the email to [email protected], I still see:

To: [email protected]
Bcc: [email protected],[email protected]

How can I make mail send bcc properly?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Sparhawk
  • 19,561
  • 18
  • 86
  • 152
  • I found this thread on SO: http://stackoverflow.com/questions/14521280/send-mail-from-linux-command-line-using-bcc-and-from-headers – slm Oct 22 '13 at 05:30
  • @slm Thanks. I did come across a few other posts complaining that `-b` didn't work, but they seemed to suggest that it wouldn't send at all, rather than the problem I have. However, if I don't get a response here, I'll check out some of the other mail programs. – Sparhawk Oct 22 '13 at 05:32
  • Could you name MTA server you use? [sendmail/postfix/exim/...] I think that many servers strip `Bcc:` headers. – AnFi Oct 22 '13 at 05:36
  • I'm using exim, which sends via gmail smtp. – Sparhawk Oct 22 '13 at 05:39

2 Answers2

7

heirloom-mailx hardcodes the options to pass to /usr/sbin/sendmail, and unfortunately the ones it passes don't make Bcc: headers work right in exim. That isn't really fixable, without patching and recompiling it. (You need to have it add -t to the arguments, and then either not pass the list of email addresses or alternatively set extract_addresses_remove_arguments = false in your exim config).

There is an easier solution: just have it send via SMTP. You do this by setting the smtp option, and since you've got a local MTA running, you should be able to set it to localhost. You can test like this:

$ mail -S smtp=localhost -s 'test message' -b [email protected] [email protected]
la la la
.

If that works, you can put the smtp option in /etc/nail.rc (system-wide) or ~/.mailrc (per-user). Note that nail.rc, with an n, is not a typo.

derobert
  • 107,579
  • 20
  • 231
  • 279
  • Oh! That was surprisingly straightforward. I previously set up gmail smtp with `sudo dpkg-reconfigure exim4-config` and modifying `/etc/exim4/passwd.client`. How should I revert these changes, and how do they relate to `/etc/nail.rc`? – Sparhawk Oct 23 '13 at 10:50
  • @Sparhawk There is no reason to revert your exim4-config changes. nail.rc just configures heirloom-mailx, and in this case you're just configuring it to speak to your local exim a different way (over a TCP connection to localhost vs. running /usr/sbin/sendmail) – derobert Oct 23 '13 at 10:52
5

If using GNU mailx from mailutils package, BCC and CC can be added by using the --append option to add header variables, like so:

$ echo -e "body text line1\nline2\n\nsent at: $(date)" | mailx --append='FROM:Foghorn Leghorn <[email protected]>' --append='BCC:[email protected]' -s "test subject" -- [email protected] [email protected] [email protected]

Also note how the above command uses the header variable for the FROM address.

Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227
scrat.squirrel
  • 303
  • 2
  • 7