19

How can I set sender name and email address using mail command in shell script.

1
  • 3
    By using commandline paramaters (try mail --help)
    – Anthon
    Commented May 4, 2015 at 6:43

3 Answers 3

21

The option -a populates the header.

quick command:

mail -a 'From: [email protected]' [email protected]

long command

mail --append='From: [email protected]' [email protected]

 Usage: mail [OPTION...] [address...]

  -a, --append=HEADER: VALUE     append given header to 
                                 the message being sent

  -A, --attach=FILE              attach FILE
      --content-type=TYPE        set content type for 
                                 subsequent --attach options

  -e, --exist                    return true if mail exists
      --encoding=NAME            set encoding for subsequent 
                                 --attach options

  -E, --exec=COMMAND             execute COMMAND

  -F, --byname                   save messages according to sender

  -H, --headers                  write a header summary and exit

  -i, --ignore                   ignore interrupts

  -n, --norc                     do not read the system mailrc file

  -N, --nosum                    do not display initial header summary

  -p, --print, --read            print all mail to standard output

  -q, --quit                     cause interrupts to terminate program

  -r, --return-address=ADDRESS   use address as the return address 
                                 when sending mail

  -s, --subject=SUBJ             send a message with the given SUBJECT

  -t, --to                       precede message by a list of addresses

  -u, --user=USER                operate on USER's mailbox

Common options

  --config-file=FILE, --rcfile=FILE
                         load this configuration file

  --config-help          show configuration file summary

  --config-lint, --rcfile-lint
                         check configuration file syntax and exit

  --config-verbose, --rcfile-verbose
                         verbosely log parsing of the configuration files

  --no-site-config, --no-site-rcfile
                         do not load site configuration file

  --no-user-config, --no-user-rcfile
                         do not load user configuration file

  --set=PARAM=VALUE      set configuration parameter

  --show-config-options  show compilation options

Global debugging settings

--debug-level=LEVEL    set Mailutils debugging level

--debug-line-info      show source info with debugging messages

-?, --help             give this help list
    --usage            give a short usage message

-V, --version          print program version

Mandatory or optional arguments to long options are also mandatory or optional for any corresponding short options.

18

Try this:

mail -s 'Some Subject' -r 'First Last <[email protected]>' [email protected]

This sets both From: and the envelope sender.

5
  • 2
    mail: invalid option -- 'r' :( Commented Jun 9, 2016 at 12:45
  • 2
    @DanielLoureiro Right, Heirloom mailx has -r, but it isn't required by POSIX. Still good enough for *BSD and Linux. :)
    – lcd047
    Commented Jun 9, 2016 at 14:10
  • works in centos 7 x64
    – Nick Dong
    Commented Feb 26, 2017 at 16:11
  • depends on the mail/mailx implementation in your distro; what I found it works is appending a header variable: --append="FROM:Foghorn Leghorn <[email protected]>" Commented May 21, 2018 at 23:30
  • @woohoo depends on the mail/mailx implementation in your distro - That of course applies equally to your solution. With my Heirloom mailx: mail: illegal option -- -.
    – lcd047
    Commented May 24, 2018 at 12:13
6

That depends which mail client you are using. Some Linux distributions link to mailx where you can use the -r from-addr parameter.

If you have mutt you should be able to use mutt -e "set from='name <name@somewhere>'>.

Other distributions which have the mail command should be able to use echo "test"|mail -s "subject" [email protected] -- -f from@address.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .