1 /* Copyright (C) 2006 - 2014 Jan Kundrát <jkt@flaska.net>
2 Copyright (C) 2013 Pali Rohár <pali.rohar@gmail.com>
4 This file is part of the Trojita Qt IMAP e-mail client,
5 http://trojita.flaska.net/
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of
10 the License or (at your option) version 3 or any later version
11 accepted by the membership of KDE e.V. (or its successor approved
12 by the membership of KDE e.V.), which shall act as a proxy
13 defined in Section 14 of version 3 of the license.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 SMTP::SMTP(QObject
*parent
, const QString
&host
, quint16 port
, bool encryptedConnect
, bool startTls
, bool auth
,
30 AbstractMSA(parent
), host(host
), port(port
),
31 encryptedConnect(encryptedConnect
), startTls(startTls
), auth(auth
),
32 user(user
), failed(false), isWaitingForPassword(false), sendingMode(MODE_SMTP_INVALID
)
34 qwwSmtp
= new QwwSmtpClient(this);
35 // FIXME: handle SSL errors properly
36 connect(qwwSmtp
, &QwwSmtpClient::sslErrors
, qwwSmtp
, &QwwSmtpClient::ignoreSslErrors
);
37 connect(qwwSmtp
, &QwwSmtpClient::connected
, this, &AbstractMSA::sending
);
38 connect(qwwSmtp
, &QwwSmtpClient::done
, this, &SMTP::handleDone
);
39 connect(qwwSmtp
, &QwwSmtpClient::socketError
, this, &SMTP::handleError
);
44 qwwSmtp
->disconnectFromHost();
47 emit
error(tr("Sending of the message was cancelled"));
51 void SMTP::handleDone(bool ok
)
54 // This is a duplicate notification. The QwwSmtpClient is known to send contradicting results, see e.g. bug 321272.
61 if (qwwSmtp
->errorString().isEmpty())
62 emit
error(tr("Sending of the message failed."));
64 emit
error(tr("Sending of the message failed with the following error: %1").arg(qwwSmtp
->errorString()));
68 void SMTP::handleError(QAbstractSocket::SocketError err
, const QString
&msg
)
75 void SMTP::setPassword(const QString
&password
)
78 if (isWaitingForPassword
)
79 sendContinueGotPassword();
82 void SMTP::sendMail(const QByteArray
&from
, const QList
<QByteArray
> &to
, const QByteArray
&data
)
87 this->sendingMode
= MODE_SMTP_DATA
;
88 this->isWaitingForPassword
= true;
89 emit
progressMax(data
.size());
92 if (!auth
|| !pass
.isEmpty()) {
93 sendContinueGotPassword();
96 emit
passwordRequested(user
, host
);
99 void SMTP::sendContinueGotPassword()
101 isWaitingForPassword
= false;
102 if (encryptedConnect
)
103 qwwSmtp
->connectToHostEncrypted(host
, port
);
105 qwwSmtp
->connectToHost(host
, port
);
109 qwwSmtp
->authenticate(user
, pass
, QwwSmtpClient::AuthAny
);
110 emit
sending(); // FIXME: later
111 switch (sendingMode
) {
114 //RFC5321 specifies to prepend a period to lines starting with a period in section 4.5.2
115 if (data
.startsWith('.'))
117 data
.replace("\n.", "\n..");
118 qwwSmtp
->sendMail(from
, to
, data
);
122 qwwSmtp
->sendMailBurl(from
, to
, data
);
126 emit
error(tr("Unknown SMTP mode"));
129 qwwSmtp
->disconnectFromHost();
132 bool SMTP::supportsBurl() const
137 void SMTP::sendBurl(const QByteArray
&from
, const QList
<QByteArray
> &to
, const QByteArray
&imapUrl
)
141 this->data
= imapUrl
;
142 this->sendingMode
= MODE_SMTP_BURL
;
143 this->isWaitingForPassword
= true;
147 if (!auth
|| !pass
.isEmpty()) {
148 sendContinueGotPassword();
151 emit
passwordRequested(user
, host
);
154 SMTPFactory::SMTPFactory(const QString
&host
, quint16 port
, bool encryptedConnect
, bool startTls
,
155 bool auth
, const QString
&user
):
156 m_host(host
), m_port(port
), m_encryptedConnect(encryptedConnect
), m_startTls(startTls
),
157 m_auth(auth
), m_user(user
)
161 SMTPFactory::~SMTPFactory()
165 AbstractMSA
*SMTPFactory::create(QObject
*parent
) const
167 return new SMTP(parent
, m_host
, m_port
, m_encryptedConnect
, m_startTls
, m_auth
, m_user
);