Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.67 KB | None | 0 0
  1. <?php
  2.     class SMTPMail {
  3.         //Member Variables
  4.         public $host, $username, $password, $from, $to, $subject, $html_body, $plaintext_body, $status, $attachment_name;
  5.         private $smtp, $mail, $body, $headers;
  6.        
  7.         //Constructor
  8.         public function __construct( $host, $username, $password, $from ) {
  9.             //Import PEAR SMTP Library
  10.             require_once("Mail.php");
  11.             require_once("Mail/mime.php");
  12.            
  13.             //Set Member Variables from Parameters
  14.             $this->host = $host;
  15.             $this->username = $username;
  16.             $this->password = $password;
  17.             $this->from = $from;
  18.            
  19.             //Create SMTP Connection
  20.             $this->smtp = Mail::factory('smtp',
  21.                 array ('host' => $this->host,
  22.                     'auth' => true,
  23.                     'username' => $this->username,
  24.                     'password' => $this->password)
  25.                 );
  26.                
  27.             //Return True For Constructor Tests
  28.             return true;
  29.         }
  30.        
  31.         //Send Mail
  32.         public function sendMail( $to, $subject, $html_body, $plaintext_body = "", $attachment_name="" ) {
  33.             //Set Member Variables from Parameters
  34.             $this->to = $to;
  35.             $this->subject = $subject;
  36.             $this->html_body = $html_body;
  37.             $this->plaintext_body = $plaintext_body;
  38.             $this->attachment = $attachment_name;
  39.             var_dump($this->attachment);
  40.             //Build Headers
  41.             $headers = array('From' => $this->from,
  42.                 'To' => $this->to,
  43.                 'Subject' => $this->subject
  44.             );
  45.            
  46.        
  47.             //Set MIME Types and Build Message
  48.             $mime = new Mail_mime();
  49.             $mime->setTXTBody( $this->plaintext_body );
  50.             $mime->setHTMLBody( $this->html_body );
  51.             $mime->addAttachment( $this->attachment,'application/octet-stream' );
  52.             $this->body = $mime->get();
  53.             $this->headers = $mime->headers($headers);
  54.        
  55.  
  56.        
  57.             //Send the Message
  58.             $this->mail = $this->smtp->send($this->to, $this->headers, $this->body);
  59.  
  60.             //Check for Error Message, Return True/False if E-mail Sent or Not
  61.             if (PEAR::isError($mail)) {
  62.                 $this->status = "FAIL:Error Code: ".$mail->getMessage();
  63.                 return false;
  64.             }
  65.             else {
  66.                 $this->status = "OK:Sent";
  67.                 //$this->status = "OK:".$mail->getMessage();
  68.                 return true;
  69.             }
  70.         }
  71.        
  72.         //Accessor Methods
  73.         public function getStatus() {
  74.             //Return E-Mail Status Message
  75.             return $this->status;
  76.         }
  77.        
  78.         public function getMail() {
  79.             //Return Mail Object Created by PEAR Mail/PEAR Mime
  80.             return $this->mail();
  81.         }
  82.        
  83.         public function getBody() {
  84.             //Return Message Object Created by PEAR Mail/PEAR Mime
  85.             return $this->body;
  86.         }
  87.        
  88.         public function getHeaders() {
  89.             //Return Message Headers Object Created by PEAR Mail/PEAR Mime
  90.             return $this->headers;
  91.         }
  92.         public function showAttachment() {
  93.             //Return Message Headers Object Created by PEAR Mail/PEAR Mime
  94.             return $this->attachment;
  95.         }
  96.     }
  97. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement