[RESOLVED] Extends trouble with construct override .......

liunx

Guest
Hi.
I've to extend a Mail class:
This is the snippet:

<?php
class User_Mail extends Mail {
const MSG_OK = 'An email has sent you. Thanks.';
const MSG_ERROR = 'The server is too busy. Please try later.';
public function __construct($to, $subject, $fromName="", $fromMail="") {
parent::__construct($to, $subject, $fromName, $fromMail);
}
public function setBodyMsg($username,$password)
{
$bodyMsg = '<p>Username :'.$username.'</p>';
$bodyMsg .= '<p>Password :'.$password.'</p>';
$this->setBodyHtml($bodyMsg);
$this->setPriority(AbstractMail::HIGH_PRIORITY);
if($this->send())
{
$outPut = self::MSG_OK;
}
else
{
$outPut = self::MSG_ERROR;
}
return $outPut;
}
?>

Well I'd like to assign to the subject parameter
a const (i.e This is an email from .........) for a
easier script configuration but I'm not able to
do it ;(
Do you have any tip ?
Take care.This is your extension, or the class you're extending? If it's the latter:

class whisher06_Mail extends User_Mail {
const MSG_SUBJECT = "This is an email from ";
public function __construct($to, $fromName="", $fromMail="") {
parent::__construct($to, MSG_SUBJECT.$fromName, $fromName, $fromMail);
}
}As usual thanks a lot buddy :)
I had to extend this very good
class:

<?php
/**
* Abstract class used for email sender implementation classes
*
* @packagemail
* @authorgustavo.gomes
* @copyright2006
*/
abstract class AbstractMail {

const CRLF = "\n";

const HIGH_PRIORITY = 2;
const NORMAL_PRIORITY = 3;
const LOW_PRIORITY = 4;

protected $to = array();
protected $fromName;
protected $fromMail;
protected $subject;
protected $contentType;
protected $charset;
protected $priority = 2;
protected $headers = array();
protected $body;

public function __construct($to, $subject, $fromName="", $fromMail="") {
$this->to[] = $to;
$this->subject = $subject;
$this->fromName = $fromName;
$this->fromMail = $fromMail;
$this->init();
}

private function init() {
if ($this->fromName != "" && $this->fromMail != "") {
$this->addHeader("From: ".$this->fromName." <".$this->fromMail.">");
$this->addHeader("Reply-To: ".$this->fromName." <".$this->fromMail.">");
} else if ($this->fromName == "" && $this->fromMail != "") {
$this->addHeader("From: ".$this->fromMail);
$this->addHeader("Reply-To: ".$this->fromMail);
}
}

public function getPriority() {
return $this->priority;
}

public function setPriority($priority) {
$this->priority = $priority;
}

public function getContentType() {
return $this->contentType;
}

public function getCharset() {
return $this->charset;
}

public function addTo($mail) {
$this->to[] = $mail;
}

public function addCC($mail) {
$this->addHeader("CC:".$mail);
}

public function addBCC($mail) {
$this->addHeader("BCC:".$mail);
}

public function addHeader($header) {
$this->headers[] = $header.self::CRLF;
}

protected function buildTo() {
return implode(", ",$this->to);
}

protected function buildHeaders() {
$headers = "";
if (count($this->headers) > 0) {
for ($i = 0;$i < count($this->headers);$i++)
$headers .= $this->headers[$i];
}
return $headers;
}

protected function createMessageHeaders($contentType, $encode="") {
$out = "";
if ($encode != "")
$out .= "Content-type:".$contentType."; charset=".$encode.self::CRLF;
else
$out .= "Content-type:".$contentType.";".self::CRLF;
return $out;
}

public static function validateAddress($mailadresse) {
if (!preg_match("/[a-z0-9_-]+(\.[a-z0-9_-]+)*@([0-9a-z][0-9a-z-]*[0-9a-z]\.)+([a-z]{2,4})/i",$mailadresse))
return false;
return true;
}

public function resetHeaders() {
$this->headers = array();
$this->init();
}

public abstract function setBodyHtml($html, $charset="iso-8859-1");

public abstract function setHtml($html, $charset="iso-8859-1");

public abstract function setBodyText($text);

public abstract function send();

}
/**
* Class to send simple emails in Text and HTML formats
*
* @packagemail
* @authorgustavo.gomes
* @copyright2006
*/
class Mail extends AbstractMail {

public function __construct($to, $subject, $fromName="", $fromMail="") {
parent::__construct($to, $subject, $fromName, $fromMail);
}

public function setBodyHtml($html, $charset="iso-8859-1") {
$this->contentType = "text/html";
$this->charset = $charset;
$this->body = "";
$this->body .= "<html><head>";
$this->body .= "<meta http-equiv=Content-Type content=\"text/html; charset=".$charset."\">";
$this->body .= "</head><body>";
$this->body .= nl2br($html)."";
$this->body .= "</body></html>";
}

public function setHtml($html, $charset="iso-8859-1") {
$this->contentType = "text/html";
$this->charset = $charset;
$this->body = nl2br($html)."";
}

public function setBodyText($text) {
$this->contentType = "text/plain";
$this->charset = "";
$this->body = $text;
}

public function send() {
$this->addHeader("MIME-Version: 1.0");
$this->addHeader("X-Mailer: RLSP Mailer");
$this->addHeader("X-Priority: ".$this->priority);
$this->addHeader($this->createMessageHeaders($this->contentType, $this->charset));
$headers = $this->buildHeaders();
return mail($this->buildTo(),
$this->subject,
$this->body,
$headers);
}
}
?>



I've followed your example here
the snippet:

<?php
class User_Mail extends Mail {
const MSG_OK = "<p class=\"validEmail\">An email has sent you. Thanks.</p>\n";
const MSG_ERROR = "<p class=\"formErrors\">The server is too busy. Please try later.</p>\n";
const MSG_SUBJECT = "This is an email from ";
const FROM_NAME = "Whisher";
const FROM_EMAIL = "admin AT blogial DOT net"; //You never know
public function __construct($to) {
parent::__construct($to, self::MSG_SUBJECT.self::FROM_NAME, self::FROM_NAME, self::FROM_EMAIL);
}
public function setBodyMsg($username,$password)
{
$bodyMsg = '<p>Username :'.$username.'</p>';
$bodyMsg .= '<p>Password :'.$password.'</p>';
$this->setBodyHtml($bodyMsg);
$this->setPriority(AbstractMail::HIGH_PRIORITY);
if($this->send())
{
$outPut = self::MSG_OK;
}
else
{
$outPut = self::MSG_ERROR;
}
return $outPut;
}
?>

Bye and Season's Greetings :D
 
Back
Top