Problem with email with embedded image

2

I'm trying to send email with embedded image in text through boundary .

In Gmail everything is beautiful, as always, but in Outlook the text does not arrive in HTML and the image arrives as an attachment.

<?
@date_default_timezone_set('America/Bahia');
$imagem_nome="imagens/logomarca.png";
$arquivo=fopen($imagem_nome,'r');
$contents = fread($arquivo, filesize($imagem_nome));
$encoded_attach = chunk_split(base64_encode($contents));
fclose($arquivo);
$limitador = "_=======". date('YmdHms'). time() . "=======_";
$limitador2 = "_=======". date('sYmdHms'). time() . "=======_";

$mailheaders = "From: [email protected]\r\n";
$mailheaders .= "MIME-version: 1.0\r\n";
$mailheaders .= "Content-type: multipart/related; boundary=\"$limitador\"\r\n";
$cid = date('YmdHms').'.'.time();

$texto="
<html>
<body>
<img src=\"cid:$cid\">
<font size=6><br />blablabla </font>
</body>
</html>
";

$msg_body = "--$limitador\r\n";
$msg_body .= "Content-Type: multipart/alternative; boundary=\"$limitador2\"\r\n";
$msg_body .= "--$limitador2\r\n";
$msg_body .= "Content-type: text/html; charset=iso-8859-1\r\n";
$msg_body .= "Content-Transfer-Encoding: quoted-printable\r\n";
$msg_body .= "$texto";
$msg_body .= "--$limitador2--\r\n";



$msg_body .= "--$limitador\r\n";
$msg_body .= "Content-type: image/png; name=\"$imagem_nome\"\r\n";
$msg_body .= "Content-Transfer-Encoding: base64\r\n";
$msg_body .= "Content-Disposition: inline; filaname=\"$imagem_nome\"\r\n";
$msg_body .= "Content-ID: <$cid>\r\n";
$msg_body .= "\n$encoded_attach\r\n";
$msg_body .= "--$limitador--\r\n";

if(mail("[email protected]","TEste",$msg_body, $mailheaders)){
    echo"Mensagem enviada";
}

?>

What can I do to solve the problem?

    
asked by anonymous 31.01.2014 / 15:02

2 answers

2

Your script has several errors. I'll mention a few.

  • You have a part defined as quoted-printable, so you need to encode that part as quoted-printable using the quoted_printable_encode function.
  • Missing line breaks separating headers and body from message parts
  • The content-disposition header has an error, where it says filaname should be filename
  • You also have inconsistencies in the character of line breaks, one hour is \ n another time is \ r \ n. The right should be \ n if you are using Linux or \ r \ n if you are using Windows.

I discovered this error by saving the message generated from the headers and body to a file and then moved to my MIME parser class. This class handles messages and tolerates errors with those of the message generated by your script. By enabling the warnings, these errors appear all that I mentioned.

But see, right not to be studying the standards of the email recommend that you use some ready and mature class to compose and send email. I've developed the class MIME email message for this purpose, but there are others if you prefer. What matters is not reinventing the wheel and benefiting from the knowledge shared by others in mature components.

    
01.02.2014 / 01:24
1

Resolved face, I started to use phpmailer, simpler and do not need to worry about header. Vlws !! Galera

    require_once('mail.php');
$database = new Database();


$pnt_not = $database->query("SELECT * FROM notificacao WHERE notificacao_id = '1'")->fetch(PDO::FETCH_ASSOC);
// NOTIFICAÇÃO
$notificacao_id = $pnt_not['notificacao_id'];
$not_imagem = $pnt_not['not_imagem'];
$not_texto =  $pnt_not['not_texto'];
$not_assunto = $pnt_not['not_assunto'];


$sql_pes = "SELECT pessoa.pessoa_id, pessoa.pes_nome, pessoa.pes_email, pessoa.pes_senha FROM pessoa 
            WHERE pessoa.pes_newsletter =  'S' 
            AND pessoa.pessoa_id NOT IN(SELECT notificacao_log.pessoa_id FROM notificacao_log WHERE notificacao_id = '".$pnt_not['notificacao_id']."')";
$arr_pnt_pes = $database->query($sql_pes)->fetchAll(PDO::FETCH_ASSOC);

foreach($arr_pnt_pes as $pnt_per){  
    // PESSOA
    $pes_email = $pnt_per['pes_email'];
    $pessoa_id = $pnt_per['pessoa_id'];
    $pes_nome = $pnt_per['pes_nome'];
    $pes_senha = $pnt_per['pes_senha'];

    // MENSAGEM
    $mensagem = "<center><img alt=\"Dengue na Web\" src=\"cid:img_incorporate_1\"></center><p></p>";
    $assunto = str_replace('#data#',  date('d/m/Y'),  $not_assunto);
    $mensagem .= str_replace('#pessoa#',  $pes_nome, $not_texto);
    $mensagem = str_replace('#link#',  '<a href="http://xxxxx/">Dengue na Web</a>', $mensagem);

    //MAIL
    $mail->AddAddress($pes_email);
    $mail->Subject = $assunto;
    $mail->AddEmbeddedImage('imagens/logomarca.png', "img_incorporate_1");

    if(!empty($not_imagem) && file_exists($not_imagem)){
        $mail->AddEmbeddedImage($not_imagem, "img_incorporate_2");
        $mensagem .= "<p></p><center><img alt=\"Dengue na Web\" src=\"cid:img_incorporate_2\"></center>";
    }

    $mail->Body = utf8_decode($mensagem);

    if($mail->Send()){
        $database->query("INSERT INTO notificacao_log (pessoa_id, notificacao_id, log_data) VALUES ('$pessoa_id', '$notificacao_id', NOW())");
        $mail->ClearAllRecipients();
        $mail->ClearAttachments();
    }
}
    
03.02.2014 / 13:14