<?php

// ------------------------------------------------------------------------- //
// Envoi de mail avec image de fond                                          //
// ------------------------------------------------------------------------- //
// Auteur: Gorn <jaxx@freesurf.fr>                                           //
// Web:                                                                      //
// ------------------------------------------------------------------------- //

/*
Je cherche encore pour rajouter les pièces jointes, mais bizarrement, ça marche
si je mets soit l'un soit l'autre, ça marche très bien, mais avec les deux en
même temps plus rien ne marche...

Donc, avis aux amateurs, pour améliorer ;)

Je suis parti d'un script posté ici (ou une classe) pour les fichiers attachés.

La fonction s'appelle avec plusieurs paramètres :

mail_fond($filename,$email,$from,$subject,$corps);

filename : nom du fichier image à mettre en fond (personnellement, je laisse la
possibilité n'importe quelle image)
email : liste des destinataires (séparés par des ;)
*/


// fonction de lecture binaire du fichier
function encode_file ($path) {
  if(!(
$fd = fopen($path, "rb"))){
    
sprintf("File Error: Could not open file %s", $path);
    return
false;
  }
  
$file = fread($fd, filesize($path));
  
$encoded = encode_string($file);
  
fclose($fd);
  return(
$encoded);
}

//fonction de codage en base64 du fichier lu
function encode_string ($str) {
  
$encoded = chunk_split(base64_encode($str));
  return(
$encoded);
}

//fonction d'envoi du mail avec image de fond
function mail_fond($filename,$destinataire,$from,$subject,$corps)
{
    
$emails = explode(';',$destinataire);
    
$destinataire = $emails[0];
    if(
$filename!=''){
            
$cid = md5(uniqid(rand()));
            
// pour rappeler en tant que background
            
$type = explode('.',$filename);
            
// ici je n'ai pas listé tous les types, mais vous pouvez les
            // rajouter facilement
            
switch(strtolower($type[1])){
                    case
'gif':
                            
$type = 'image/gif';
                            break;
                    case
'jpg':
                    case
'jpeg':
                            
$type = 'image/jpeg';
                            break;
                    default:
                            
$type = 'application/octect-stream';
            }
    }
    
$limite1 = md5 (uniqid (rand()));
    
//premier boundary
    
$limite2 = md5 (uniqid (rand()));
    
//second boundary
    
$entete = "Return-Path: <$from>\n";
    
$entete .= "Delivered-To: $destinataire\n";
    
$entete .= "Subject: $subject\n";
    
$entete .= "To: $destinataire\n";
    if(
sizeof($emails)>1){
            
array_shift($emails);
            
$emails = implode(", ",$emails);
            
$entete .= "Cc: $emails\n";
    }
    
$entete .= "Date: ".date("D, j M Y G:i:s +0200")."\n";
    
$entete .= "From: \"Backup Technology\" <$from>\n";
    
$entete .= "X-Priority: 3\n";
    
$entete .= "X-Mailer: PHP\n";
    
$entete .= "MIME-Version: 1.0\n";
    
$entete .= "Content-Type: multipart/related;\n  type=multipart/alternative\n    boundary=\"b1_$limite1\"\n";
    
//les entêtes du mail
    
$body = "--b1_$limite1\n";
    
$body .= "Content-Type: multipart/alternative;\n        boundary=\"b2_$limite2\"\n\n";
    
$body .= "--b2_$limite2\n";
    
$body .= "Content-Type: text/plain; charset = \"iso-8859-1\"\n";
    
$body .= "Content-Transfer-Encoding: quoted-printable\n\n\n";
    
$body .= "$corps\n\n";
    
$body .= "--b2_$limite2\n";
    
$body .= "Content-Type: text/html; charset = \"iso-8859-1\"\n";
    
$body .= "Content-Transfer-Encoding: quoted-printable\n\n\n";
    
$body .= '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD><TITLE></TITLE><META http-equiv=3DContent-Type content=3D"text/html; charset=3Diso-8859-1">';
    
$body .= '<STYLE>BODY { BACKGROUND-POSITION: left top; MARGIN-TOP: 80px; FONT-SIZE: 12pt; MARGIN-LEFT: 175px; COLOR: #000000; BACKGROUND-REPEAT: no-repeat; FONT-FAMILY: Times New Roman}';
    
$body .= '</STYLE><META content=3D"MSHTML 5.50.4134.600" name=3DGENERATOR></HEAD>'."\n".'<BODY bgColor=3D#ffffff';
    
//si une image a été uploadée, elle se place en fond du body (non répétée)
    
$body .= (isset($cid))? ' backGround=3Dcid:'.$cid:'';
    
$body .= '>'."\n".'<DIV>'."\n".'<PRE>'.$corps.'</PRE>'."\n".'</DIV>'."\n".'</BODY>'."\n".'</HTML>'."\n\n\n";
    
$body .= "--b2_$limite2";
    
$body .= "--\n\n";
    
$body .= "--b1_$limite1";
    
// on encode l'image dans le corps du mail si elle existe
    
if($filename!=''){
            
$body .= "\n\nContent-Type: $type;\n    name=\"$filename\"\n";
            
$body .= "Content-Transfer-Encoding: base64\n";
            
$body .= "Content-Disposition: inline; filename=\"$filename\"\n";
            
$body .= "Content-ID: <$cid>\n\n";
            
$body .= encode_file($filename);
            
$body .= "\n\n\n--b1_$limite1";
    }
    
$body .= "--\n\n";
    
$bool = mail($destinataire, $subject, $body, $entete);
    
// on supprime l'image pour ne pas encombrer le serveur
    
if(file_exists('images/'.$filename) && $filename!='') unlink('images/'.$filename);
    return
$bool;
}

?>