
[php:1:582ef6d3db]<?php
class MIME {
private $attachments = array();
private $to;
private $from;
private $subject;
private $body;
private $boundary;
public function MIME($to = '', $from = '', $subject = '', $body = '') {
$this->to = $to;
$this->from = $from;
$this->subject = $subject;
$this->body = $body;
$this->boundary = $this->buildBoundary();
}
public function attachment($name = '', $contents = '', $type = 'application/octet-stream', $encoding = 'base64') {
$this->attachments[] = array(
'filename' => $name,
'type' => $type,
'encoding' => $encoding,
'data' => $contents
);
}
private function buildBoundary() {
mt_srand((double)microtime()*1000000);
return 'b' . md5(uniqid(mt_rand())) . getmypid();
}
private function buildMessage() {
$msg = 'From: '. $this->from . n();
$msg.= 'MIME-Version: 1.0'. n();
$msg.= 'Content-type: multipart/mixed; boundary="'. $this->boundary .'"'. n();
$msg.= 'This is a MIME encoded message.'. n(2);
$msg.= '--'. $this->boundary . n();
$msg.= 'Content-Type: text/plain; charset = "iso-8859-2"'. n();
$msg.= 'Content-Transfer-Encoding: 8bit'. n(2);
$msg.= $this->body . n(2);
$msg.= '--'. $this->boundary;
foreach ($this->attachments as $attachment) {
$attachment['data'] = chunk_split(base64_encode($attachment['data']));
$filename = $attachment['filename'] ? $attachment['filename']: '';
$data = 'Content-Type: '. $attachment['type']
. '; name="'. $filename .'"'
. n()
. 'Content-Transfer-Encoding: '. $attachment['encoding'] . n()
. 'Content-Disposition: attachment; filename="'. $filename .'"'
. n(2)
. $attachment['data']
. n(2);
$msg.= n() . $data .'--'. $this->boundary;
}
$msg.= '--'. n();
return $msg;
}
public function send() {
return @mail($this->to, $this->subject, '', $this->buildMessage());
}
}
?>[/php:1:582ef6d3db]
użycie:
[php:1:582ef6d3db]<?php
$mm = new MIME('ozzy@xxx.pl', 'test@xxx.pl', 'test', 'treść');
$mm->attachment('plik.jpg', 'kjiphrepguh', 'image/jpeg');
$mm->send();
?>[/php:1:582ef6d3db]
funkcja n() zwraca chr(13) . chr(10), czyli rn (i na pewno robi to dobrze;)
funkcja buildMessage() zwraca:
Cytat
From: test@xxx.pl
MIME-Version: 1.0
Content-type: multipart/mixed; boundary="ba0ddc420ff90f26850ce62e6797c13863448"
This is a MIME encoded message.
--ba0ddc420ff90f26850ce62e6797c13863448
Content-Type: text/plain; charset = "iso-8859-2"
Content-Transfer-Encoding: 8bit
treść
--ba0ddc420ff90f26850ce62e6797c13863448
Content-Type: image/jpeg; name="plik.jpg"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="plik.jpg"
a2ppcGhyZXBndWg=
--ba0ddc420ff90f26850ce62e6797c13863448--
MIME-Version: 1.0
Content-type: multipart/mixed; boundary="ba0ddc420ff90f26850ce62e6797c13863448"
This is a MIME encoded message.
--ba0ddc420ff90f26850ce62e6797c13863448
Content-Type: text/plain; charset = "iso-8859-2"
Content-Transfer-Encoding: 8bit
treść
--ba0ddc420ff90f26850ce62e6797c13863448
Content-Type: image/jpeg; name="plik.jpg"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="plik.jpg"
a2ppcGhyZXBndWg=
--ba0ddc420ff90f26850ce62e6797c13863448--
I niby wszystko byłoby ok...jednak po przekazaniu wartości zwracanej przez buildMessage() do funkcji mail() znaki końca linii jakby 'znikają'.
Kod, który otrzymuję mailem wygląda tak:
Cytat
From: test@xxx.pl
MIME-Version: 1.0
Content-type: multipart/mixed; boundary="b36efa5f4dea771d4dcce15275e7212123976"
This is a MIME encoded message.
--b36efa5f4dea771d4dcce15275e7212123976
Content-Type: text/plain; charset = "iso-8859-2"
Content-Transfer-Encoding: 8bit
treść
--b36efa5f4dea771d4dcce15275e7212123976
Content-Type: image/jpeg; name="plik.jpg"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="plik.jpg"
a2ppcGhyZXBndWg=
--b36efa5f4dea771d4dcce15275e7212123976--
MIME-Version: 1.0
Content-type: multipart/mixed; boundary="b36efa5f4dea771d4dcce15275e7212123976"
This is a MIME encoded message.
--b36efa5f4dea771d4dcce15275e7212123976
Content-Type: text/plain; charset = "iso-8859-2"
Content-Transfer-Encoding: 8bit
treść
--b36efa5f4dea771d4dcce15275e7212123976
Content-Type: image/jpeg; name="plik.jpg"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="plik.jpg"
a2ppcGhyZXBndWg=
--b36efa5f4dea771d4dcce15275e7212123976--
czyli już bez odstępów:(
Byłbym gotów winić za to serwer smtp, jednak przy korzystaniu z klasy phpmailer problem ten nie występuje.
Ma ktoś jakiś pomysł? Mi się już wyczerpały
