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