I need to be able to send mail from a bash script with the following: To Address, From Address, Subject, Body Text, and File Attachment

I can send everything just perfectly using mutt EXCEPT, no matter what I do the "From" address does not get set correctly:

    e.g. mutt -e "my_hdr From:${FROM}")...

I can also use sendmail or mailx but I can't seem to get the file attachment and the body text correct.

    e.g. sendmail -t -a file
  • The body comes from stdin. Any attachments you need to encode yourself and add to the body. – Ignacio Vazquez-Abrams Jun 7 '18 at 17:45
  • For the sendmail approach, see unix.stackexchange.com/q/160200/4667 – glenn jackman Jun 7 '18 at 18:18
  • Some implementations of mail have a -a argument which can be used to attach files, e. g. mail -s "Subject goes here" -a /path/to/file.tgz somebody@example.com < /path/to/messagebody. – DopeGhoti Jun 7 '18 at 19:48

You can set the From: and other settings in a configuration file. The body must come from standard input.

For example:

cat msg.txt | mutt to@example.com -F ~/.mutt_settings -s "this is my subject" -a file.zip

For the settings file, copy an already working settings file and change the From line:

my_hdr From: Super Man <me@example.com>
  • Hello Juancho... you mention a configuration file but I am not sure what you mean. If you are talking about .muttrc I cannot edit this due to co. policy. Can you elaborate further? I am very new at this! Thanks! – SSDdude Jun 7 '18 at 19:40

There are two methods usually of building emails, over the system aka compatible sendmail interface or via PORT 25/TCP. No matter what programming language or scripting language you are using, you will find examples for both cases.

It is known that when using the compatible sendmail interface, i.e. directly via the system, and not via port 25, only root can change the From Field.

When building emails from PORT 25/TCP, you usually can pretty much build email body with any FROM: field as you wish.

Other option is setting it globally as @Juancho says for one server, but then you are limited to only one and one From:

The other option is sending them as root, which I do not advise.


This is the code required to override .muttrc from a script...

    echo "THIS IS THE BODY" | mutt -e "send-hook . \"my_hdr From: TechSupport <TechSupport@SSDdude.com>\"" -s "THIS IS THE SUBJECT" recipient@somewhere.com -a test.log


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.