Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: MIME/problem z '\r\n'
Forum PHP.pl > Forum > PHP
Ozzy
Mam taki kod: (co niektórzy skojarzą pewne podobieństwa z kodem z książki Sterling'a H. winksmiley.jpg)

[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--


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--


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 sad.gif
Ozzy
dzieki wielkie...ehhh:P
Ozzy
Headersy i body muszą być przekazane do mail() osobno.

Poprawny kod:
[php:1:b88726d31b]<?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 buildHeaders() {

$headers = 'From: '. $this->from . n();
$headers.= 'Reply-To: '. $this->from . n();
$headers.= 'X-Mailer: php/' . phpversion() . n();
$headers.= 'X-Priority: 1'. n();
$headers.= 'MIME-Version: 1.0'. n();
$headers.= 'Content-type: multipart/mixed; boundary="'. $this->boundary .'"'. n();
$headers.= 'This is a MIME encoded message.'. n(2);

return $headers;

}


private function buildMessage() {


$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(), $this->buildHeaders());

}


}


?>[/php:1:b88726d31b]
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.