<?php

// ------------------------------------------------------------------------- //
// Permet d'envoyer un mailing avec pièce jointe. Les personnes recevront ce //
// mail sans voir qu'il s'agit d'un mailing. Le tout est lié avec une base   //
// MySQL.                                                                    //
// ------------------------------------------------------------------------- //
// Auteur: Ludovic Fotin                                                     //
// Email:  ludovic@fotin.net                                                 //
// Web:                                                                      //
// ------------------------------------------------------------------------- //

<?php
$mailheaders  
= "From: $from\n";
$mailheaders .= "Reply-To: $from\n";

$msg_body = stripslashes($body);

if (
$to == "tous")
{
    
$file = fopen($attach, "r");
    
$contents = fread($file, $attach_size);
    
$encoded_attach = chunk_split(base64_encode($contents));
    
fclose($file);

    
$mailheaders .= "MIME-version: 1.0\n";
    
$mailheaders .= "Content-type: multipart/mixed; ";
    
$mailheaders .= "boundary=\"Message-Boundary\"\n";
    
$mailheaders .= "Content-transfer-encoding: 7BIT\n";
    
$mailheaders .= "X-attachments: $attach_name";

    
$body_top = "--Message-Boundary\n";
    
$body_top .= "Content-type: text/plain; charset=US-ASCII\n";
    
$body_top .= "Content-transfer-encoding: 7BIT\n";
    
$body_top .= "Content-description: Mail message body\n\n";

    
$msg_body = $body_top . $msg_body;

    
$msg_body .= "\n\n--Message-Boundary\n";
    
$msg_body .= "Content-type: $attach_type; name=\"$attach_name\"\n";
    
$msg_body .= "Content-Transfer-Encoding: BASE64\n";
    
$msg_body .= "Content-disposition: attachment; filename=\"$attach_name\"\n\n";
    
$msg_body .= "$encoded_attach\n";
    
$msg_body .= "--Message-Boundary--\n";

    
$serveur = "xxxx";
    
$base = "xxxx";
    
$identifiant="xxxx";
    
$motdepasse="xxxx";
    
$table= "mail";

    
mysql_connect($serveur,$identifiant,$motdepasse);
    
mysql_select_db("$base");

    
$query = "select * from $table";
    
$result = mysql_query($query);

    while (
$row = mysql_fetch_array($result))
    {
        
$mail=$row["mail"];

        
mail($mail, stripslashes($subject), $msg_body, $mailheaders);

        echo
"$mail<br>";
    }
}
else
{
    
$file = fopen($attach, "r");
    
$contents = fread($file, $attach_size);
    
$encoded_attach = chunk_split(base64_encode($contents));
    
fclose($file);

    
$mailheaders .= "MIME-version: 1.0\n";
    
$mailheaders .= "Content-type: multipart/mixed; ";
    
$mailheaders .= "boundary=\"Message-Boundary\"\n";
    
$mailheaders .= "Content-transfer-encoding: 7BIT\n";
    
$mailheaders .= "X-attachments: $attach_name";

    
$body_top = "--Message-Boundary\n";
    
$body_top .= "Content-type: text/plain; charset=US-ASCII\n";
    
$body_top .= "Content-transfer-encoding: 7BIT\n";
    
$body_top .= "Content-description: Mail message body\n\n";

    
$msg_body = $body_top . $msg_body;

    
$msg_body .= "\n\n--Message-Boundary\n";
    
$msg_body .= "Content-type: $attach_type; name=\"$attach_name\"\n";
    
$msg_body .= "Content-Transfer-Encoding: BASE64\n";
    
$msg_body .= "Content-disposition: attachment; filename=\"$attach_name\"\n\n";
    
$msg_body .= "$encoded_attach\n";
    
$msg_body .= "--Message-Boundary--\n";

    
mail($to, stripslashes($subject), $msg_body, $mailheaders);
    echo
"Le mail a bien été envoyé à $to";
}

?>