Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QProcess && send mail from Linux
Forum Update on Monday, May 27th 2025

QProcess && send mail from Linux

Scheduled Pinned Locked Moved General and Desktop
15 Posts 6 Posters 9.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    luca
    wrote on 28 Feb 2011, 16:43 last edited by
    #1

    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?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Alicemirror
      wrote on 28 Feb 2011, 16:57 last edited by
      #2

      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

      Enrico Miglino (aka Alicemirror)
      Balearic Dynamics
      Islas Baleares, Ibiza (Spain)
      www.balearicdynamics.com

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Alicemirror
        wrote on 28 Feb 2011, 17:04 last edited by
        #3

        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 ?

        Enrico Miglino (aka Alicemirror)
        Balearic Dynamics
        Islas Baleares, Ibiza (Spain)
        www.balearicdynamics.com

        1 Reply Last reply
        0
        • B Offline
          B Offline
          baysmith
          wrote on 28 Feb 2011, 17:42 last edited by
          #4

          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;;
          

          }
          @

          Nokia Certified Qt Specialist.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            luca
            wrote on 28 Feb 2011, 17:47 last edited by
            #5

            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.

            1 Reply Last reply
            0
            • L Offline
              L Offline
              luca
              wrote on 28 Feb 2011, 17:56 last edited by
              #6

              [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.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Alicemirror
                wrote on 28 Feb 2011, 18:03 last edited by
                #7

                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...

                Enrico Miglino (aka Alicemirror)
                Balearic Dynamics
                Islas Baleares, Ibiza (Spain)
                www.balearicdynamics.com

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  tobias.hunger
                  wrote on 28 Feb 2011, 20:02 last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on 28 Feb 2011, 21:14 last edited by
                    #9

                    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.

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      luca
                      wrote on 1 Mar 2011, 07:26 last edited by
                      #10

                      [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(....);
                      @

                      1 Reply Last reply
                      0
                      • L Offline
                        L Offline
                        luca
                        wrote on 1 Mar 2011, 07:37 last edited by
                        #11

                        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.

                        1 Reply Last reply
                        0
                        • L Offline
                          L Offline
                          luca
                          wrote on 1 Mar 2011, 09:51 last edited by
                          #12

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

                          Thanks all!

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            goetz
                            wrote on 1 Mar 2011, 12:29 last edited by
                            #13

                            [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?

                            http://www.catb.org/~esr/faqs/smart-questions.html

                            1 Reply Last reply
                            0
                            • L Offline
                              L Offline
                              luca
                              wrote on 1 Mar 2011, 13:17 last edited by
                              #14

                              [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.

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                actionking
                                wrote on 8 Jul 2013, 13:33 last edited by
                                #15

                                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 function QxtSmtp::~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 function main':
                                /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 to QxtMailMessage::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 to QxtMailMessage::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 to QxtSmtp::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 to QxtSmtp::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 to QxtSmtp::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 to QxtMailMessage::~QxtMailMessage()'
                                :-1: error: collect2: ld returned 1 exit status...
                                @
                                could you please tell me,What kind of error is this.....?.......

                                1 Reply Last reply
                                0

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved