L. Pillot
L. Pillot

Reputation: 3

HTML/PHP Mail with attachment displayed as text and not HTML

I followed this tutorial (it's in french, but you can check the code) : https://openclassrooms.com/courses/e-mail-envoyer-un-e-mail-en-php

In the first part, (sending a basic mail) I had no trouble : I recieved an email on both formats (plain text and HTML).

But when I went on second part (add an attached-file to the mail sent), I had troubles. I do recieved the attached PDF, mais the mail is only read in text, not in HTML. Which is a problem to me. And I find it disturbing.

I looked on the internet, saw there are other ways to do (use libraries and things like that), but I kinda "don't want to". Don't get me wrong, I'm usually eager to use easier ways that works, and I don't really like reinventing the wheel, but here are my reasons : - I don't want to contact my CTO to implement libraries or new fonctionnalities to our server - I don't know if there are already libraries or extensions installed (I've been here for 4 months, but I was doing other tasks than coding). - now it's personnal, I WANT to know why it doesn't work (even if it's probably a stupid mistake of mine)

The code in question :

if (!preg_match("#^[a-z0-9._-]+@(hotmail|live|msn).[a-z]{2,4}$#", $mail)) // On filtre les serveurs qui rencontrent des bogues.
{
    $passage_ligne = "\r\n";
}
else
{
    $passage_ligne = "\n";
}

//=====Déclaration des messages au format texte et au format HTML.
$message_txt = "my text, nothing to see here";

$message_html = "my text in html, nothing to see here";
//==========

//=====Création de la boundary
$boundary = "-----=".md5(rand());
$boundary_alt = "-----=".md5(rand());


//==========

//=====Définition du sujet.
$sujet = utf8_decode("My subject");
//=========

//=====Création du header de l'e-mail.
$header = "From: \"Mail adress\"<[email protected]>".$passage_ligne;
$header.= "Reply-to: \"Mail adress\" <[email protected]>".$passage_ligne;
$header.= "MIME-Version: 1.0".$passage_ligne;
$header.= "Content-Type: multipart/mixed;".$passage_ligne." boundary=\"$boundary\"".$passage_ligne;
//==========

//=====Création du message.
$message = $passage_ligne."--".$boundary.$passage_ligne;

$message.= "Content-Type: multipart/alternative;".$passage_ligne." boundary=\"$boundary_alt\"".$passage_ligne;

$message.= $passage_ligne."--".$boundary_alt.$passage_ligne;

//=====Ajout du message au format texte.
$message.= "Content-Type: text/plain; charset=\"UTF8\"".$passage_ligne;
$message.= "Content-Transfer-Encoding: 8bit".$passage_ligne;
$message.= $passage_ligne.$message_txt.$passage_ligne;
//==========

$message.= $passage_ligne."--".$boundary_alt."--".$passage_ligne;

//=====Ajout du message au format HTML
$message.= "Content-Type: text/html; charset=\"UTF8\"".$passage_ligne;
$message.= "Content-Transfer-Encoding: 8bit".$passage_ligne;
$message.= $passage_ligne.$message_html.$passage_ligne;
//==========

//=====On ferme la boundary alternative.

$message.= $passage_ligne."--".$boundary_alt."--".$passage_ligne;

//==========


// AJOUT DE L'EBOOK AU FORMAT PDF
$file_name = "myfile.pdf";
$file_type = filetype($file_name);
$file_size = filesize($file_name);

$fichier   = fopen($file_name, "r");   //on ouvre le fichier en lecture seule.
$attachement = fread($fichier, $file_size); //on lit l'ensemble du fichier avec la fonction fread.
$attachement = chunk_split(base64_encode($attachement));
fclose($fichier); //on ferme le fichier.

$message.= $passage_ligne."--".$boundary.$passage_ligne;

$message.= "Content-Type: ".$file_type."; name=".$file_name.$passage_ligne;
$message.= "Content-Transfer-Encoding: base64".$passage_ligne;
$message.= "Content-Disposition: attachment; filename=".$file_name.$passage_ligne.$passage_ligne;

$message.= $passage_ligne.$attachement.$passage_ligne.$passage_ligne;

$message.= $passage_ligne."--".$boundary."--".$passage_ligne;


//=====Envoi de l'e-mail.
if(mail($mail,$sujet,$message,$header)) echo "Mail correctement envoyé à l'adresse ".$mail.".";
    else echo "Le mail n'a pas pu être envoyé à l'adresse ".$mail.".";
//==========

So here's the code. I checked on other websites, my code should work, but still, it doesn't. After a few hours working on it and with the birth of a small headache, I'm placing my fate on your hands. Thanks in advance, and have a good day.

PS : as you would have guessed, English isn't my mother tongue, so I might not understand everything in your answers !

Upvotes: 0

Views: 845

Answers (1)

Arnial
Arnial

Reputation: 1441

You have error on line 47 (in example, line before sending text/html).

Current version

$message.= $passage_ligne."--".$boundary_alt."--".$passage_ligne;

Should be

$message.= $passage_ligne."--".$boundary_alt.$passage_ligne;

Reason --$boundary_alt-- will close multipart/alternative part of email. Your HTML part will be ignored. On line 47 you need to indicate end only text/plain part, not whole multipart/alternative

Upvotes: 1

Related Questions