Mutt Bash E-mail Script

Send E-mail by Bash Script using Fedora, Cent OS and/or Redhat

This is my guide for “How to send an e-mail using a bash script including an attachment…. In order to send an e-mail with an attachment from a bash script you will need to install the program MUTT, you can use YUM or RPM to install the program.

STEP 1 To install MUTT using YUM or UP2DATE run the following at your command line:

yum install mutt (Fedora and Cent OS method)
up2date mutt (Redhat)

STEP 2 Once installed you can start writing the script. The following is a simple example of how the line should look:

mutt -s “SUBJECT” -a /DIRECTORY_PATH [email protected] < /home/username/mail-message.txt

-s is the subject, where “SUBJECT” should be tittled accordingly
-a is the attachement, where “/DIRECTORY_PATH” should be the path to the file you need to attatch
[email protected]” is the receiver, this is the person who will receive this e-mail
mail-message.txt is the file that is read into the message body in which the recipient will read

STEP 3 To put this all together in a script you would write the following:
(using my favorite text editor emacs I would type: emacs test_email.bsh, don’t forget to change the permissions, set them by typing: chmod +x test_email.bsh, if you don’t make the file executable it won’t run)

#!/bin/bash
mutt -s “this is a test” -a /home/username/log/test.log [email protected] < /home/username/mail-message.txt

The above script would send a message with a Subject of: “this is a test” and would include an attachment of the file: “test.log” and would be sent to” [email protected]

PLEASE NOTE that if you do not have MUTT installed you can send an e-mail using the following command, I am however unaware of a way to send an attachment using this method (this method uses sendmail which is usually installed by default on Linux systems):

echo “message body” | mail -s “subject” [email protected]

[ad#google-468×60]

7 thoughts on “Mutt Bash E-mail Script

  1. Brendan Skoreyko

    Hi Mariano,

    I think I understand but I want to clarify what exactly it is you are trying to do… I am assuming you want to send email to [email protected] and that you want to use mutt to read that email. Are you trying to get mutt to process the email automatically or just use mutt as a client like Microsoft Outlook? If you are trying to process the email automatically then I’d have to research that further (or try good old GOOGLE). Otherwise if you are trying to use it like a client then the type “mutt” at the command line and it will open up the mutt email interface.

    Please let me know if this is of use to you or if I have totally missed what you are asking of me.

    Thank you,
    Brendan Skoreyko

  2. Carsten

    Hi Brendan,

    I am getting all these munin cron job emails every 5 minutes. And I want to delete them once a week via cron job. Is it possible to delete hundreds of emails via command line?

    Greatpost, even when it’s old:)

    Thanks
    Carsten

  3. Brendan Skoreyko

    Hi Carsten,

    I think I understand but I want to be sure before giving you an answer. I am thinking that you have a script which is running and each time it runs it generates an email which gets sent to your inbox (locally depedning on your machines config). Is this correct?

    Thank you,
    Brendan

  4. Mariano Porcile

    Hi Brendan,
    I need to receive emails to addresses like this: [email protected], read from a program in bash and process.
    The number before the @ will change in each email.
    Need for SMS forwarding email content at the number before the @.
    You can help me?

    Thanks.

    Mariano.

Leave a Reply

Your email address will not be published. Required fields are marked *

*