30-07-2008, 23:35
|
|
|
חבר מתאריך: 14.04.06
הודעות: 331
|
|
מחלקה ב PHP לשליחת אימייל כולל תמיכה בHTML וצירוף קבצים
לא ראיתי נושא שמאפשר צירוף של מחלקות שלמות לביצוע פעולות מסויימות וחבל. אז לכל מי שמעוניין זה משהו שנכתב על ידי ומשומש בצורה קבוע לצורך שליחת אימיילים בקלות ויעילות.
תמיכה ב HTML, קידוד, ואפשרות לצרף קבצים.
אם משהו מעוניין לערוך משהו הוא מוזמן.
קוד PHP:
<?php
/*--------------------------------------- // Class Email Version 1.0 // Written by vadim gavrilov (vadimg88@gmail.com) // $ Date started june 28th 2008 // $ Revision 122 // Contact: vadimg88@gmail.com ----------------------------------------*/
/*
USAGE:
Simple email send:
require_once('class.email.php'); $email = new email;
$email->from = ''; // Senders email address $email->to = ''; // who will get the email $email->msg = ''; // email message (html enabled)
$email->sendmail(); // send email!
ADDING ATTACHMENTS:
$email->add_attachment(); // 3 arguments 1. the actuall attachemnt file 2. the attachment name 3. the type
TO ADD MULTIPLE ATTACHMENTS JUST COPY AND PASTE THE LINE ABOVE MULTIPLE TIMES, EX:
$email->add_attachment('data','name','type'); $email->add_attachment('data','name','type'); $email->add_attachment('data','name','type');
*/
/** * Main Email Class. * * This class holds all the email related functions. * * @author vadim gavrilov * @version 1.0 */
class email { # Global Vars var $mail_headers = ""; var $bcc = array(); var $to = ""; var $from = ""; var $msg = ""; var $crlf; var $header = ""; var $footer = ""; var $boundry = ""; var $parts = array(); var $multipart = "";
// Editable Vars var $email_from = 'youremail@domain.com'; // default email from var $char_set = "windows-1255"; // default email char' set function email() { $this->email_init(); }
function email_init() { $this->header = ""; $this->footer = ""; $this->boundry = 'PHP' . md5(uniqid(time())); $this->from = $this->email_from; }
function build_headers() {
$this->mail_headers .= "MIME-Version: 1.0\n"; $this->mail_headers .= "Content-type: text/html; charset=\"".$this->char_set."\"\n";
$this->mail_headers .= "From: <".$this->from.">\n";
if ( count( $this->bcc ) > 1 ) { $this->mail_headers .= "Bcc: ".implode( "," , $this->bcc ) . "\n"; }
if ( $this->to ) { $this->mail_headers .= "To: ".$this->to."\n"; } $this->mail_headers .= "Subject: ".$this->subject."\n";
//----------------------------------------- // we're not spam, really! //-----------------------------------------
$this->mail_headers .= "Return-Path: ".$this->from."\n"; $this->mail_headers .= "X-Priority: 3\n"; $this->mail_headers .= "X-Mailer: Rent Center Mailer\n";
//----------------------------------------- // Count.. oh you get the idea //-----------------------------------------
if ( count ($this->parts) > 0 ) { $this->mail_headers .= "MIME-Version: 1.0\n"; $this->mail_headers .= "Content-Type: multipart/mixed;\n\tboundary=\"".$this->boundry."\"\n\nThis is a MIME encoded message.\n\n--".$this->boundry; $this->mail_headers .= "\nContent-Type: text/html;\n\tcharset=\"".$this->char_set."\"\nContent-Transfer-Encoding: quoted-printable\n\n".$this->message."\n\n--".$this->boundry;
$this->mail_headers .= $this->build_multipart();
$this->message = ""; }
}
function sendmail() {
//----------------------------------------- // Wipe ya face //-----------------------------------------
$this->to = preg_replace( "/[ \t]+/" , "" , $this->to ); $this->from = preg_replace( "/[ \t]+/" , "" , $this->from );
$this->to = preg_replace( "/,,/" , "," , $this->to ); $this->from = preg_replace( "/,,/" , "," , $this->from );
$this->to = preg_replace( "#\#\[\]'\"\(\):;/\$!£%\^&\*\{\}#" , "", $this->to ); $this->from = preg_replace( "#\#\[\]'\"\(\):;/\$!£%\^&\*\{\}#" , "", $this->from);
//----------------------------------------- // Build headers //-----------------------------------------
$this->build_headers();
//----------------------------------------- // Lets go.. //-----------------------------------------
if ( @mail( $this->to, $this->subject, $this->message, $this->mail_headers ) ) { return TRUE; }
# Reset vars $this->to = ""; $this->from = ""; $this->message = ""; $this->subject = ""; $this->mail_headers = "";
$this->email_init();
}
function add_attachment($data = "", $name = "", $ctype='application/octet-stream') {
$this->parts[] = array( 'ctype' => $ctype, 'data' => $data, 'encode' => 'base64', 'name' => $name ); }
function encode_attachment($part) {
$msg = chunk_split(base64_encode($part['data']));
return "Content-Type: ".$part['ctype']. ($part['name'] ? ";\n\tname =\"".$part['name']."\"" : ""). "\nContent-Transfer-Encoding: ".$part['encode']."\nContent-Disposition: attachment;\n\tfilename=\"".$part['name']."\"\n\n".$msg."\n";
}
function build_multipart() {
$multipart = "";
for ($i = sizeof($this->parts) - 1 ; $i >= 0 ; $i--) { $multipart .= "\n".$this->encode_attachment($this->parts[$i]) . "--".$this->boundry; }
return $multipart . "--\n";
}
}
?>
ואדים.
|