22 #include <sys/utsname.h>
28 SMTP::SMTP(
char *serverhost,
unsigned short int port,
int timeout)
32 serverHost = serverhost;
34 timeOut = timeout * 1000;
36 senderAddress =
"user@example.net";
37 recipientAddress =
"user@example.net";
38 messageSubject =
"(no subject)";
39 messageBody =
"empty";
50 domainName = uts.nodename;
53 if(domainName.isEmpty())
54 domainName =
"somemachine.example.net";
56 kDebug() <<
"SMTP object created";
59 connect(&timeOutTimer, SIGNAL(timeout()),
this, SLOT(
connectTimedOut()));
76 serverHost = serverhost;
91 senderAddress = sender;
92 int index = senderAddress.indexOf(
'<');
95 senderAddress = senderAddress.mid(index + 1);
96 index = senderAddress.indexOf(
'>');
98 senderAddress = senderAddress.left(index);
99 senderAddress = senderAddress.simplified();
101 index = senderAddress.indexOf(
' ');
103 senderAddress = senderAddress.mid(index + 1);
107 index = senderAddress.indexOf(
'@');
109 senderAddress.append(
"@localhost");
115 recipientAddress = recipient;
120 messageSubject = subject;
125 messageBody = message;
130 messageHeader = header;
135 kDebug() <<
"started connect timer";
136 connectTimer.setSingleShot(
true);
137 connectTimer.start(100);
150 kDebug() <<
"state was == Finished\n";
153 writeString = QString::fromLatin1(
"helo %1\r\n").arg(domainName);
154 sock->write(writeString.toLatin1().constData(), writeString.length());
157 kDebug() <<
"enabling read on sock...\n";
158 interactTimer.setSingleShot(
true);
159 interactTimer.start(timeOut);
168 kDebug() <<
"connectTimerTick called...";
173 kDebug() <<
"connecting to " << serverHost <<
":" << hostPort <<
" ..... ";
182 connect(sock, SIGNAL(
error(QAbstractSocket::SocketError)),
this,
184 connect(sock, SIGNAL(disconnected()),
this, SLOT(
socketClosed()));
193 kDebug() <<
"socket connection timed out";
200 interactTimer.stop();
202 kDebug() <<
"time out waiting for server interaction";
211 kDebug() <<
"socketRead() called...";
212 interactTimer.stop();
221 lineBuffer += readBuffer;
222 nl = lineBuffer.indexOf(
'\n');
225 lastLine = lineBuffer.left(nl);
226 lineBuffer = lineBuffer.right(lineBuffer.length() - nl - 1);
229 interactTimer.setSingleShot(
true);
230 interactTimer.start(timeOut);
236 kDebug() << socketError << sock->errorString();
237 Q_UNUSED(socketError);
245 kDebug() <<
"connection terminated";
258 i = line->indexOf(
' ');
259 tmpstr = line->left(i);
261 kDebug() <<
"warning: SMTP status code longer than 3 digits: " << tmpstr;
262 stat = tmpstr.toInt();
266 kDebug() <<
"smtp state: [" << stat <<
"][" << *line <<
"]";
271 writeString = QString::fromLatin1(
"helo %1\r\n").arg(domainName);
272 kDebug() <<
"out: " << writeString;
273 sock->write(writeString.toLatin1().constData(), writeString.length());
282 writeString = QString::fromLatin1(
"mail from: %1\r\n").arg(senderAddress);
283 kDebug() <<
"out: " << writeString;
284 sock->write(writeString.toLatin1().constData(), writeString.length());
288 writeString = QString::fromLatin1(
"rcpt to: %1\r\n").arg(recipientAddress);
289 kDebug() <<
"out: " << writeString;
290 sock->write(writeString.toLatin1().constData(), writeString.length());
294 writeString = QLatin1String(
"data\r\n");
295 kDebug() <<
"out: " << writeString;
296 sock->write(writeString.toLatin1().constData(), writeString.length());
305 kDebug() <<
"smtp error (state error): [" << lastState <<
"]:[" << stat <<
"][" << *line <<
"]";
313 writeString = QString::fromLatin1(
"Subject: %1\r\n").arg(messageSubject);
314 writeString += messageHeader;
315 writeString +=
"\r\n";
316 writeString += messageBody;
317 writeString += QLatin1String(
".\r\n");
318 kDebug() <<
"out: " << writeString;
319 sock->write(writeString.toLatin1().constData(), writeString.length());
323 kDebug() <<
"smtp error (command error): [" << lastState <<
"]:[" << stat <<
"][" << *line <<
"]\n";
329 kDebug() <<
"smtp error (unknown user): [" << lastState <<
"]:[" << stat <<
"][" << *line <<
"]";
335 kDebug() <<
"unknown response: [" << lastState <<
"]:[" << stat <<
"][" << *line <<
"]";
void setSenderAddress(const QString &sender)
void setMessageSubject(const QString &subject)
int stat(const QString &path, KDE_struct_stat *buf)
void processLine(QString *line)
SMTP(char *serverhost=0, unsigned short int port=0, int timeout=DEFAULT_SMTP_TIMEOUT)
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
void setMessageHeader(const QString &header)
void setTimeOut(int timeout)
void setMessageBody(const QString &message)
QTcpSocket * connectToHost(const QString &protocol, const QString &host, quint16 port, QObject *parent=0)
void socketError(QAbstractSocket::SocketError)
void setServerHost(const QString &serverhost)
void setRecipientAddress(const QString &recipient)
void setPort(unsigned short int port)
#define SMTP_READ_BUFFER_SIZE