File indexing completed on 2025-01-05 04:49:34

0001 /*
0002     SPDX-FileCopyrightText: 2007 Volker Krause <vkrause@kde.org>
0003     SPDX-FileCopyrightText: 2007 KovoKs <kovoks@kovoks.nl>
0004 
0005     Based on KMail code by:
0006     SPDX-FileCopyrightText: 1996-1998 Stefan Taferner <taferner@kde.org>
0007 
0008     SPDX-License-Identifier: LGPL-2.0-or-later
0009 */
0010 
0011 #include "sendmailjob.h"
0012 #include "sendmailplugin_debug.h"
0013 #include <MailTransport/Transport>
0014 
0015 #include <KLocalizedString>
0016 #include <KShell>
0017 #include <QBuffer>
0018 
0019 using namespace MailTransport;
0020 
0021 SendmailJob::SendmailJob(Transport *transport, QObject *parent)
0022     : TransportJob(transport, parent)
0023     , mProcess(new QProcess(this))
0024 {
0025     connect(mProcess, &QProcess::finished, this, &SendmailJob::sendmailExited);
0026     connect(mProcess, &QProcess::errorOccurred, this, &SendmailJob::receivedError);
0027     connect(mProcess, &QProcess::readyReadStandardError, this, &SendmailJob::receivedStdErr);
0028 }
0029 
0030 SendmailJob::~SendmailJob() = default;
0031 
0032 void SendmailJob::doStart()
0033 {
0034     QStringList arguments = QStringList() << QStringLiteral("-i") << QStringLiteral("-f") << sender() << to() << cc() << bcc();
0035     if (!transport()->options().isEmpty()) {
0036         const QStringList arg = KShell::splitArgs(transport()->options().trimmed());
0037         arguments << arg;
0038     }
0039     qCDebug(MAILTRANSPORT_PLUGIN_LOG) << "Sendmail arguments " << arguments;
0040     mProcess->start(transport()->host(), arguments);
0041 
0042     if (!mProcess->waitForStarted()) {
0043         setError(UserDefinedError);
0044         setErrorText(i18n("Failed to execute mailer program %1", transport()->host()));
0045         emitResult();
0046     } else {
0047         mProcess->write(buffer()->readAll());
0048         mProcess->closeWriteChannel();
0049     }
0050 }
0051 
0052 void SendmailJob::sendmailExited(int exitCode, QProcess::ExitStatus exitStatus)
0053 {
0054     if (exitStatus != 0 || exitCode != 0) {
0055         setError(UserDefinedError);
0056         if (mLastError.isEmpty()) {
0057             setErrorText(i18n("Sendmail exited abnormally."));
0058         } else {
0059             setErrorText(i18n("Sendmail exited abnormally: %1", mLastError));
0060         }
0061     }
0062     emitResult();
0063 }
0064 
0065 void SendmailJob::receivedError()
0066 {
0067     mLastError += mProcess->errorString();
0068 }
0069 
0070 void SendmailJob::receivedStdErr()
0071 {
0072     mLastError += QLatin1StringView(mProcess->readAllStandardError());
0073 }
0074 
0075 bool SendmailJob::doKill()
0076 {
0077     delete mProcess;
0078     return true;
0079 }
0080 
0081 #include "moc_sendmailjob.cpp"