FRIHOST • FORUMS • SEARCH • FAQ • TOS • BLOGS • COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


PHP Observer pattern for sending email?





lauthiamkok
I have a contact form on my website where the client can send messages to me.

When my server receives the message, I want to send a notification to the client and another one to myself (server).

So I thought the observer pattern is suitable to this task where the client and the server are both observers.

Below is my attempt:

Interface:

Code:
interface Observer
    {
        function onChanged(Observable $Observable, array $params);
    }
   
    interface Observable
    {
        function addObserver(Observer $Observer);
        function removeObserver(Observer $Observer);
        function notifyObserver(array $params);
    }


The concrete observable:

Code:
 class MessageObservable implements Observable
    {
        private $Observers = [];
   
        public function recieved($message)
        {
            return $this->notifyObserver($message);
        }
   
        public function addObserver(Observer $Observer)
        {
            $this->Observers[] = $Observer;
        }
   
        public function notifyObserver(array $params)
        {
            foreach($this->Observers as $Observer) {
                return $Observer->onChanged($this, $params);
            }
        }
   
        public function removeObserver(Observer $Observer)
        {
            foreach($this->Observers as $observerKey => $observerValue) {
                if ($observerValue === $Observer) {
                    unset($this->Observers[ $observerKey ]);
                }
            }
        }
    }



The concrete observers:

Code:
class ClientObserver implements Observer
    {
        public function onChanged(Observable $Observable, array $params)
        {
            // Separate the client and server params.
            $client = $params['client'];
            $server = $params['server'];
   
            // Client e-mail.
            $emailTo = $client['email'];
   
            // Server e-mail.
            $emailFrom = $server['email'];
   
            // Headers.
            $emailHeaders = "From: " . $server['name'] . " <$emailFrom>";
   
            // Server subject.
            $emailSubject = 'Message Sent - ' . $client['subject'];
   
            // Server content.
            $emailContent = "Dear " . $client['name'] . ",
   
    You have sent a message at " . $server['website'] . " using this e-mail.
   
    Your Name : " . $client['name'] . "
    Email : " . $client['email'] . "
    Subject : " . $client['subject'] . "
    Message : " . $client['content'] . "
   
    With thanks,
    " . $server['website'] . "
   
    ";
            print_r($emailContent);
   
            // Send email.
            // $sentMail = mail($emailTo, $emailSubject, $emailContent, $emailHeaders);
   
            // Assume sent OK.
            $sentMail = true;
   
            // If sent ok.
            if($sentMail) {
                return [
                    "status" => "success",
                    "message" => "Your message has been sent successfully. Thanks for contacting us."
                ];
            } else {
                return [
                    "status" => "fail",
                    "message" => "Your message is not sent. Please try again."
                ];
            }
   
        }
    }
   
    class ServerObserver implements Observer
    {
        public function onChanged(Observable $Observable, array $params)
        {
            // Separate the client and server params.
            $client = $params['client'];
            $server = $params['server'];
   
            // Server e-mail
            $emailTo = $server['email'];
   
            // Client e-mail
            $emailFrom = $client['email'];
   
            // Client Headers
            $emailHeaders = "From: " . $client['name'] . " <$emailFrom>";
   
            // Client subject
            $emailSubject = $client['subject'];
   
            // Client Message.
            $emailContent = $client['content'];
   
            // Send email.
            // $sentMail = mail($emailTo, $emailSubject, $emailContent, $emailHeaders);
   
            // Assume sent OK.
            $sentMail = true;
   
            return false;
        }
    }


On a controller:

Code:
$message = [
        'client' => [
            'name' => 'Client Name',
            'email' => 'Client Email',
            'subject' => 'Client Subject',
            'content' => 'Client content',
        ],
        'server' => [
            'name' => 'Server Name',
            'email' => 'Server Email',
            'website' => 'Server Website',
            'subject' => 'Server Subject',
        ]
    ];
   
    $res = $MessageObservable->recieved($message);
    print_r($res);


Result:

Code:
Array
    (
        [status] => success
        [message] => Your message has been sent successfully. Thanks for contacting us.
    )


It works fine but I have repeated:

1. $sentMail = mail($emailTo, $emailSubject, $emailContent, $emailHeaders);

2. $emailTo, $emailFrom, etc.

Is there anyway to make to better? Or it is **not** the observer pattern that I should be using?
Related topics
[php scripts ] phpweather&email
SendMail Script
how to make web hosting
Sending mail with flash using PHP
formularios y correo-e, !ayuda¡
cool sms addon
No mail sending on Outlook 2003
Sending email via PHP
Contact form
sending email to multiple users.
PHP script to create email addresses/ftp accounts/etc...
Sending Email from localhost
DEBUG MODE?
File : emailer.php yeah... it happened again.
php mail function not sending email to Yahoo address
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.