简体   繁体   English

PHP邮件函数在hotmail / outlook中返回utf8错误

[英]PHP mail function returning utf8 errors in hotmail/outlook

Lately I've been having some big problems with sending e-mails through my website, as it seems to always show major encoding differences on the e-mail clients. 最近,我在通过网站发送电子邮件时遇到了一些大问题,因为它似乎总是在电子邮件客户端上显示出主要的编码差异。 As it almost always works on Gmail and others, on Hotmail/Outlook there's always an UTF8 error in the title/subject of the message. 由于它几乎始终可以在Gmail和其他邮件上使用,因此在Hotmail / Outlook上,邮件标题/主题中始终存在UTF8错误。 I tried encoding/decoding several variables to keep that from happening, but every solution ends up leaving an error. 我尝试对几个变量进行编码/解码,以防止发生这种情况,但是每种解决方案最终都会留下错误。

Here's the function to send the e-mails via form: 这是通过表单发送电子邮件的功能:

function enviarEmail($nomeRemetente = '', $emailRemetente = 'email@dominio.com', $emailDestinatario = 'email@dominio.com', $emailResposta = 'email@dominio.com', $assunto = '', $campos = array(), $dados = array(), $customMsg = false, $mensagemHTML = '')
{
   $retorno = true;
   $quebra_linha = "\n";
   if (PHP_OS == 'Linux') {
      $quebra_linha = "\n";
   } elseif (PHP_OS == 'WINNT') {
      $quebra_linha = "\r\n";
   }

   if (!$customMsg) {
      $mensagemHTML = '<table width="490" border="0" cellpadding="0" cellspacing="6">
                <tr>
                    <td colspan="2">
                        <p style="font-size: 14px; font-family: Arial, Helvetica, sans-serif; color: #A93118;">..: ' . $assunto . '</p>
                        <p>Formul&aacute;rio preenchido em ' . date('d/m/Y') . ' as ' . date('H:i') . '</p>
                    </td>
                </tr>
                ';
      $qtde = count($campos);
      for ($i = 0; $i < $qtde; $i++) {
          $mensagemHTML .= '
                <tr> 
                  <td align="right"><strong>' . $campos[$i] . ': </strong></td>
                  <td>' . $dados[$i] . '</td>
                </tr>
                ';
      }
      $mensagemHTML .= '</table>';
   }

   $headers = implode($quebra_linha, #
              array('MIME-Version: 1.1', #
                   'Content-type: text/html; charset=utf-8', #
                   'From: ' . html_entity_decode($nomeRemetente) . ' <' . $emailRemetente . '>', #
                   'Return-Path: ' . utf8_decode($nomeRemetente) . ' <' . $emailRemetente . '>', #
                   'Reply-To: ' . $emailResposta, #
                   'Subject: ' . $assunto, #
                   'X-Priority: 3'
              ));

   $emailDestinatario = is_array($emailDestinatario) ? $emailDestinatario : array($emailDestinatario);
   foreach ($emailDestinatario as $emailDestino) {
      mail($emailDestino, $assunto, $mensagemHTML, $headers) or $retorno = false;// die('Erro no servidor!');
   }
   return $retorno;
}

And this function is called here: 这个函数在这里被调用:

function enviarContato()
{
   $nomeRemetente = PROJECT_SHORT_TITLE;
   $emailRemetente = $emailResposta = PROJECT_EMAIL;

   $subject = 'Contato no site Modelo Site Rápido - ' . date('d/m/Y H:i:s');

   $emailDestinatario = array('programacao@monge.com.br'/*, PROJECT_EMAIL*/);

   $campos = array();
   $dados = array();

   $campos[] = 'Nome';
   $dados[] = isset($_REQUEST['contatoNome']) ? $_REQUEST['contatoNome'] : '';

   $campos[] = 'Email';
   $emailResposta = $dados[] = isset($_REQUEST['contatoEmail']) ?   $_REQUEST['contatoEmail'] : '';

   $campos[] = 'Telefone';
   $dados[] = isset($_REQUEST['contatoTelCel']) ? htmlspecialchars($_REQUEST['contatoTelCel'], ENT_COMPAT, 'UTF-8') : '';

   $campos[] = 'Mensagem';
   $dados[] = isset($_REQUEST['contatoMensagem']) ? nl2br(stripcslashes($_REQUEST['contatoMensagem'])) : '';

   $conf = enviarEmail($nomeRemetente, $emailRemetente, $emailDestinatario, $emailResposta, utf8_decode($subject), $campos, $dados);

$link = 'http://' . PROJECT_URL . '/contato.php'; // usado sem mod_rewrite
if (isset($MG_MR_Settings['active']) && $MG_MR_Settings['active']) {
       $link = 'http://' . PROJECT_URL . '/contato'; // usado com mod_rewrite
    }

if ($conf) {
       echo "<script type='text/javascript'>";
    echo "alert('Contato enviado com sucesso!');";
    echo "document.location.replace('$link');";
    echo "</script>";
    die();
    } else {
       echo "<script type='text/javascript'>";
    echo "alert('Erro ao enviar contato, contate o administrador.');";
    echo "document.location.replace('$link');";
    echo "</script>";
    die();
    }
}

$msgContato = '';
if (!empty($_POST['SubmitContato'])) {
   $msgContato = enviarContato();
}

The problem that this is returning on Hotmail/Outlook is like this: 在Hotmail / Outlook上返回的问题是这样的:

Contato no site Centro Estético Bela - 17/12/2013 11:48:53 177.97.93.251 Contato没有网站CentroEsté©tico Bela-17/12/2013 11:48:53 177.97.93.251

It works well on Gmail. 它在Gmail上运作良好。 If anyone can point to the right direction I would greatly appreciate it. 如果有人能指出正确的方向,我将不胜感激。 Please ask for any info that might help you solve this, hope it seems clear enough. 请要求提供任何可能帮助您解决此问题的信息,希望看起来足够清楚。

Thank you in advance. 先感谢您。

Good day: 美好的一天:

i´m use this subject 我正在使用这个主题

$subject = "=?UTF-8?B?" . base64_encode('Confirmación de Compra (' . $order_id .")" ) . "?=";

in your case: 在您的情况下:

$subject = "=?UTF-8?B?" . base64_encode('Contato no site Modelo Site Rápido - ' . date('d/m/Y H:i:s') ) . "?="; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM