How to send mail from Linux with To, From, Subject, and File Attachment

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












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






share|improve this question





















  • The body comes from stdin. Any attachments you need to encode yourself and add to the body.
    – Ignacio Vazquez-Abrams
    Jun 7 at 17:45










  • For the sendmail approach, see unix.stackexchange.com/q/160200/4667
    – glenn jackman
    Jun 7 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 at 19:48














up vote
0
down vote

favorite












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






share|improve this question





















  • The body comes from stdin. Any attachments you need to encode yourself and add to the body.
    – Ignacio Vazquez-Abrams
    Jun 7 at 17:45










  • For the sendmail approach, see unix.stackexchange.com/q/160200/4667
    – glenn jackman
    Jun 7 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 at 19:48












up vote
0
down vote

favorite









up vote
0
down vote

favorite











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






share|improve this question













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








share|improve this question












share|improve this question




share|improve this question








edited Jun 7 at 18:15









Jeff Schaller

30.9k846105




30.9k846105









asked Jun 7 at 17:44









SSDdude

544




544











  • The body comes from stdin. Any attachments you need to encode yourself and add to the body.
    – Ignacio Vazquez-Abrams
    Jun 7 at 17:45










  • For the sendmail approach, see unix.stackexchange.com/q/160200/4667
    – glenn jackman
    Jun 7 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 at 19:48
















  • The body comes from stdin. Any attachments you need to encode yourself and add to the body.
    – Ignacio Vazquez-Abrams
    Jun 7 at 17:45










  • For the sendmail approach, see unix.stackexchange.com/q/160200/4667
    – glenn jackman
    Jun 7 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 at 19:48















The body comes from stdin. Any attachments you need to encode yourself and add to the body.
– Ignacio Vazquez-Abrams
Jun 7 at 17:45




The body comes from stdin. Any attachments you need to encode yourself and add to the body.
– Ignacio Vazquez-Abrams
Jun 7 at 17:45












For the sendmail approach, see unix.stackexchange.com/q/160200/4667
– glenn jackman
Jun 7 at 18:18




For the sendmail approach, see unix.stackexchange.com/q/160200/4667
– glenn jackman
Jun 7 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 at 19:48




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 at 19:48










3 Answers
3






active

oldest

votes

















up vote
0
down vote













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>





share|improve this answer





















  • 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 at 19:40

















up vote
0
down vote













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.






share|improve this answer




























    up vote
    0
    down vote













    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


    THANKS!






    share|improve this answer





















      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );








       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f448479%2fhow-to-send-mail-from-linux-with-to-from-subject-and-file-attachment%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      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>





      share|improve this answer





















      • 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 at 19:40














      up vote
      0
      down vote













      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>





      share|improve this answer





















      • 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 at 19:40












      up vote
      0
      down vote










      up vote
      0
      down vote









      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>





      share|improve this answer













      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>






      share|improve this answer













      share|improve this answer



      share|improve this answer











      answered Jun 7 at 18:17









      Juancho

      51625




      51625











      • 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 at 19:40
















      • 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 at 19:40















      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 at 19:40




      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 at 19:40












      up vote
      0
      down vote













      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.






      share|improve this answer

























        up vote
        0
        down vote













        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.






        share|improve this answer























          up vote
          0
          down vote










          up vote
          0
          down vote









          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.






          share|improve this answer













          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.







          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered Jun 7 at 18:19









          Rui F Ribeiro

          34.4k1268113




          34.4k1268113




















              up vote
              0
              down vote













              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


              THANKS!






              share|improve this answer

























                up vote
                0
                down vote













                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


                THANKS!






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  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


                  THANKS!






                  share|improve this answer













                  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


                  THANKS!







                  share|improve this answer













                  share|improve this answer



                  share|improve this answer











                  answered Jun 7 at 21:09









                  SSDdude

                  544




                  544






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f448479%2fhow-to-send-mail-from-linux-with-to-from-subject-and-file-attachment%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      Peggy Mitchell

                      Palaiologos

                      The Forum (Inglewood, California)