Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Mail {
- private $connection;
- public $server;
- public $port = 25;
- public $timeout = 60;
- private $newline = "\r\n";
- private $charset = "utf-8";
- private $format = "plain";
- public $username;
- public $password;
- private $headers;
- private $from;
- private $to = array();
- private $cc = array();
- private $bcc = array();
- public $subject;
- public $message;
- public $debug = array();
- public function __construct($server = null, $port = 25, $username = null, $password = null){
- if(!is_null($server)){
- $this->server = $server;
- $this->port = $port;
- $this->username = $username;
- $this->password = $password;
- }
- set_time_limit(240);
- }
- public function initialize($config = array()){
- }
- public function from($email, $name = null){
- $this->from = $email;
- if(is_null($name)){
- $this->headers['From'] = "<$email>";
- }
- else{
- $this->headers['From'] = "$name <$email>";
- }
- }
- public function reply_to($email){
- if(is_null($name)){
- $this->headers['Reply-To'] = "<$email>";
- }
- else{
- $this->headers['Reply-To'] = "$name <$email>";
- }
- }
- public function to($email){
- if(is_array($email)){
- $this->to = array_merge($this->to, $email);
- }
- else{
- array_push($this->to, $email);
- }
- }
- public function cc($email){
- if(is_array($email)){
- $this->cc = array_merge($this->cc, $email);
- }
- else{
- array_push($this->cc, $email);
- }
- }
- public function bcc($email){
- if(is_array($email)){
- $this->bcc = array_merge($this->bcc, $email);
- }
- else{
- array_push($this->bcc, $email);
- }
- }
- public function subject($subject){
- $this->subject = $subject;
- }
- public function message($message){
- $this->message = $message;
- }
- private function compile_message(){
- //plain
- $headers = "";
- $headers .= "MIME-Version: 1.0" . $this->newline;
- foreach ($this->headers as $key => $val) {
- $headers .= $key . ": " . $val . $this->newline;
- }
- $headers .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
- $headers .= "Content-Transfer-Encoding: 8bit" . $this->newline;
- return $headers . $this->newline . $this->newline .$this->message;
- }
- public function command($command, $nls = 1){
- $newlines = "";
- for ($i = 0; $i < $nls; $i++) {
- $newlines .= $this->newline;
- }
- fwrite($this->connection, $command . $newlines);
- array_push($this->debug, "Command: $command");
- array_push($this->debug, "Response: ".$this->response());
- $command = htmlentities($command);
- echo "<pre>Command: $command</pre>";
- }
- public function response(){
- $data = "";
- while ($str = fgets($this->connection, 512)) {
- $data .= $str;
- if (substr($str, 3, 1) == " ") {
- break;
- }
- }
- echo "<pre>$data</pre>";
- return $data;
- }
- public function send(){
- $success = false;
- $this->connection = fsockopen($this->server, $this->port, $errno, $errstr, $this->timeout);
- $this->command('EHLO '.$this->server);
- $this->command('AUTH LOGIN');
- $this->command(base64_encode($this->username));
- $this->command(base64_encode($this->password));
- if(!empty($this->from)){
- $this->command('MAIL FROM: <' . $this->from .'>');
- }
- if(!empty($this->to)){
- foreach($this->to as $to){
- $this->command('RCPT TO: <' . $to . '>');
- }
- }
- if(!empty($this->cc)){
- foreach($this->cc as $cc){
- $this->command('RCPT TO: <' . $cc . '>');
- }
- }
- if(!empty($this->bcc)){
- foreach($this->bcc as $bcc){
- $this->command('RCPT TO: <' . $bcc . '>');
- }
- }
- $this->command('DATA');
- $this->command('From: ' . $this->from);
- $this->command('To: ' . implode(',', $this->to));
- $this->command('Reply-To: ' . $this->from);
- $this->command('Subject: '.$this->subject);
- fflush($this->connection);
- if($this->format != "html"){
- $message = $this->compile_message();
- $this->command($message, 1);
- $response = $this->command('.');
- }
- if(substr($response, 0, 3) == 250){
- $success = true;
- }
- $this->command('QUIT');
- fclose($this->connection);
- return $success;
- }
- public function debug(){
- return $this->debug;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement