QProcess && send mail from Linux



  • Hi all,
    I need to send an email from my application running on Linux.
    The command I neet to execute is:
    @
    mail -s 'Alarm' lucaATmydomail.com < /tmp/text
    @

    If I execute this from console by hand it works fine.

    In my application I'm trying with this:
    @
    QProcess *process_mail = new QProcess(this);
    process_mail->start("mail -s 'Alarm' lucaATmydomail.com < /tmp/text");
    qDebug() << "waiting";
    process_mail->waitForFinished();
    qDebug() << "finished";
    @

    but it doesn't works and it never exit from the line:
    @
    process_mail->waitForFinished();
    @

    so it seems the process never end....

    What's wrong?



  • Luca,

    I'm also interested in this problem because in the near future I need something similar in an application. Please, can you be more clear ? The code is text while the cite is in form of code, is true ? What line never exit? What is the logic you have in mind?
    I will try to replicate this condition on my computer.

    Thks



  • Again...

    It is possible that the specific 'mail' call (you should check what this command do respect to linux processes) generate some other kind of signal from 'finished()'
    You can set the function call @process_mail->waitForFinished(); @ with a timeout, i.e. @process_mail->waitForFinished(5000); @ (= 5 secs)
    The function surely exits, and you can debug the conditions of the application before and after.

    A question: the mail is always sent ?



  • This doesn't solve your QProcess problem, but an easy alternative way to send mail via an SMTP server is to use the "libqxt":http://libqxt.org/ extension library for Qt.

    @
    #include <QtCore>
    #include <QxtNetwork/QxtSmtp>
    #include <QxtNetwork/QxtMailMessage>

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    QxtMailMessage message;
    message.setSender("From <fromATmail.com>");
    message.addRecipient("To <toATmail.com>");
    message.setSubject("Alarm");
    message.setBody("Alarm message.");
    
    QxtSmtp smtp;
    a.connect(&smtp, SIGNAL(mailSent(int)), SLOT(quit()));
    smtp.setUsername("from_user");
    smtp.setPassword("");
    smtp.connectToSecureHost("smtp.mail.com");
    smtp.send(message);
    
    return a.exec&#40;&#41;;
    

    }
    @



  • Ok I'll be more clear.

    To send mail from linux console I usually use this command:
    @
    mail -s 'Alarm' lucaATmydomail.com < /tmp/text
    @

    this command immediately return because this way it doesn't wait user input.

    While If I execute from command line this:
    @
    mail -s 'Alarm' lucaATmydomail.com
    @
    it wait user input from stdin so I can write the message and end with "." to return to console.

    Because of this I expect that QProcess immediately finish:
    @
    process_mail->start("mail -s 'Alarm' lucaATmydomail.com < /tmp/text");
    @
    like if I execute:
    @
    process_mail->start("touch /tmp/pippo");
    @

    but it doesn't finish and if I put this:
    @
    process_mail->waitForFinished(5000);
    @
    it return false after 5 sec.

    It seems like if it expect some input from stdin.



  • [quote author="Bradley" date="1298914945"]This doesn't solve your QProcess problem, but an easy alternative way to send mail via an SMTP server is to use the "libqxt":http://libqxt.org/ extension library for Qt.

    @
    #include <QtCore>
    #include <QxtNetwork/QxtSmtp>
    #include <QxtNetwork/QxtMailMessage>

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    QxtMailMessage message;
    message.setSender("From <fromATmail.com>");
    message.addRecipient("To <toATmail.com>");
    message.setSubject("Alarm");
    message.setBody("Alarm message.");
    
    QxtSmtp smtp;
    a.connect(&smtp, SIGNAL(mailSent(int)), SLOT(quit()));
    smtp.setUsername("from_user");
    smtp.setPassword("");
    smtp.connectToSecureHost("smtp.mail.com");
    smtp.send(message);
    
    return a.exec(&#41;;
    

    }
    @
    [/quote]

    This seems useful, tomorrow I'll try. Thanks.



  • This is only a supposition: consider the the console redirection I am not 100% sure that works under the QProcess call. Should be that is works only under the console, and I don't know exactly how does it works the QT call (is it exactly a shell ???)

    Why don't set a bash command, that works and from your QProcess try to call this? There is also the advantage that you can debug both the QT behaviour and what receive the command in the shell, that can redirect its debug messages to a file (redirect the sterr to a file, or create a log file) and see what does really happens. It can be useful for any use of the shell call. In other cases I solved the process call with a command...


  • Moderators

    Did you check the "documentation on QProcess":http://doc.qt.nokia.com/4.7/qprocess.html#start? Note the QStringList containing the command arguments.



  • And also have a look at the documentation about "Communicating via Channels":http://doc.qt.nokia.com/4.7/qprocess.html#communicating-via-channels. Shell redirection does not work, as there is no shell involved at all.



  • [quote author="Tobias Hunger" date="1298923360"]Did you check the "documentation on QProcess":http://doc.qt.nokia.com/4.7/qprocess.html#start? Note the QStringList containing the command arguments.[/quote]

    Yes, I checked it but I don't think it's a arguments problem because the documentation also report this:
    @
    QProcess process;
    process.start("del /s .txt");
    // same as process.start("del", QStringList() << "/s" << "
    .txt");
    @

    [quote author="Volker" date="1298927673"]And also have a look at the documentation about "Communicating via Channels":http://doc.qt.nokia.com/4.7/qprocess.html#communicating-via-channels. Shell redirection does not work, as there is no shell involved at all.[/quote]

    So I can't use:
    @
    .... < /tmp/text ...
    @
    ?

    mail command accept input from stdin so I'll try with:
    @
    process_mail->write(....);
    @



  • I tried this:
    @
    process_mail->start("mail -s 'Alarm ' lucaATmydomail.com");
    process_mail->waitForStarted();
    process_mail->write(QByteArray("TEXT"));
    process_mail->write(QByteArray(".\n"));
    process_mail->write(QByteArray("\n"));
    qDebug() << process_mail->waitForFinished(5000);
    @
    but the process never finish. I think the problem is in the end character of the input.
    Using mail from command line is like this:
    @
    root@star:~# mail -s my-subject luca@mydomain.com
    this is
    a
    very simple
    test
    .
    EOT
    @
    As you can see, to exit from stdin I write a "." character then press ENTER and it close stdin, write EOT to stdout and exit.



  • I finally solved with "Qxt":http://libqxt.org/ library.

    Thanks all!



  • [quote author="Luca" date="1298965057"]I tried this:
    @
    process_mail->start("mail -s 'Alarm ' lucaATmydomail.com");
    process_mail->waitForStarted();
    process_mail->write(QByteArray("TEXT"));
    process_mail->write(QByteArray(".\n"));
    process_mail->write(QByteArray("\n"));
    qDebug() << process_mail->waitForFinished(5000);
    @
    but the process never finish. I think the problem is in the end character of the input.
    Using mail from command line is like this:
    @
    root@star:~# mail -s my-subject luca@mydomain.com
    this is
    a
    very simple
    test
    .
    EOT
    @
    As you can see, to exit from stdin I write a "." character then press ENTER and it close stdin, write EOT to stdout and exit.[/quote]

    Did you try closeWriteChannel() once you're done with outputting the data?



  • [quote author="Volker" date="1298982546"]
    Did you try closeWriteChannel() once you're done with outputting the data?[/quote]

    ... as always you are right! :-D

    Now it works, Thanks!

    This is the code:
    @
    QProcess *process_mail;
    process_mail = new QProcess();
    process_mail->start("mail -s Alarm lucaATmydomain.com");
    process_mail->waitForStarted();
    process_mail->write(QByteArray("TEXT of the email"));
    process_mail->closeWriteChannel();
    @
    I hope this help someone.



  • hi Mr. Luca and Mr. Bradley,
    I have tried your code ,when i have used the header
    #include <QxtNetwork/QxtSmtp>
    #include <QxtNetwork/QxtMailMessage>.......
    It is showing as error....after going through the installed path..i have replaced it as
    #include<qxt/QxtNetwork/qxtsmtp.h>
    #include<qxt/QxtNetwork/qxtmailmessage.h>...error gone.....But another problem arises.......#include "qxtglobal.h"....Again i searched for the exact path and i changed the header into "qxt/QxtCore/qxtglobal.h"....Now the error gone.....
    After debugging every errors,,I thought i will get the output,but it shows new 15 errors,But when i clicked those errors,it shows the main.cpp is having error....
    These are the 15 errors
    @
    /home/bts-007/Desktop/Qprocess/main.cpp:-1: error: undefined reference to vtable for QxtSmtp' /home/bts-007/Desktop/Qprocess/main.o:-1: In functionQxtSmtp::~QxtSmtp()':
    /home/bts-007/Desktop/Qprocess/main.cpp:-1: error: undefined reference to vtable for QxtSmtp' /home/bts-007/Desktop/Qprocess/main.o:-1: In functionmain':
    /home/bts-007/Desktop/Qprocess/main.cpp:-1: error: undefined reference to QxtMailMessage::QxtMailMessage()' /home/bts-007/Desktop/Qprocess/main.cpp:-1: error: undefined reference toQxtMailMessage::setSender(QString const&)'
    /home/bts-007/Desktop/Qprocess/main.cpp:-1: error: undefined reference to QxtMailMessage::addRecipient(QString const&, QxtMailMessage::RecipientType)' /home/bts-007/Desktop/Qprocess/main.cpp:-1: error: undefined reference toQxtMailMessage::setSubject(QString const&)'
    /home/bts-007/Desktop/Qprocess/main.cpp:-1: error: undefined reference to QxtMailMessage::setBody(QString const&)' /home/bts-007/Desktop/Qprocess/main.cpp:-1: error: undefined reference toQxtSmtp::QxtSmtp(QObject*)'
    /home/bts-007/Desktop/Qprocess/main.cpp:-1: error: undefined reference to QxtSmtp::setUsername(QByteArray const&)' /home/bts-007/Desktop/Qprocess/main.cpp:-1: error: undefined reference toQxtSmtp::setPassword(QByteArray const&)'
    /home/bts-007/Desktop/Qprocess/main.cpp:-1: error: undefined reference to QxtSmtp::connectToHost(QString const&, unsigned short)' /home/bts-007/Desktop/Qprocess/main.cpp:-1: error: undefined reference toQxtSmtp::send(QxtMailMessage const&)'
    /home/bts-007/Desktop/Qprocess/main.cpp:-1: error: undefined reference to QxtMailMessage::~QxtMailMessage()' /home/bts-007/Desktop/Qprocess/main.cpp:-1: error: undefined reference toQxtMailMessage::~QxtMailMessage()'
    :-1: error: collect2: ld returned 1 exit status...
    @
    could you please tell me,What kind of error is this.....?.......


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.