phobia
Terugkerende gebruiker
- Lid geworden
- 4 sep 2006
- Berichten
- 1.777
Als ik geen attachment mee stuur zie ik de html tekst,
Wil ik een bijlage mee sturen, dan is de tekst verdwenen en de bijlage is onbruikbaar!
Class:
Wil ik een bijlage mee sturen, dan is de tekst verdwenen en de bijlage is onbruikbaar!
PHP:
$mail = New Mail("Ikke", "me@somewhere.nl");
$mail->HtmlLayout = true;
$mail->to = "someone@there.com";
$mail->subject = "Test mail";
$mail->attachment("pad_naar_bestand");
$mail->send());
Class:
PHP:
/**
* Description of Mail
*
* @author Phobia
*/
class Mail {
//put your code here
private $_tags = Null;
private $_template = Null;
private $fromName = Null;
private $fromEmail = Null;
private $replyName = "NoReply";
private $replyEmail = "noreply@nowhere.com";
private $attachment = '';
private $attachment_filename = '';
private $_uid = Null;
public $to = False;
public $cc = False;
public $bcc = False;
public $subject = Null;
public $errormail = False;
public $HtmlLayout = False;
public function __construct($YourName, $YourEmail) {
if (!empty($YourName) && !empty($YourEmail)) {
$this->fromName = $YourName;
$this->fromEmail = $YourEmail;
$this->_uid = md5(uniqid(time()));
} else {
throw new Exception("Mail error: You must provide an email and name");
}
}
public function attachment($attachment = '', $attachment_filename = '') {
$this->attachment = $attachment;
$this->attachment_filename = $attachment_filename;
}
public function send() {
if ($this->HtmlLayout == true) {
echo $headers = $this->headers();
$send = mail($this->to, $this->subject, $this->_template, $headers);
} else {
$header = "From: " . ($this->fromName) . " <" . ($this->fromEmail) . ">\r\n";
$header .= "Reply-To: " . ($this->replyName) . "\r\n";
$send = mail($this->to, $this->subject, $this->_template, $header);
}
return $send;
}
public function template($filepath) {
if (is_readable($filepath)) {
$this->_template = file_get_contents($filepath);
$this->striptags();
}
}
public function reply($ReplyName = false, $ReplyEmail = false) {
if ($ReplyName)
$this->replyName = $ReplyName;
if ($ReplyEmail)
$this->replyEmail = $ReplyEmail;
}
// Scrape the placeholders out of the template
private function striptags() {
if ($this->_template != Null) {
$out = null;
preg_match_all('|{+{+[A-Za-z0-9\\:\_]+}+}|u', $this->_template, $out, PREG_PATTERN_ORDER);
$this->_tags = array_unique($out[0]);
}
}
private function headers() {
$headers = 'From: ' . $this->fromName . ' <' . $this->fromEmail . '>' . PHP_EOL;
$headers .= 'Reply-To: ' . $this->replyName . ' <' . $this->replyEmail . '>' . PHP_EOL;
$headers .= 'Return-Path: Mail-Error <' . (($this->errormail) ? $this->errormail : $this->fromEmail) . '>' . PHP_EOL;
$headers .= ($this->bcc) ? 'Cc: ' . $this->cc . PHP_EOL : '';
$headers .= ($this->bcc) ? 'Bcc: ' . $this->bcc . PHP_EOL : '';
$headers .= 'X-Mailer: PHP/' . phpversion() . PHP_EOL;
$headers .= 'X-Priority: Normal' . PHP_EOL;
if ($this->HtmlLayout == true) {
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $this->_uid . "\"\r\n\r\n";
$headers .= "This is a multi-part message in MIME format.\r\n";
$headers .= "--" . $this->_uid . "\r\n";
$headers .= "Content-type:text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$headers .= "<b>Test</b><hr/>\r\n\r\n";// hier komt de html make up
//$headers .= "--" . $this->_uid . "\r\n";
}
if (!empty($this->attachment) && $this->HtmlLayout == True) {
$filename = empty($this->attachment_filename) ? basename($this->attachment) : $this->attachment_filename;
$path = dirname($this->attachment);
$file = $path . '\\' . $filename;
//echo (is_readable($file)) ? "readable" : "Not readable";
$mine = $this->mime_content_type($file);
$file_size = filesize($file);
$handle = fopen($file, "r");
$filecontent = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($filecontent));
$headers .= "--" . $this->_uid . PHP_EOL;
$headers .= "Content-Type: ".$mine."; name=\"" . $filename . "\"" . PHP_EOL; // use diff. tyoes here
$headers .= "Content-Transfer-Encoding: base64" . PHP_EOL;
$headers .= "Content-Disposition: attachment; filename=\"" . $filename . "\"" . PHP_EOL . PHP_EOL;
$headers .= $content . PHP_EOL . PHP_EOL;
$headers .= "--" . $this->_uid . "--";
}
return $headers;
}
private function mime_content_type($filename) {
$mime_types = array(
'txt' => 'text/plain',
'htm' => 'text/html',
'html' => 'text/html',
'php' => 'text/html',
'css' => 'text/css',
'js' => 'application/javascript',
'json' => 'application/json',
'xml' => 'application/xml',
'swf' => 'application/x-shockwave-flash',
'flv' => 'video/x-flv',
// images
'png' => 'image/png',
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'ico' => 'image/vnd.microsoft.icon',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'svg' => 'image/svg+xml',
'svgz' => 'image/svg+xml',
// archives
'zip' => 'application/zip',
'rar' => 'application/x-rar-compressed',
'exe' => 'application/x-msdownload',
'msi' => 'application/x-msdownload',
'cab' => 'application/vnd.ms-cab-compressed',
// audio/video
'mp3' => 'audio/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
// adobe
'pdf' => 'application/pdf',
'psd' => 'image/vnd.adobe.photoshop',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
// ms office
'doc' => 'application/msword',
'rtf' => 'application/rtf',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
// open office
'odt' => 'application/vnd.oasis.opendocument.text',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
);
$ext = strtolower(array_pop(explode('.',$filename)));
if (array_key_exists($ext, $mime_types)) {
return $mime_types[$ext];
}
elseif (function_exists('finfo_open')) {
$finfo = finfo_open(FILEINFO_MIME);
$mimetype = finfo_file($finfo, $filename);
finfo_close($finfo);
return $mimetype;
}
else {
return 'application/octet-stream';
}
}
//end of Class
}