11#include "ksmtp_debug.h"
12#include "serverresponse_p.h"
14#include <KLocalizedString>
18class SendJobPrivate :
public JobPrivate
28 SendJobPrivate(SendJob *job, Session *session,
const QString &name)
29 : JobPrivate(session,
name)
36 void sendNextRecipient();
37 void addRecipients(
const QStringList &rcpts);
40 using MessagePart =
struct {
47 QStringList m_recipients;
50 QStringList m_recipientsCopy;
51 Status m_status = Idle;
58SendJob::SendJob(
Session *session)
59 :
Job(*new SendJobPrivate(this, session,
i18n(
"SendJob")))
69 d->m_returnPath = QStringLiteral(
"<%1>").arg(from.
mid(start + 1, end - start - 1));
71 d->m_returnPath = QStringLiteral(
"<%1>").arg(from);
90 d->addRecipients(bcc);
99 d->m_data.
replace(
"\r\n.",
"\r\n..");
102void SendJob::doStart()
108 setErrorText(
i18n(
"Could not send the message because either the sender or recipient field is missing or invalid"));
113 const int sizeLimit = session()->
sizeLimit();
114 if (sizeLimit > 0 &&
size() > sizeLimit) {
116 setErrorText(
i18n(
"Could not send the message because it exceeds the maximum allowed size of %1 bytes. (Message size: %2 bytes.)", sizeLimit,
size()));
121 d->m_status = SendJobPrivate::SendingReturnPath;
122 sendCommand(
"MAIL FROM:" + d->m_returnPath.toUtf8());
125void SendJob::handleResponse(
const ServerResponse &r)
132 switch (d->m_status) {
133 case SendJobPrivate::Idle:
137 case SendJobPrivate::SendingReturnPath:
141 d->m_status = SendJobPrivate::SendingRecipients;
142 d->sendNextRecipient();
146 case SendJobPrivate::SendingRecipients:
150 if (d->m_recipientsCopy.isEmpty()) {
152 d->m_status = SendJobPrivate::SendingData;
154 d->sendNextRecipient();
159 case SendJobPrivate::SendingData:
164 sendCommand(d->m_data);
165 sendCommand(
"\r\n.");
176void SendJobPrivate::sendNextRecipient()
178 const bool dsnSupport = m_session->allowsDsn() ? m_dsn :
false;
180 q->sendCommand(
"RCPT TO:<" + m_recipientsCopy.takeFirst().toUtf8() +
'>' + (dsnSupport ?
" NOTIFY=success,failure" :
""));
183void SendJobPrivate::addRecipients(
const QStringList &rcpts)
185 for (
const auto &rcpt : rcpts) {
186 if (rcpt.isEmpty()) {
190 const int start = rcpt.indexOf(QLatin1Char(
'<'));
192 const int end = qMax(
start, rcpt.indexOf(QLatin1Char(
'>'),
start));
193 m_recipients.push_back(rcpt.mid(
start + 1, end -
start - 1));
195 m_recipients.push_back(rcpt);
200bool SendJobPrivate::prepare()
202 if (m_data.isEmpty()) {
203 qCWarning(KSMTP_LOG) <<
"A message has to be set before starting a SendJob";
207 m_recipientsCopy = m_recipients;
209 if (m_recipients.isEmpty()) {
210 qCWarning(KSMTP_LOG) <<
"Message has no recipients";
221 return d->m_data.size();
230#include "moc_sendjob.cpp"
void setErrorText(const QString &errorText)
void setError(int errorCode)
void setCc(const QStringList &cc)
Add recipients.
void setBcc(const QStringList &bcc)
Add recipients.
void setData(const QByteArray &data)
Set the actual message data.
void setDeliveryStatusNotification(bool b)
Set Delivery Status Notification.
void setFrom(const QString &from)
Set the sender email address.
int size() const
Returns size of the encoded message data.
void setTo(const QStringList &to)
Add recipients.
int sizeLimit() const
Returns the maximum message size in bytes that the server accepts.
Q_SCRIPTABLE Q_NOREPLY void start()
QString i18n(const char *text, const TYPE &arg...)
QString name(StandardAction id)
const QList< QKeySequence > & end()
QByteArray & replace(QByteArrayView before, QByteArrayView after)
qsizetype indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const const
QString mid(qsizetype position, qsizetype n) const const