tuurbo
tuurbo

Reputation: 361

php static email class

Im new to classes and im trying to create a static email class that uses the phpmailer class.

What I'd like to do is something like...

Email::send('from', 'to', 'subject', 'html message'); // works

but if i want to add an attachment...

Email::send('from', 'to', 'subject', 'html message')->attach('file/blah.txt');

This throws a fatal error: Call to undefined method PHPMailer::attach(), I understand why, I just don't know how to go about it making the Email class do the above code, if it's even possible.

Below is what I've experimented with.

class Email {

    static $attach;

    public static function send($from, $to, $subject, $message)
    {
        $email = new PHPmailer();

        try {

            $email->AddAddress($to);
            $email->SetFrom($from);
            $email->Subject = $subject;
            $email->MsgHTML($message);

            if (self::$attach) $email->AddAttachment(self::$attach);        

            $email->Send();
        }
        catch (phpmailerException $e)
        {
            return $e->errorMessage();
        }
        catch (Exception $e)
        {
            return $e->getMessage();
        }

        return $email;
    }

    public static function attach($attachment)
    {
        self::$attach = $_SERVER['DOCUMENT_ROOT'].$attachment;
    }
}

Upvotes: 0

Views: 711

Answers (2)

prodigitalson
prodigitalson

Reputation: 60413

You API doesnt make any sense. To do what youre trying to do with the chaining youre goin to need to use instances but you can also use statics to mke the interface more like what you want:

class Email {

    protected $attchements = array();
    protected $mailer;

    public function __construct($from, $to, $subject, $message) {
          $this->mailer = new PHPMailer();

          $this->mailer->AddAddress($to);
          $this->mailer->SetFrom($from);
          $this->mailer->Subject = $subject;
          $this->mailer->MsgHTML($message);

    }

    public static function create($from, $to, $subject, $message) {
        $instance = new Self($from, $to, $subject, $message);
        return $instance;

    }

    public static function createAndSend($from, $to, $subject, $message) {
         $instance = new Self($from, $to, $subject, $message);
         return $instance->send();
    }

    public function send()
    {
       if(!empty($this->attachments)) {
           foreach($this->attachments as $attachment) {
               $this->mailer->AddAttachment($attachment);
           }
       }

       return $this->mailer->send();        
    }

    public function attach($attachment)
    {
        $this->attachments[] = $_SERVER['DOCUMENT_ROOT'].$attachment;
        return $this;
    }
}

So with this your usage looks like:

//simple
Email::createAndSend($to, $from, $subject, $message);

// with attachment
Email::create($to, $from, $subject, $message)
   ->attach('fileone.txt')
   ->attach('filetwo.txt')
   ->send();

Also should be noted i took your exception handling out of my example... you should integrate that... i only did it for the sake of keeping it short and sweet :-)

Upvotes: 2

php-yvsppt
php-yvsppt

Reputation: 31

Fluent instruction works fine with object (different from static class).

In your case, simply invert instruction:

Email::attach('file/blah.txt');
Email::send('from', 'to', 'subject', 'html message');

But a real object may works better.

Upvotes: 0

Related Questions