Importing Emails to a CRM

-1

I would like to know how to do an integration of the messages sent via CRM in php, and when the person responds, be able to import the email automatically to the CRM.

I imagined something like this, enable the email on the server, pegatudo, and when to send a message through the CRM, put in reply-to: [email protected], enable the IMAP module in php and read the email, based on your Header to find the TO

Does it work? Is there somewhere that explains something, I saw this in the pipedrive Marco

    
asked by anonymous 03.01.2018 / 02:59

1 answer

2

Use the IMAP functions to get the message lists and you can use the filter to detect the messages you want

So to connect and get the INBOX UIDs use something like that with SSL:

$mbox = imap_open('{imap.dominio.com:993/imap/ssl}INBOX', '<seu email>', '<senha>');

If is not with SSL:

$mbox = imap_open('{imap.dominio.com:143/imap/novalidate-cert}INBOX', '<seu email>', '<senha>');

And to search for the messages, create a variable like this:

$filters = array();

And choose the types of filters, search the content of the email:

$corpo = '<busca algo no corpo da pergunta>';
$corpo = addcslashes($corpo, '"\');

$filters[] = 'BODY "' . $corpo . '"';
$filters[] = 'TEXT "' . $corpo . '"';

Search by sender:

$remetente = '<endereço de quem enviou o email>';

$filters[] = 'FROM "' . addcslashes($remetente, '"\') . '"';

Search for subject:

$assunto = '<assunto/titulo da mensagem de email>';

$filters[] = 'SUBJECT "' . addcslashes($assunto, '"\') . '"';

Then after the filters add this ( UNSEEN ) will be only to pick up unread messages yet:

$filters[] = 'UNSEEN';

$uids = imap_search($mbox, implode(' ', $filters), SE_UID);

Then the return can be downloaded like this:

foreach ($uids as $uid) {

    $header = imap_headerinfo($mbox, $uid);
    $structure = imap_fetchstructure($mbox, $uid);

    //Pega os dados da origem do email
    $fromAddress    = isset($header->from) ? $header->from : null;
    $toAddress      = isset($header->to) ? $header->to : null;
    $ccAddress      = isset($header->cc) ? $header->cc : null;
    $bccAddress     = isset($header->bcc) ? $header->bcc : null;
    $replyToAddress = isset($header->reply_to) ? $header->reply_to : null;
    $subject        = isset($header->subject) ? $header->subject : null;
    $date           = isset($header->date) ? $header->date : null;

    if ($structure->type === 1) {
        $mensagem = imap_fetchbody($mbox, $uid, '2');
    } else {
        $mensagem = imap_body($mbox, $uid);
    }

    // Salvar dados das variáveis no banco aqui

}

And to mark as read use the imap_setflag_full function:

 imap_setflag_full($mbox, $uid, '\SEEN', SE_UID)

Then just pick up the variables and save the data to a database or another type you want, an example of insertion in mysql would look something like:

$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world');

/* verifica a conexão */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit;
}

$mbox = imap_open('{imap.dominio.com:143/imap/novalidate-cert}INBOX', '<seu email>', '<senha>')
            or die('Erro ao conectar com o Email');

$assunto = 'ASSUNTO TESTE';

$filters[] = 'SUBJECT "' . addcslashes($assunto, '"\') . '"';

$filters[] = 'UNSEEN';

$uids = imap_search($mbox, implode(' ', $filters), SE_UID);

$stmt = $mysqli->prepare('INSERT INTO mensagens (uid, from, to, cc, bcc, replyto, assunto, data, mensagem) values (?, ?, ?, ?, ?, ?, ?, ?, ?)');

if ($stmt) {
    foreach ($uids as $uid) {

        $header = imap_headerinfo($mbox, $uid);
        $structure = imap_fetchstructure($mbox, $uid);

        //Pega os dados da origem do email
        $fromAddress    = isset($header->from) ? $header->from : null;
        $toAddress      = isset($header->to) ? $header->to : null;
        $ccAddress      = isset($header->cc) ? $header->cc : null;
        $bccAddress     = isset($header->bcc) ? $header->bcc : null;
        $replyToAddress = isset($header->reply_to) ? $header->reply_to : null;
        $subject        = isset($header->subject) ? $header->subject : null;
        $date           = isset($header->date) ? $header->date : null;

        if ($structure->type === 1) {
            $mensagem = imap_fetchbody($mbox, $uid, '2');
        } else {
            $mensagem = imap_body($mbox, $uid);
        }

        // Salvar dados das variáveis no banco aqui
        $stmt->bind_param('issssssss', $uid, $from, $to, $cc, $bcc, $replyto, $assunto, $data, $mensagem);

        $stmt->execute();

        // Marca como lido
        imap_setflag_full($mbox, $uid, '\SEEN', SE_UID);
    }

    printf("Foram inseridas %d mensagens.\n", $stmt->affected_rows);

    $stmt->close();
}

$mysqli->close();
    
03.01.2018 / 04:08