PDA

View Full Version : Sending Mail through QProcess



KaptainKarl
13th August 2008, 15:48
QT: 4.3.3
OS: RHEL 5

I am attempting to send email by running the mail command from QProcess. Here is my code snippet:


QString sALERT = "Your fly is unzipped"
QString sEMAIL = someemail@some.dom
QString sCMD = "/bin/echo \'" + sALERT + "\' | /bin/mail -s \" SMART ALERT \" " + sEMAIL;
QProcess pCVT;
pCVT.execute(sCMD);
QString sMsg;
sMsg.setNum(pCVT.exitCode());
logger("... EMAIL exit code: " + sMsg);
logger(sCMD);

logger is a method that appends the current DateTime and the QString to a file.
The exit code comes back as 0.
If I copy and paste the command to a UNIX shell and run it, it works.
However, when I run the QT code, I get the outputs in the log file but no email.

Can anyone see anything obviously wrong?

Thanks,
Karl

jpn
13th August 2008, 15:52
You need a shell to interpret the command. See for example this post (http://www.qtcentre.org/forum/p-qprocess-and-spumux-post57472/postcount2.html).

KaptainKarl
13th August 2008, 16:40
Thank you for the input.

I have altered my code to reflect the following:



QString sALERT = "Your shoe is untied";
QString sEMAIL = someone@some.dom;
QString sCMD = "/bin/sh";
QStringList slCvtArgs;
slCvtArgs.clear();
slCvtArgs << "-c" << "\"/bin/echo" << "\'" + sALERT + "\'" << "|" << "/bin/mail" << "-s" << "\'SMART ALERT\'" << sEMAIL << "\"";

QString sMsg = sCMD;
for (int iString = 0; iString < slCvtArgs.size(); iString++)
sMsg = sMsg + " " + slCvtArgs.at(iString);
logger("... Sending email using the following command:");
logger("...... " + sMsg);
QProcess pCVT;
pCVT.execute(sCMD, slCvtArgs);
if (pCVT.exitCode())
{
sMsg.setNum(pCVT.exitCode());
logger("... EMAIL exit code: " + sMsg);
}


Exit code of QProcess is still 0.
I still do not receive the email.
I can still cut and paste from the log file and it works.

I appreciate any inputs.

Karl

KaptainKarl
13th August 2008, 17:52
I have attempted to alter the code using a "cat'ed" file rather than an echoed variable.
The behavior is still the same.



QString sCMD = "/bin/sh";
QString sALERT = "Your shoe is untied.";
QString sEMAIL = "someone@some.dom";
QString sIP = "some.IP.address";

sMsg = "An ALERT Message was received from the Client with IP address " + sIP + ".\n\n"
"Message: " + sALERT;

// Create file to write the message into.
QString sAlertFile = sSMARTHOME + "/tmp/alert.txt";
FILE *fAlertFile;
// Open the alert file for write only.
if ((fAlertFile = fopen(sAlertFile.toAscii(), "w")) == NULL)
{
logger(".... Unable to alert file " + sAlertFile + " for email.");
}
else
{
// Write the message and close the file
fputs(sMsg.toAscii(), fAlertFile);
fclose(fAlertFile);
QStringLilst slCvtArgs;
slCvtArgs.clear();
slCvtArgs << "-c" << "\"/bin/cat" << "\'" + sAlertFile + "\'" << "|" << "/bin/mail" << "-s" << "\'SMART ALERT\'" << sEMAIL << "\"";

// Build a message to write to the log file.
sMsg = sCMD;
for (int iString = 0; iString < slCvtArgs.size(); iString++)
sMsg = sMsg + " " + slCvtArgs.at(iString);
// Write message to the log file.
logger("... Sending email using the following command:");
logger("...... " + sMsg);

// Now execute the mail commmand.
QProcess pCVT;
pCVT.execute(sCMD, slCvtArgs);
if (pCVT.exitCode())
{
sMsg.setNum(pCVT.exitCode());
logger("... EMAIL exit code: " + sMsg);
}
}
Karl

wysota
13th August 2008, 19:32
It is all because QProcess can't interprete the pipe symbol. You are still passing arguments as if you were inside a shell and you're not. Either place the whole script in a file and call sh on the file using QProcess or run mail in a second QProcess and connect output of the first one to the input of the second one.

KaptainKarl
13th August 2008, 20:33
That solved it. I dropped the command into a shell script, set the permissions to executable and then called it with QProcess and it works.

I appreciate the help.

Karl

wysota
13th August 2008, 22:51
You might have simply used system() you know...