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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
<?php
class Mail
{
const LINE_SEPARATOR = "\n";
private $aAttachements = array();
private $sTextPlain = null;
private $sHTML = null;
private $aHeaders = array();
private $aInlines = array();
private $sBoundary;
/**
* Ajoute un header a l'email.
*
* ATTENTION: Plusieurs header peuvent porter le meme nom.
*
* @param string $sName
* @param string $sValue
*/
public function appendHeader($sName, $sValue) {
//TODO check parameters
if (!self::checkHeader($sName, $sValue)){
return false;
}
if (!isset($this->aHeaders[$sName])) {
$this->aHeaders[$sName] = array();
}
$this->aHeaders[$sName][] = $sValue;
return true;
}
/**
* Definie la valeur du texte plat du message
*
* @param string $sText
* @param string $sEncoding
*/
public function setTextPlain($sText, $sEncoding='iso-8859-1') {
//TODO check parameters
$this->sTextPlain = implode(Mail::LINE_SEPARATOR, array(
'Content-Type: text/plain; charset="'.strtolower($sEncoding).'"',
'Content-Transfer-Encoding: quoted-printable'
));
$this->sTextPlain .= Mail::LINE_SEPARATOR . Mail::LINE_SEPARATOR;
$this->sTextPlain .= self::QPE($sText);
$this->sTextPlain .= Mail::LINE_SEPARATOR;
}
/**
* Definie la valeur de l'HTML du message
*
* @param string $sHTML
* @param string $sEncoding
*/
public function setHTML($sHTML, $sEncoding='iso-8859-1'){
//TODO check parameters
$this->sHTML = implode(Mail::LINE_SEPARATOR, array(
'Content-Type: text/html; charset="'.strtolower($sEncoding).'"',
'Content-Transfer-Encoding: quoted-printable'
));
$this->sHTML .= Mail::LINE_SEPARATOR . Mail::LINE_SEPARATOR;
$this->sHTML .= self::QPE($sHTML);
$this->sHTML .= Mail::LINE_SEPARATOR;
}
/**
* Cree une frontiere pour les multiparts
*
* @return string '----='.md5()
*/
private static function createBoundary() {
return '----='.md5(time() . mt_rand());
}
/**
* Cree un multipart.
*
* @param string $sType
* @param array $aParts
* @return string
*/
private static function createMultipart($sType, $aParts){
//TODO check parameters
$sBoundary = self::createBoundary().'_'.$sType;
$sMessage = '';
$sMessage .= 'Content-Type: multipart/'.$sType.'; boundary="'.$sBoundary.'"';
$sMessage .= Mail::LINE_SEPARATOR . Mail::LINE_SEPARATOR;
foreach ($aParts as $sPart) {
$sMessage .= '--'.$sBoundary;
$sMessage .= Mail::LINE_SEPARATOR;
$sMessage .= $sPart;
}
$sMessage .= '--'.$sBoundary.'--';
$sMessage .= Mail::LINE_SEPARATOR;
return $sMessage;
}
/**
* Construit le message de l'email
*
* @return string
*/
private function buildMessage() {
if (($this->sHTML !== null) && ($this->sTextPlain !== null)) {
return self::createMultipart('alternative', array($this->sTextPlain, $this->sHTML));
}
return $this->sTextPlain;
}
/**
* Construit le corps de l'email
*
* @return string le corps de l'email
*/
private function buildBody() {
$aBody[] = 'This is a multi-part message in MIME format.'.Mail::LINE_SEPARATOR;
$aBody[] = '--'.$this->sBoundary;
$aBody[] = $this->buildMessage();
foreach ($this->aAttachements as $aAttachement) {
$aBody[] = '--'.$this->sBoundary;
$aBody[] = $aAttachement;
}
$aBody[]= '--'.$this->sBoundary.'--';
return implode(Mail::LINE_SEPARATOR, $aBody);;
}
/**
* Retourne le contenu d'un fichier chunked et encoder en base64
*
* @param string $sPath
* @return string
*/
private static function getChunkedFileContent($sPath) {
//TODO check parameters
$xData = null;
if (!is_readable($sPath)){
throw new Exception('`'.$sPath.'`: can\'t read.');
return;
}
if (!is_file($sPath)) {
throw new Exception('`'.$sPath.'`: isn\'t a file.');
return;
}
$rFD = fopen($sPath, 'r' );
if ($rFD) {
$xData = chunk_split(base64_encode(fread($rFD, filesize($sPath)+1)));
fclose($rFD);
} else {
throw new Exception('`'.$sPath.'`: Invalid file.');
}
return $xData;
}
/**
* Ajoute un fichier au mail. Exemple, les images pour les emails en HTML.
*
* @param string $sPath
* @param string $sTypeMIME
* @param string $xData
*/
public function appendInline($sPath, $sTypeMIME, $sID = null, $xData = null) {
//TODO
return;
$sPath = basename($sPath);
if ($sID === null) {
$sID = '<'.$sPath.'@'.'sdqsdqsdqsdqsd'.'>';
}
$sMessage = implode(Mail::LINE_SEPARATOR, array(
'Content-Type: '.$sTypeMIME.'; name="'.$sPath.'"',
'Content-Transfer-Encoding: base64',
'Content-Disposition: inline; filename="'.$sPath.'"',
'Content-ID: '. $sID
)
);
$sMessage .= Mail::LINE_SEPARATOR;
$sMessage .= (($xData === null)? self::getChunkedFileContent($sPath) : chunk_split(base64_encode($xData)));
$sMessage .= Mail::LINE_SEPARATOR;
$this->aInlines[] = $sMessage;
}
/**
* Ajoute une piece jointe
*
* @param string $sPath
* @param string $sTypeMIME
* @param string $xData
*/
public function appendAttachement($sPath, $sTypeMIME, $xData = null) {
//TODO check parameters
$sMessage = implode(Mail::LINE_SEPARATOR, array(
'Content-Type: '.$sTypeMIME.'; name="'.basename($sPath).'"',
'Content-Transfer-Encoding: base64',
'Content-Disposition: attachment; filename="'.basename($sPath).'"'
)
);
$sMessage .= Mail::LINE_SEPARATOR.Mail::LINE_SEPARATOR;
$sMessage .= (($xData === null)? Mail::getChunkedFileContent($sPath) : chunk_split(base64_encode($xData)));
$sMessage .= Mail::LINE_SEPARATOR;
$this->aAttachements[] = $sMessage;
}
/**
* Construit la string pour le header de l'email
*
* @return string
*/
private function buildHeaders() {
$aHeaders = array(
'X-Mailer: FidesioMailer',
'Content-Language: en',
'MIME-Version: 1.0',
'Content-Type: multipart/mixed; boundary="'.$this->sBoundary.'"'
);
foreach ($this->aHeaders as $sName => $aValues) {
if ($sName != 'To' && $sName != 'Subject') {
foreach ($aValues as $aValue) {
$aHeaders[] = $sName.': '.$aValue;
}
}
}
return implode(Mail::LINE_SEPARATOR, $aHeaders);
}
private static function checkHeader($sName, $sValue){
switch($sName){
case 'To':
case 'Bcc':
case 'Cc':
return self::checkMailList($sValue);
break;
default:
break;
}
return true;
}
/**
*/
private static function checkMailList($sMail) {
if (mb_ereg('^([-a-zA-Z0-9_.]+@[-a-zA-Z0-9]+\.[-a-zA-Z0-9]+)(;([-a-zA-Z0-9_.]+@[-a-zA-Z0-9]+\.[-a-zA-Z0-9]+))*$', $sMail) == false)
return false;
else
return true;
}
/**
* Control les headers.
*/
private function checkHeaders() {
if (!isset($this->aHeaders['Subject'])) {
throw new Exception('Invalid Subject.');
}
if (!isset($this->aHeaders['To'])) {
throw new Exception('Invalid To.');
}
}
/**
* Envoi le(s) email(s).
*/
public function send() {
$this->checkHeaders();
$this->sBoundary = self::createBoundary();
$sBody = $this->buildBody();
$sSubject = implode(' ', $this->aHeaders['Subject']);
$sSubject = '=?ISO-8859-1?B?'.base64_encode($sSubject).'?=';
$sHeaders = $this->buildHeaders();
$nbr = 0;
$nbr_tot = count($this->aHeaders['To']);
foreach ($this->aHeaders['To'] as $sTo) {
$res = mail($sTo, $sSubject, $sBody, $sHeaders);
if($res)
$nbr += 1;
}
if($nbr){
$s = $nbr>1 ? "s" : "";
Ba::trace("$nbr message$s envoyé$s.", "s");
echo 'envoyer';
}
else {
Ba::trace("Problème ! Message pas envoyé.", "e");
echo 'non envoyer';
}
return $nbr;
}
private static function QPE($chaine){
if(function_exists("quoted_printable_encode"))
return quoted_printable_encode($chaine);
elseif(function_exists("imap_8bit"))
return imap_8bit($chaine);
else{
Ba::trace("Vous devez activer l'extension php_imap ou bien utiliser une version php égale ou supérieure à 5.3");
return "";
}
}
} |
Partager