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
| <?php public function sending()
{
if (empty($this->to) and empty($this->cc) and empty($this->bcc)) throw new DomainException("No valid recipient");
$attached = false;
if (!empty($this->files)) {
$attached = true;
$attachements = [];
foreach ($this->files as $key=>$value) {
$attachements[$key] = $this->attachement($value['tmp_name']);
}
}
// 'Boundary' fixing
$boundary = "-----=".md5(rand()); // Sert à séparer les différentes parties du courriel
$boundaryAlt= "-----=".md5(rand()); // Sert à séparer les différentes parties de la partie texte
// Assembly of addresses
if (!empty($this->to)) $toLine = implode(', ', $this->to);
// headers definition
$headers = [];
$headers[] = 'From: ' . $this->fromLine;
if ($this->replyAddress) $headers[] = 'Reply-to: ' . $this->addressConcat($this->replyAddress, $this->replyName);
if (!empty($this->cc)) $headers[] = 'Cc: ' . implode(', ', $this->cc );
if (!empty($this->bcc)) $headers[] = 'Bcc: ' . implode(', ', $this->bcc );
$headers[] = 'MIME-Version: 1.0';
$headers[] = $attached ? 'Content-Type: multipart/mixed;' : 'Content-Type: multipart/alternative;';
$headers[] = " boundary=\"$boundary\"";
$header = implode(self::CRLF, $headers) . self::CRLF;
// Message body
if (! $attached) {
// opening
$message = [];
$message[] = self::CRLF . "--" . $boundary;
// adding plain text
$message[] = "Content-Type: text/plain; charset=\"ISO-8859-1\"";
$message[] = "Content-Transfer-Encoding: 8bit";
$message[] = self::CRLF . $this->plainText;
// boundary
$message[] = self::CRLF . "--" . $boundary;
// adding html text
$message[] = "Content-Type: text/html; charset=\"ISO-8859-1\"";
$message[] = "Content-Transfer-Encoding: 8bit";
$message[] = self::CRLF . $this->htmlText;
// Boundaries double closing (one for each message format)
$message[] = self::CRLF . "--" . $boundary;
$message[] = self::CRLF . "--" . $boundary;
$message = implode(self::CRLF, $message);
}
else {
// opening
$message = [];
$message[] = self::CRLF . "--" . $boundary;
$message[] = 'Content-Type: multipart/alternative;';
$message[] = "boundary=\"$boundaryAlt\"";
$message[] = self::CRLF . "--" . $boundaryAlt;
// adding plain text
$message[] = "Content-Type: text/plain; charset=\"ISO-8859-1\"";
$message[] = "Content-Transfer-Encoding: 8bit";
$message[] = self::CRLF . $this->plainText;
// boundary
$message[] = self::CRLF . "--" . $boundaryAlt;
// adding html text
$message[] = "Content-Type: text/html; charset=\"ISO-8859-1\"";
$message[] = "Content-Transfer-Encoding: 8bit";
$message[] = self::CRLF . $this->htmlText;
// closing alternative boundary
$message[] = self::CRLF . "--" . $boundaryAlt;
// adding attachements
foreach ($this->files as $name=>$file) {
// opening main boundary
$message[] = self::CRLF . "--" . $boundary;
// declaring each attachement
$message[] = "Content-type: " . $file['type'] . "; name=\"$name\"";
$message[] = "Content-Transfer-Encoding: base64";
$message[] = "Content-Disposition: attachement; filename=\"$name\"";
$message[] = self::CRLF . $attachements[$name] . self::CRLF;
}
// closing main boundary
$message[] = self::CRLF . "--" . $boundary . "--";
$message = implode(self::CRLF, $message);
}
// Debugging
//var_dump(__method__, $message);
//$test = $toLine."\n\n".$this->strSubject."\n\n".$message."\n\n".$header;
//file_put_contents('_test.txt', $test);
// Sending
$check = false;
if ($this->fromAddress) {
//var_dump($toLine, $this->strSubject, $message, $header);
$check = mail($toLine, $this->strSubject, $message, $header);
}
return $check;
}
?> |
Partager