// that's the function to use...
function SendEmail($email,$titre,$corps,$lien,$attach = '') {
$delim='\r\n';
CreateEmail("$email","$titre", "$corps".$delim.$delim."Merci de votre confiance,".$delim.$delim."Signature.");
if ($attach<>'') AddAttachment($attach); // full file path
CloseAndSend();
} // SendEmail procedure
// here si the 'unit' (include file) defining the stuff :
<?
//
// ----------------------------- EMAILS --------------------------------
//
//globales
$headers='';
$subject='';
$recipient='';
$body='';
$separateur='';
function CreateEmail($destinataires,$sujet,$contenu) {
GLOBAL $headers,$subject,$recipient,$body,$separateur,$SERVER_NAME;
// REMISE ? VIDE
$headers='';
$subject='';
$recipient='';
$body='';
$separateur='12EDAIN34';
$From="Robot MyCompany. <myemailaddress>";
//s?parateur
// sujet
$subject=$sujet;
// headers de base
$headers.="MIME-Version: 1.0\r\nFrom: $From\n"; // later : contact@$SERVER_NAME
// d?coupage pour destinataires
$tableau=array();
$tableau=explode(';',$destinataires);
for ($i=0;$i<count($tableau);$i++) {
if (substr($tableau[$i],0,3)=='CC:') {
$adresses=substr($tableau[$i],3); // fin de la sous-cha?ne
// $adresses=substr($adresses,0,strlen($adresses)-2); // on enl?ve le 0A 0D final
$headers2="cc: $adresses\n";
} else if (substr($tableau[$i],0,4)=='BCC:') {
$adresses=substr($tableau[$i],4); // fin de la sous-cha?ne
// $adresses=substr($adresses,0,strlen($adresses)-2); // on enl?ve le 0A 0D final
$headers2.="Bcc: $adresses\n";
} else { // cas par d?faut ou To:
if (substr($tableau[$i],0,3)=='To:') $adresses=substr($tableau[$i],3); // fin de la sous-cha?ne
else $adresses=$tableau[$i];
// $adresses=substr($adresses,0,strlen($adresses)-2); // on enl?ve le 0A 0D final
$recipient="$adresses"; // pas de \r\n
}
} // for destinataires rencontr?s
$headers.="Reply-To: $From\r\nX-Mailer: PHP/" . phpversion()."\r\nX-Priority: 3\r\n";
// premier s?parateur et body textuel
$headers.="Content-Type: multipart/mixed; boundary=\"$separateur\"\r\n";
$headers.="Content-Transfer-Encoding: 7bit\r\n";
$headers.=$headers2;
$body.="This part of the E-mail should never be seen. If\nyou are reading this, consider upgrading your e-mail\nclient to a MIME-compatible client.\r\n";
$body.="\r\n--$separateur\r\n";
//ent?tes
$body.="Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$body.="Content-Transfer-Encoding: 7bit\r\n";
//data
$body.="\r\n$contenu\r\n"; // blank line required before body part !!!
} // CreateEmail Procedure
function AddAttachment( $filename ) {
GLOBAL $headers,$subject,$recipient,$body,$separateur;
// d?termination du type de l'attachement
// calcul extension du nom local
if (strpos($filename,'.')) $locext=substr($filename,strpos($filename,'.')); // . dedans
else $locext='';
$locext=strtolower($locext);
switch ($locext) {
case '.jpg' :
case '.jpe' :
case '.jpeg' : $locmimetype='image/jpeg'; $locencode='base64'; break;
case '.txt' : $locmimetype='text/plain'; $locencode="7bit"; break;
case '.ai' :
case '.eps' :
case '.ps' : $locmimetype='application/postscript'; $locencode="7bit"; break;
case '.rtf' : $locmimetype='application/rtf'; $locencode="7bit"; break;
case '.wav' : $locmimetype='audio/x-wav'; $locencode="base64"; break;
case '.gif' : $locmimetype='image/gif'; $locencode="base64"; break;
case '.tiff' :
case '.tif' : $locmimetype='image/tiff'; $locencode="base64"; break;
case '.html' : $locmimetype='text/html'; $locencode="7bit"; break;
case '.mpeg' :
case '.mpg' :
case '.mpe' : $locmimetype='video/mpeg'; $locencode="base64"; break;
case '.mov' : $locmimetype='video/quicktime'; $locencode="base64"; break;
case '.avi' : $locmimetype='video/x-msvideo'; $locencode="base64"; break;
case '.doc' : $locmimetype='application/msword'; $locencode="base64"; break;
case '.pdf' : $locmimetype='application/pdf'; $locencode="base64"; break;
default : $locmimetype='text/plain'; $locencode="7bit"; break;
} // case of $locext
$locname=basename($filename);
$fd = fopen ($filename, "r");
$dataread = fread ($fd, filesize ($filename));
fclose ($fd);
//test:$dataread=file($filename);
if ($locencode=='base64') $locdata=base64_encode($dataread);
else $locdata=$dataread;
// s?parateur et body
$body.="\r\n--$separateur\r\n";
//ent?tes
$body.="Content-Type: $locmimetype; name=$locname\r\n";
$body.="Content-Transfer-Encoding: $locencode\r\n";
$body.="Content-Description: $locname\r\n";
$body.="Content-Disposition: attachment\r\n";
//data
$body.="\r\n$locdata\r\n"; // blank line required before body part !!!
} // AddAttachment Procedure
function CloseAndSend() {
GLOBAL $headers,$subject,$recipient,$body,$separateur;
//s?parateur final (terminateur)
$body.="\r\n--$separateur--\r\n";
mail("$recipient", "$subject", "$body","$headers");
/*pour v?rifs :
echo "recipient ".htmlspecialchars($recipient)."<HR>";
echo "subject ".htmlspecialchars($subject)."<HR>";
echo "body ".htmlspecialchars($body)."<HR>";
echo "headers ".htmlspecialchars($headers)."<HR>";
*/
} // CloseAndSend Procedure
?>