Results 1 to 7 of 7

Thread: Sending Mail through QProcess

  1. #1
    Join Date
    Feb 2006
    Location
    Jarrell, Texas, USA
    Posts
    70
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Sending Mail through QProcess

    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:

    Qt Code:
    1. QString sALERT = "Your fly is unzipped"
    2. QString sEMAIL = [email]someemail@some.dom[/email]
    3. QString sCMD = "/bin/echo \'" + sALERT + "\' | /bin/mail -s \" SMART ALERT \" " + sEMAIL;
    4. QProcess pCVT;
    5. pCVT.execute(sCMD);
    6. QString sMsg;
    7. sMsg.setNum(pCVT.exitCode());
    8. logger("... EMAIL exit code: " + sMsg);
    9. logger(sCMD);
    To copy to clipboard, switch view to plain text mode 
    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
    Last edited by jpn; 13th August 2008 at 14:50. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Sending Mail through QProcess

    You need a shell to interpret the command. See for example this post.
    J-P Nurmi

  3. #3
    Join Date
    Feb 2006
    Location
    Jarrell, Texas, USA
    Posts
    70
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Sending Mail through QProcess

    Thank you for the input.

    I have altered my code to reflect the following:

    Qt Code:
    1. QString sALERT = "Your shoe is untied";
    2. QString sEMAIL = someone@some.dom;
    3. QString sCMD = "/bin/sh";
    4. QStringList slCvtArgs;
    5. slCvtArgs.clear();
    6. slCvtArgs << "-c" << "\"/bin/echo" << "\'" + sALERT + "\'" << "|" << "/bin/mail" << "-s" << "\'SMART ALERT\'" << sEMAIL << "\"";
    7.  
    8. QString sMsg = sCMD;
    9. for (int iString = 0; iString < slCvtArgs.size(); iString++)
    10. sMsg = sMsg + " " + slCvtArgs.at(iString);
    11. logger("... Sending email using the following command:");
    12. logger("...... " + sMsg);
    13. QProcess pCVT;
    14. pCVT.execute(sCMD, slCvtArgs);
    15. if (pCVT.exitCode())
    16. {
    17. sMsg.setNum(pCVT.exitCode());
    18. logger("... EMAIL exit code: " + sMsg);
    19. }
    To copy to clipboard, switch view to plain text mode 

    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

  4. #4
    Join Date
    Feb 2006
    Location
    Jarrell, Texas, USA
    Posts
    70
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Sending Mail through QProcess

    I have attempted to alter the code using a "cat'ed" file rather than an echoed variable.
    The behavior is still the same.

    Qt Code:
    1. QString sCMD = "/bin/sh";
    2. QString sALERT = "Your shoe is untied.";
    3. QString sEMAIL = "someone@some.dom";
    4. QString sIP = "some.IP.address";
    5.  
    6. sMsg = "An ALERT Message was received from the Client with IP address " + sIP + ".\n\n"
    7. "Message: " + sALERT;
    8.  
    9. // Create file to write the message into.
    10. QString sAlertFile = sSMARTHOME + "/tmp/alert.txt";
    11. FILE *fAlertFile;
    12. // Open the alert file for write only.
    13. if ((fAlertFile = fopen(sAlertFile.toAscii(), "w")) == NULL)
    14. {
    15. logger(".... Unable to alert file " + sAlertFile + " for email.");
    16. }
    17. else
    18. {
    19. // Write the message and close the file
    20. fputs(sMsg.toAscii(), fAlertFile);
    21. fclose(fAlertFile);
    22. QStringLilst slCvtArgs;
    23. slCvtArgs.clear();
    24. slCvtArgs << "-c" << "\"/bin/cat" << "\'" + sAlertFile + "\'" << "|" << "/bin/mail" << "-s" << "\'SMART ALERT\'" << sEMAIL << "\"";
    25.  
    26. // Build a message to write to the log file.
    27. sMsg = sCMD;
    28. for (int iString = 0; iString < slCvtArgs.size(); iString++)
    29. sMsg = sMsg + " " + slCvtArgs.at(iString);
    30. // Write message to the log file.
    31. logger("... Sending email using the following command:");
    32. logger("...... " + sMsg);
    33.  
    34. // Now execute the mail commmand.
    35. QProcess pCVT;
    36. pCVT.execute(sCMD, slCvtArgs);
    37. if (pCVT.exitCode())
    38. {
    39. sMsg.setNum(pCVT.exitCode());
    40. logger("... EMAIL exit code: " + sMsg);
    41. }
    42. }
    To copy to clipboard, switch view to plain text mode 
    Karl

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Sending Mail through QProcess

    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.

  6. The following user says thank you to wysota for this useful post:

    KaptainKarl (13th August 2008)

  7. #6
    Join Date
    Feb 2006
    Location
    Jarrell, Texas, USA
    Posts
    70
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Sending Mail through QProcess

    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

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Sending Mail through QProcess

    You might have simply used system() you know...

Similar Threads

  1. Sending Mail with attachment files
    By joy in forum Qt Programming
    Replies: 2
    Last Post: 14th March 2008, 08:51
  2. sending encrypted data via mail using smtp
    By vermarajeev in forum General Programming
    Replies: 20
    Last Post: 14th August 2007, 19:47
  3. Sending a shortcut to an exec managed by QProcess ?
    By Ti_Thom in forum Qt Programming
    Replies: 1
    Last Post: 10th April 2007, 17:31
  4. Sending mail using Qt
    By munna in forum Qt Programming
    Replies: 3
    Last Post: 18th December 2006, 11:17
  5. qt network performance
    By criss in forum Qt Programming
    Replies: 16
    Last Post: 24th July 2006, 09:23

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.