1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| <?php
class MIME_Mail
{
var $from;
var $to;
var $subject;
var $message;
var $attached;
function MIME_Mail($from, $to=NULL, $subject=NULL, $message=NULL)
{
$this->from = $from;
$this->to = $to;
$this->subject = $subject;
$this->message = $message;
$this->attached = array();
}
function set_from($from)
{
$this->from = $from;
}
function set_to($to)
{
$this->to = $to;
}
function set_subject($subject)
{
$this->subject = $subject;
}
function set_message($message)
{
$this->message = $message;
}
function attach_file($path)
{
switch( strrchr( $path, '.') )
{
case '.gz':
$ctype = "application/x-gzip";
break;
case ".tgz":
$ctype = "application/x-gzip";
break;
case ".zip":
$ctype = "application/zip";
break;
case ".pdf":
$ctype = "application/pdf";
break;
case ".png":
$ctype = "image/png";
break;
case ".gif":
$ctype = "image/gif";
break;
case ".jpg":
$ctype = "image/jpeg";
break;
case ".txt":
$ctype = "text/plain";
break;
case ".htm":
$ctype = "text/html";
break;
case ".html":
$ctype = "text/html";
break;
default:
$ctype = "application/octet-stream";
break;
}
$handle = fopen($path, "r");
$code = fread($f, sizeof($path));
$code = chunk_split(base64_encode($code));
fclose($handle);
$this->attached[] = array('ctype' => $ctype, 'code' => $code, 'name' => basename($path));
}
function send_mail()
{
$boundary = md5(uniqid(time() ));
$entete = "From: {$this->from}\r\n";
$entete .= "Reply-To: {$this->from}\r\n";
$entete .= "MIME-Version: 1.0\r\n";
$entete .= "X-Mailer: PHP\r\n";
$entete .= "Content-Type: multipart/mixed;boundary=$boundary\r\n\r\n";
$entete_message = "--$boundary\r\n";
$entete_message .= "Content-type:text/plain;charset=us-ascii\r\n";
$entete_message .= "Content-transfer-encoding:8bit\r\n\r\n";
$entete_message .= "{$this->message}\r\n";
$entete_attached = '';
foreach( $this->attached as $attach )
{
$entete_attached .= "--$boundary\r\n";
$entete_attached .= "Content-type:{$attach['ctype']};name={$attach['name']}\r\n";
$entete_attached .= "Content-transfer-encoding:base64\r\n\r\n";
$entete_attached .= "{$attach['code']}\r\n";
$entete_attached .= "--$boundary--";
}
return mail($this->to, $this->subject,"",$entete . $entete_message . $entete_attached);
}
}
?> |
Partager