Change 'from' address in Linux mail command

Q How do I change the 'from' address when using the Linux mail command? It insists on marking mail as from user@user-laptop (user-laptop is my hostname).

A This is not possible with the standard mail command without fiddling with the USER and HOSTNAME environment variables, which may have unwelcome side-effects on other programs running in the same shell. However, there are a number of alternative commands that will do what you want. Mutt is able to read the 'from' address from the EMAIL environment variable. This is worth knowing if you already have Mutt installed, but it is a lot more than you need if you only want to send out messages. A small alternative is SMTPClient (www.engelschall.com/sw/smtpclient), which is similar to mail in operation but accepts the --from argument to set the 'from' address. SMTPClient only passes your mail to a suitable SMTP server and defaults to localhost. If you want to use a different server, you will need to specify it with the --smtp-host command line option, or set the SMTPSERVER environment variable.

echo "Hello world - what else?" | smtpclient --smtp-host=my.mail.server
--from=hubris@wherever --subject "Hello World" someone@someplace

Follow us on Identi.ca or Twitter

Username:   Password:
Create Account | About TuxRadar