Problem with email with embedded image

Asked

Viewed 1,755 times

2

I am trying to send email with image embedded in the text through the Boundary.

In Gmail everything beautiful, as always, but in Outlook does not arrive the text 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?

  • So when it comes to email, it is always indicated to use tables, to stay standardized on all email clients, because as even you said or outlook is the one that gives the most headache. Here is a document for you to see some tips Email Marketing Creation Pattern

  • 1

    I’m doing something similar using PHP Perl Mail, just add the image with Attach and everything works out.

2 answers

2

Your script has several errors. I will mention a few.

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

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

But you see, the right thing not to be studying the patterns of the email I recommend you use some ready and mature class to compose and email. I 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.

1

Solved dude, I started using phpmailer, simpler and I don’t have to worry about header. Vlws!! Guys

    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();
    }
}

Browser other questions tagged

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