Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "smtp.h"
- #include <QtGui>
- #include <QtNetwork/QSslSocket>
- smtp::smtp(QObject *parent) :
- QObject(parent)
- {
- conn = 0;
- m_user = "";
- m_password = "";
- server = "";
- port = 25;
- SSL = false;
- sending = false;
- }
- void smtp::setUser(QString user, QString password){
- m_user = user;
- m_password = password;
- }
- void smtp::configureServer(QString server, int port=25, bool useSSL = false){
- this->server = server;
- this->port = port;
- this->SSL = useSSL;
- }
- void smtp::setMessage(QString subject, QString text){
- this->subject = subject.replace("\r\n","\n").replace("\n","");
- this->body = text.replace("\r\n","\n");
- }
- smtp_error smtp::send(){
- if (server.isEmpty()){
- emit sentError(tr("Servidor não configurado"));
- return INVALID_SERVER;
- }
- else if (!recipients.count()){
- emit sentError(tr("Sem Destinatários"));
- return NO_RECIPIENTS;
- }
- if (!sending){
- qDebug() << "sending";
- QString rcpt;
- m_timeout.setSingleShot(true);
- m_timeout.setInterval(30000);
- connect(&m_timeout,SIGNAL(timeout()),this,SLOT(timeout()));
- //copying recipients to queue
- foreach(rcpt, recipients)
- recipientsCopy << rcpt;
- sending = true;
- if (conn) delete conn;
- conn = new QSslSocket(this);
- connect(conn,SIGNAL(readyRead()),this,SLOT(onRead()));
- connect(conn,SIGNAL(connected()),this,SLOT(onConnect()));
- connect(conn,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(onConnectError()));
- if (SSL){
- conn->connectToHostEncrypted(server,port);
- }
- else conn->connectToHost(server,port);
- }
- }
- void smtp::onRead(){
- qDebug() << "reading";
- while(conn->isReadable()){
- QByteArray buf;
- buf = conn->readLine();
- qDebug() << buf;
- if (!buf.length()) break;
- if (buf.contains("AUTH")){
- QByteArray a;
- //base64 auth plain
- a.append(m_user);
- a.append('\0');
- a.append(m_user);
- a.append('\0');
- a.append(m_password);
- QString coded = a.toBase64();
- conn->write(QString("AUTH PLAIN %1\r\n").arg(coded).toAscii());
- emit sendState(1);
- }
- else if (buf.startsWith("235")){ //auth accepted
- conn->write(QString("MAIL FROM: <%1>\r\n").arg(m_user).toAscii());
- }
- else if (buf.startsWith("250 2.1.0")){
- if (this->recipientsCopy.length()){
- QString coded;
- coded = recipientsCopy[0];
- recipientsCopy.removeFirst();
- conn->write(QString("RCPT TO: <%1>\r\n").arg(coded).toAscii());
- }
- emit sendState(2);
- }
- else if (buf.startsWith("250 2.1.5")){
- if (recipientsCopy.length()){
- QString coded;
- coded = recipientsCopy[0];
- recipientsCopy.removeFirst();
- conn->write(QString("RCPT TO: <%1>\r\n").arg(coded).toAscii());
- }
- else {
- conn->write("DATA\r\n");
- conn->flush();
- emit sendState(3);
- }
- }
- else if(buf.startsWith("535")){ //wrong password
- emit sentError(tr("Usuário Inválido."));
- conn->close();
- sending = false;
- disconnect(&m_timeout,SIGNAL(timeout()));
- }
- else if (buf.contains("354")) { //mail body
- /*from: <Nome_do_Remetente>
- cc: <destinatario_x1>
- bcc: <destinatario_x2>
- Date: dd mmm aaaa hh:mm:ss +0000
- subject: <Assunto>*/
- conn->write(QString(
- "From: %1 <%2>\r\n"
- "Cc: Ninguém <rockristao@gmail.com>\r\n"
- "Subject: %3\r\n\r\n")
- .arg(tr("Notificador de Status do Sistema"))
- .arg(m_user)
- .arg(subject)
- .toAscii());
- conn->write(body.toAscii());
- conn->write("\r\n.\r\n");
- conn->flush();
- emit sendState(4);
- }
- else if(buf.startsWith("250 2.0.0")){ // sent
- emit sendState(5);
- emit sent();
- conn->write("quit\r\n");
- conn->flush();
- conn->close();
- sending = false;
- disconnect(&m_timeout,SIGNAL(timeout()));
- }
- else if (buf.startsWith("553-5.1.2")){ // recipients erroneous
- emit sentError("O Servidor não reconhece um dos recipients");
- conn->close();
- sending = false;
- emit sent();
- disconnect(&m_timeout,SIGNAL(timeout()));
- }
- }
- }
- void smtp::onConnect()
- {
- conn->write("EHLO\r\n");
- emit sendState(0);
- }
- void smtp::onConnectError(){
- emit sentError(tr("Erro ao se conectar ao servidor."));
- sending = false;
- disconnect(&m_timeout,SIGNAL(timeout()));
- }
- void smtp::timeout(){
- if (sending){
- emit sentError(tr("O Servidor Demorou demais para responder."));
- sending = false;
- conn->close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement