Problem sending email (with html) using PHP

Asked

Viewed 426 times

1

Hello, I have an html code to send in the email that in normal html mode it usually appears but when I send the email, when I visualize it in gmail, I get all the html wrong. Already in Thunderbird the email arrives with html correctly.

Someone has a solution for me?

My html code is:

function html() {
    $r = '
<!-- v 0.1 -->
<head>
    <style>
    .descricao {text-align:center;float:left;width:350px;font-family:Arial;height:100px;border:1px solid green;background-color:white;font-size:40px;color:black;padding:15px;}
    .especificacao {margin-left:3px;float:left;text-align:center;width:350px;font-family:Arial;height:100px;border:1px solid green;background-color:white;font-size:40px;color:black;padding:15px;}
    .empresa {text-align:center;width:735px;margin-bottom:3px;font-family:Arial;height:75px;border:1px solid green;background-color:white;font-size:40px;color:black;padding:15px;}
    </style>
</head>
<div style="width:770px;border:1px solid gray;padding:15px;background-color:green;">
    <div class="empresa">
        '.template("empresa").'
    </div>
    <div class="descricao">
        '.template("descricao_item_01").'
    </div>
    <div class="especificacao">
        DESCRICAO ---> 01
        PREÇO ----> R$ '.template("preco_item_01").'
    </div>
    <div class="descricao">
        '.template("descricao_item_02").'
    </div>
    <div class="especificacao">
        DESCRICAO ---> 02
        PREÇO ----> R$ '.template("preco_item_02").'
    </div>
    <div class="descricao">
        '.template("descricao_item_03").'
    </div>
    <div class="especificacao">
        DESCRICAO ---> 03
        PREÇO ----> R$ '.template("preco_item_03").'
    </div>
    <div class="descricao">
        '.template("descricao_item_04").'
    </div>
    <div class="especificacao">
        DESCRICAO ---> 04
        PREÇO ----> R$ '.template("preco_item_04").'
    </div>
    <div class="descricao">
        '.template("descricao_item_05").'
    </div>
    <div class="especificacao">
        DESCRICAO ---> 05
        PREÇO ----> R$ '.template("preco_item_05").'
    </div>
    <div class="descricao">
        '.template("descricao_item_06").'
    </div>
    <div class="especificacao">
        DESCRICAO ---> 06
        PREÇO ----> R$ '.template("preco_item_06").'
    </div>
    <div style="clear:both;">'.template("cnpj").' - '.template("telefone").'</div>
</div>



    ';
    return $r;
}

The html upload code is:

function sendmail() {
    if(!isset($_POST[Submit])) die("Não foi recebido nenhum parâmetro");
    /* Medida preventiva para evitar que outros domínios sejam remetente da sua mensagem. */
    if (eregi('tempsite.ws$|locaweb.com.br$|hospedagemdesites.ws$|websiteseguro.com$', $_SERVER[HTTP_HOST])) {
            $emailsender=trim($_POST['emailremetente']);
    } else {
            $emailsender = "noreply@" . $_SERVER[HTTP_HOST];
    }

    /* Verifica qual é o sistema operacional do servidor para ajustar o cabeçalho de forma correta. Não alterar */
    if(PHP_OS == "Linux") $quebra_linha = "\n";
    elseif(PHP_OS == "WINNT") $quebra_linha = "\r\n";
    else die("Incompatibilidade com Sistema Operacional");

    // Passando os dados obtidos pelo formulário para as variáveis abaixo
    $nomeremetente     = $_POST['nomeremetente'];
    $emailremetente    = trim($_POST['emailremetente']);
    $assunto           = $_POST['assunto'];

    $array_emaildestinatario = array();
    $array_emaildestinatario = explode("\n",trim($_POST['emaildestinatario'])); // separa os emails pelas vírgulas em uma array
    foreach($array_emaildestinatario as $elemento) {
        //pra cada email envia
        /* Montando a mensagem a ser enviada no corpo do e-mail. */
        $mensagemHTML = html();


        /* Montando o cabeçalho da mensagem */
        $headers = "MIME-Version: 1.1".$quebra_linha;
        $headers .= "Content-type: text/html; charset=iso-8859-1".$quebra_linha;
        // Perceba que a linha acima contém "text/html", sem essa linha, a mensagem não chegará formatada.
        $headers .= "From: ".$emailsender.$quebra_linha;
        $headers .= "Return-Path: " . $emailsender . $quebra_linha;

        $headers .= "Reply-To: ".$emailremetente.$quebra_linha;
        // Note que o e-mail do remetente será usado no campo Reply-To (Responder Para)

        /* Enviando a mensagem */
        mail($elemento, $assunto, $mensagemHTML, $headers, "-r". $emailsender);
    }
    /* Mostrando na tela as informações enviadas por e-mail */
    ?>
        <div style="color:white;border:1px solid black;padding:15px;background-color:rgba(150,50,50,0.8);font-family:Arial;margin-bottom:5px;font-size:14px;">
            Enviando <?php echo count($array_emaildestinatario); ?> e-mails, aguarde um instante...
        </div>
        <div style="color:white;border:1px solid black;padding:15px;background-color:rgba(150,50,50,0.8);font-family:Arial;margin-bottom:5px;font-size:14px;">
            De:<?php echo $emailsender; ?>
        </div>
        <div style="color:white;border:1px solid black;padding-left:15px;padding-top:5px;padding-bottom:5px;background-color:rgba(150,50,50,0.3);font-family:Arial;margin-bottom:5px;font-size:14px;">
        <?php 
        foreach($array_emaildestinatario as $elemento) {
            echo "<p>$elemento</p>";
        }
        ?>
        </div>
<?php
}

HE GETS WRONG IN GMAIL LIKE THIS: (MY PROBLEM IS HERE IN GMAIL) HTML BUGADO AND IN THE THUNDERBIRD: HTML NAO BUGADO

Someone has a solution?

  • 1

    The solution is to use HTML 4, <table>, and virtually no CSS for email.

  • 1

    True, no CSS and if you have to use it, inline it into html code and not into a separate file. Always use for the absolute url images.

  • I had to switch to CSS INLINE, removing all my HEAD to work. In this case the solution I got at the moment is better than using HTML 4 Table. But now I wanted to load an external css instead of inline, by the external way or in the own head as it was does not give :/ ? Thanks for the personal help, Marcos Vinicius..., that’s right, INLINE solved the first trip. A shame to have to use this lot of css code in the middle of my html.

  • I will make the template and after it ready to go to inline, if I do everything inline I get crazier that I am erasing and rewriting. Thanks there

2 answers

1


In Thunderbird it can be encoding conflict with your PHP (check your php encoding if it is also 8859-1).

Gmail may be a matter of using css inline in the head, according to this article (here) it lists in the items that should not be made.

0

You don’t need to use these line breaking tests. The constant PHP_EOL already does that. You cannot use external styles inside the tag style.

Use an inliner CSS tool to do this for you, it’s easier. http://templates.mailchimp.com/resources/inline-css/

You nay should use ISO-8859-1 encoding as the colleague above spoke, and yes Unicode UTF-8 ALWAYS, if you do not want to have problems with accentuation and strange characters.

Browser other questions tagged

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