Skip to content

Commit

Permalink
Proper error handlying like when auth fails
Browse files Browse the repository at this point in the history
  • Loading branch information
dantti committed Dec 29, 2023
1 parent c92b21e commit 1b5dc7f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build*/
9 changes: 5 additions & 4 deletions demos/demo2/sendemail.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <QFileDialog>
#include <QErrorMessage>
#include <QSslError>
#include <QMessageBox>

#include "server.h"
Expand Down Expand Up @@ -66,10 +67,7 @@ void SendEmail::on_sendEmail_clicked()
message.addTo(to);
}

auto content = std::make_shared<MimeHtml>();
content->setHtml(ui->texteditor->toHtml());

message.addPart(content);
message.addPart(std::make_shared<MimeHtml>(ui->texteditor->toHtml()));

for (int i = 0; i < ui->attachments->count(); ++i) {
message.addPart(std::make_shared<MimeAttachment>(std::make_shared<QFile>(ui->attachments->item(i)->text())));
Expand Down Expand Up @@ -105,6 +103,9 @@ void SendEmail::sendMailAsync(const MimeMessage &msg)

if (!server) {
server = new Server(this);
connect(server, &Server::sslErrors, this, [](const QList<QSslError> &errors) {
qDebug() << "Server SSL errors" << errors.size();
});
server->setHost(host);
server->setPort(port);
server->setConnectionType(ct);
Expand Down
27 changes: 21 additions & 6 deletions src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,22 +574,20 @@ bool ServerPrivate::parseResponseCode(int expectedCode, Server::SmtpError defaul
const int responseCode = responseText.left(3).toInt();

if (responseCode / 100 == 4) {
// lastError = QString::fromLatin1(responseText);
Q_EMIT q->smtpError(Server::ServerError, QString::fromLatin1(responseText));
failConnection(Server::ServerError, responseCode, QString::fromLatin1(responseText));
return false;
}

if (responseCode / 100 == 5) {
// lastError = QString::fromLatin1(responseText);
Q_EMIT q->smtpError(Server::ClientError, QString::fromLatin1(responseText));
failConnection(Server::ClientError, responseCode, QString::fromLatin1(responseText));
return false;
}

if (responseText[3] == ' ') {
if (responseCode != expectedCode) {
const QString lastError = QString::fromLatin1(responseText);
qCWarning(SIMPLEMAIL_SERVER) << "Unexpected server response" << lastError << expectedCode;
Q_EMIT q->smtpError(defaultError, lastError);
failConnection(defaultError, responseCode, lastError);
return false;
}
if (responseMessage) {
Expand All @@ -600,7 +598,7 @@ bool ServerPrivate::parseResponseCode(int expectedCode, Server::SmtpError defaul

const QString lastError = QString::fromLatin1(responseText);
qCWarning(SIMPLEMAIL_SERVER) << "Unexpected server response" << lastError << expectedCode;
Q_EMIT q->smtpError(defaultError, lastError);
failConnection(defaultError, responseCode, lastError);
return false;
}

Expand Down Expand Up @@ -678,4 +676,21 @@ void ServerPrivate::commandQuit()
socket->write(QByteArrayLiteral("QUIT\r\n"));
}

void ServerPrivate::failConnection(Server::SmtpError defaultError, int responseCode, const QString &error)
{
Q_Q(Server);

qCDebug(SIMPLEMAIL_SERVER) << "failConnection" << defaultError << responseCode << error;
// Call this when the connection should be closed due an error
for (auto &mail : queue) {
ServerReply *reply = mail.reply;
reply->finish(true, responseCode, error);
}
queue.clear();

socket->close();

Q_EMIT q->smtpError(defaultError, error);
}

#include "moc_server.cpp"
1 change: 1 addition & 0 deletions src/server_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class ServerPrivate
inline void commandReset();
inline void commandNoop();
inline void commandQuit();
void failConnection(Server::SmtpError defaultError, int responseCode, const QString &error);

QList<ServerReplyContainer> queue;
Server *q_ptr;
Expand Down

0 comments on commit 1b5dc7f

Please sign in to comment.