Import Emails to a CRM

Asked

Viewed 85 times

-1

I would like to know how to integrate the messages sent via CRM in php, and when the person responds, can import the email automatically to CRM.

I imagined something like this, enable the mail on the server, take everything, and when sending a message through 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 out? Is there anywhere that explains anything, I saw it on the pipedrive Marco

1 answer

2

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

So to connect and get INBOX Uids use something like SSL:

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

If nay for with SSL:

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

And to find the messages create a variable like this:

$filters = array();

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

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

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

Search for sender:

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

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

Search for the subject:

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

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

Then after the filters add this (the UNSEEN) will only be 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 score as dealt use the function imap_setflag_full:

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

So just take the variables and save the data in a database or other type you want, an insert example in mysql would be 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();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.