File indexing completed on 2025-01-19 04:52:00

0001 /*
0002     Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsys.com>
0003 
0004     This library is free software; you can redistribute it and/or modify it
0005     under the terms of the GNU Library General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or (at your
0007     option) any later version.
0008 
0009     This library is distributed in the hope that it will be useful, but WITHOUT
0010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
0012     License for more details.
0013 
0014     You should have received a copy of the GNU Library General Public License
0015     along with this library; see the file COPYING.LIB.  If not, write to the
0016     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0017     02110-1301, USA.
0018 */
0019 #include "sinkutils.h"
0020 
0021 #include <QTemporaryFile>
0022 #include <sink/store.h>
0023 #include <sink/log.h>
0024 
0025 KAsync::Job<void> SinkUtils::sendMail(const QByteArray &messageContent, const QByteArray &accountId)
0026 {
0027     using namespace Sink;
0028     using namespace Sink::ApplicationDomain;
0029 
0030     SinkLog() << "Sending message via account: " << accountId;
0031     Query query;
0032     query.containsFilter<SinkResource::Capabilities>(ResourceCapabilities::Mail::transport);
0033     query.filter<SinkResource::Account>(accountId);
0034     return Store::fetchAll<SinkResource>(query)
0035         .then([=](const QList<SinkResource::Ptr> &resources) {
0036             if (!resources.isEmpty()) {
0037                 auto resourceId = resources[0]->identifier();
0038                 SinkLog() << "Sending message via resource: " << resourceId;
0039                 Mail mail(resourceId);
0040                 mail.setMimeMessage(messageContent);
0041                 return Store::create(mail)
0042                     .then<void>([=] {
0043                         //Trigger a sync, but don't wait for it.
0044                         Store::synchronize(Sink::SyncScope{}.resourceFilter(resourceId)).exec();
0045                     });
0046             }
0047             SinkWarning() << "Failed to find a mailtransport resource";
0048             return KAsync::error("Failed to find a MailTransport resource.");
0049         })
0050         .then([&] (const KAsync::Error &error) {
0051             if (error) {
0052                 QTemporaryFile tmp;
0053                 tmp.setAutoRemove(false);
0054                 if (tmp.open()) {
0055                     tmp.write(messageContent);
0056                     tmp.close();
0057                     SinkWarning() << "Saved your message contents to: " << tmp.fileName();
0058                 }
0059                 SinkError() << "Failed to send the message: " << error;
0060             } else {
0061                 SinkLog() << "Message was sent.";
0062             }
0063             return error;
0064         });
0065 }