Advertisement
Guest User

Untitled

a guest
Jul 11th, 2017
516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.24 KB | None | 0 0
  1. #include "smtp.h"
  2. #include <QtGui>
  3. #include <QtNetwork/QSslSocket>
  4.  
  5. smtp::smtp(QObject *parent) :
  6.     QObject(parent)
  7. {
  8.     conn = 0;
  9.     m_user = "";
  10.     m_password = "";
  11.     server = "";
  12.     port = 25;
  13.     SSL = false;
  14.     sending = false;
  15. }
  16. void smtp::setUser(QString user, QString password){
  17.     m_user = user;
  18.     m_password = password;
  19. }
  20.  
  21. void smtp::configureServer(QString server, int port=25, bool useSSL = false){
  22.     this->server = server;
  23.     this->port = port;
  24.     this->SSL = useSSL;
  25. }
  26.  
  27. void smtp::setMessage(QString subject, QString text){
  28.     this->subject = subject.replace("\r\n","\n").replace("\n","");
  29.     this->body = text.replace("\r\n","\n");
  30. }
  31.  
  32. smtp_error smtp::send(){
  33.     if (server.isEmpty()){
  34.         emit sentError(tr("Servidor não configurado"));
  35.         return INVALID_SERVER;
  36.     }
  37.     else if (!recipients.count()){
  38.         emit sentError(tr("Sem Destinatários"));
  39.         return NO_RECIPIENTS;
  40.     }
  41.     if (!sending){
  42.         qDebug() << "sending";
  43.         QString rcpt;
  44.         m_timeout.setSingleShot(true);
  45.         m_timeout.setInterval(30000);
  46.         connect(&m_timeout,SIGNAL(timeout()),this,SLOT(timeout()));
  47.         //copying recipients to queue
  48.         foreach(rcpt, recipients)
  49.             recipientsCopy << rcpt;
  50.         sending = true;
  51.         if (conn) delete conn;
  52.         conn = new QSslSocket(this);
  53.         connect(conn,SIGNAL(readyRead()),this,SLOT(onRead()));
  54.         connect(conn,SIGNAL(connected()),this,SLOT(onConnect()));
  55.         connect(conn,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(onConnectError()));
  56.         if (SSL){
  57.             conn->connectToHostEncrypted(server,port);
  58.         }
  59.         else conn->connectToHost(server,port);
  60.     }
  61.  
  62.  
  63.  
  64. }
  65. void smtp::onRead(){
  66.     qDebug() << "reading";
  67.     while(conn->isReadable()){
  68.         QByteArray buf;
  69.         buf = conn->readLine();
  70.         qDebug() << buf;
  71.         if (!buf.length()) break;
  72.  
  73.         if (buf.contains("AUTH")){
  74.            QByteArray a;
  75.  
  76.            //base64 auth plain
  77.            a.append(m_user);
  78.            a.append('\0');
  79.            a.append(m_user);
  80.            a.append('\0');
  81.            a.append(m_password);
  82.  
  83.            QString coded = a.toBase64();
  84.            conn->write(QString("AUTH PLAIN %1\r\n").arg(coded).toAscii());
  85.            emit sendState(1);
  86.        }
  87.        else if (buf.startsWith("235")){ //auth accepted
  88.  
  89.            conn->write(QString("MAIL FROM: <%1>\r\n").arg(m_user).toAscii());
  90.        }
  91.        else if (buf.startsWith("250 2.1.0")){
  92.  
  93.            if (this->recipientsCopy.length()){
  94.                QString coded;
  95.                coded = recipientsCopy[0];
  96.                recipientsCopy.removeFirst();
  97.                conn->write(QString("RCPT TO: <%1>\r\n").arg(coded).toAscii());
  98.            }
  99.            emit sendState(2);
  100.        }
  101.        else if (buf.startsWith("250 2.1.5")){
  102.            if (recipientsCopy.length()){
  103.                QString coded;
  104.                coded = recipientsCopy[0];
  105.                recipientsCopy.removeFirst();
  106.                conn->write(QString("RCPT TO: <%1>\r\n").arg(coded).toAscii());
  107.            }
  108.            else {
  109.                conn->write("DATA\r\n");
  110.                conn->flush();
  111.                emit sendState(3);
  112.            }
  113.        }
  114.        else if(buf.startsWith("535")){ //wrong password
  115.            emit sentError(tr("Usuário Inválido."));
  116.            conn->close();
  117.            sending = false;
  118.            disconnect(&m_timeout,SIGNAL(timeout()));
  119.        }
  120.        else if (buf.contains("354")) { //mail body
  121.            /*from: <Nome_do_Remetente>
  122.            cc: <destinatario_x1>
  123.            bcc: <destinatario_x2>
  124.            Date: dd mmm aaaa hh:mm:ss +0000
  125.            subject: <Assunto>*/
  126.            conn->write(QString(
  127.                         "From: %1 <%2>\r\n"
  128.                         "Cc: Ninguém <rockristao@gmail.com>\r\n"
  129.                         "Subject: %3\r\n\r\n")
  130.                        .arg(tr("Notificador de Status do Sistema"))
  131.                        .arg(m_user)
  132.                        .arg(subject)
  133.                        .toAscii());
  134.            conn->write(body.toAscii());
  135.            conn->write("\r\n.\r\n");
  136.            conn->flush();
  137.            emit sendState(4);
  138.  
  139.  
  140.        }
  141.        else if(buf.startsWith("250 2.0.0")){ // sent
  142.  
  143.            emit sendState(5);
  144.            emit sent();
  145.            conn->write("quit\r\n");
  146.            conn->flush();
  147.            conn->close();
  148.            sending = false;
  149.            disconnect(&m_timeout,SIGNAL(timeout()));
  150.        }
  151.        else if (buf.startsWith("553-5.1.2")){ // recipients erroneous
  152.            emit sentError("O Servidor não reconhece um dos recipients");
  153.            conn->close();
  154.            sending = false;
  155.            emit sent();
  156.            disconnect(&m_timeout,SIGNAL(timeout()));
  157.        }
  158.  
  159.     }
  160.  
  161. }
  162. void smtp::onConnect()
  163. {
  164.     conn->write("EHLO\r\n");
  165.     emit sendState(0);
  166.  
  167. }
  168. void smtp::onConnectError(){
  169.     emit sentError(tr("Erro ao se conectar ao servidor."));
  170.     sending = false;
  171.     disconnect(&m_timeout,SIGNAL(timeout()));
  172. }
  173.  
  174. void smtp::timeout(){
  175.     if (sending){
  176.         emit sentError(tr("O Servidor Demorou demais para responder."));
  177.         sending = false;
  178.         conn->close();
  179.     }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement