QProcess && send mail from Linux
-
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();
}
@ -
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();
}
@
[/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...
-
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 tovtable 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 tovtable for QxtSmtp' /home/bts-007/Desktop/Qprocess/main.o:-1: In function
main':
/home/bts-007/Desktop/Qprocess/main.cpp:-1: error: undefined reference toQxtMailMessage::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 toQxtMailMessage::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 toQxtMailMessage::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 toQxtSmtp::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 toQxtSmtp::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 toQxtMailMessage::~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.....?.......