0

I have an application that needs to run as root for access to hardware on the Raspberry Pi. In this application I need to send emails with attachments containing data.

I am able to write a bash script that successfully sends emails using mutt. In a quick test using C++ code I am able to call this script and it runs successfully.

However, when I integrate this into my larger project and I am forced to run as root it fails to send. I then noticed that if I ran the script by itself with sudo it also fails.

I assume the config for the root user is missing and the config used ordinarily isn't being used with sudo. The pi is at default settings in terms of configuration of users. I have tried to place my 'muttrc' file that I edited with my configuration details in /etc/, /etc/Mutt, /etc/mutt and /etc/.mutt and none of these worked.

edit:

Here's the email script

    #!/bin/bash  
    DATAFILE=$1  
    RECIPIENT=$2  
    echo "Sending email with data..."  
    echo "Recipient: " $RECIPIENT  
    echo "Attached file: " $DATAFILE  
    echo "" | mutt -s "Update" -i /home/pi/msgbody.txt $RECIPIENT -a $DATAFILE

When provided with arguments and called directly from the terminal without sudo, this script functions as intended. With sudo it doesn't work. I am inclined to believe that mutt configuration is the root (pun intended) cause of the problem, and not the C code.

Here's the output from ls -l for permissions (not sure if useful)

-rwxr-xr-x  1 pi   pi    222 Mar 19 21:51 emailscript

I believe the default user is pi

C++ Code (works without sudo):

void sendFile()
{
    string command = "/home/pi/emailscript " + lastFile + " " + emailAddress;
    system(command.c_str());
}

int main()
{

    cout << "Testing file sending function" << endl;
    sendFile();

    return 0;

}

Where filename and email address are initialised above as global variables. This too works when run without sudo, but ceases to when run with.

ls -l output for this code:

-rwxr-xr-x 1 pi pi 68540 Mar 19 22:49 emailTestCode

The main project is one process, as far as I'm aware. Its around 400 lines of code and makes use of a couple libraries. Permissions are exactly the same in main project as the test code given above.

edit2: (C++ not C)

Running the C++ code as root results in the expected echos going to std out (not included here as its just my email address and a filename ( absolute path is given to the file))

Std error:

/home/pi/emailscript: line 8:  1911 Done                    echo ""
      1912 Alarm clock             | mutt -s "Update" -i /home/pi/msgbody.txt $RECIPIENT -a $DATAFILE
2
  • 1
    Can you post the script, the permission bits of the script, the user / group of the script, the sample C code. Also how large is your main project and is it one or multiple processes? Who is the user+group that owns that? Also can you pipe stdout and stderr to a file and provide it Mar 19, 2019 at 23:27
  • @ChrisMaggiulli Updated Mar 19, 2019 at 23:58

2 Answers 2

0

I received some advice that fixed it for me, some may see this as a workaround.

I edited the command to force the script to run as the user Pi:

void sendFile()
{
    string command = "su pi -c \"/home/pi/emailscript " + lastFile + " " + emailAddress + "\"";
    system(command.c_str());
}

The "proper" solution might be to include the mutt configuration in /root, but this didn't seem to work me.

0

I don't know if you still have this problem, but this solved it for me:

sudo sh -c "echo >> /etc/Muttrc && cat /home/pi/.muttrc >> /etc/Muttrc"

When I ran into mutt not working with sudo, I tried to find errors, didn't find any. Not in /home/pi/.muttdebug0 anyway, turned out I had to look into /root/.muttdebug0, but no errors there either, not even with "-d 2". Just a lack of information when compared to /home/pi/.muttdebug0, it was almost like mutt didn't even try to send emails when executed with sudo.

When Googling, I came across this page and thought "include the mutt configuration in /root" might be the trick but you said it didn't work, I might know why.

Log file /root/.muttdebug0 didn't show any errors, but it did show this: "Reading configuration file '/etc/Muttrc'."

Unlike the config filename in /home/pi, that config filename does not start with a . , maybe that's why it didn't work for you? That you might not have known the filename is different, oh and don't forget the uppercase M ;)

You must log in to answer this question.

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