Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8699041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:51:09+00:00 2026-06-13T01:51:09+00:00

Im new to classes and im trying to create a static email class that

  • 0

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;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:51:10+00:00Added an answer on June 13, 2026 at 1:51 am

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a custom set of classes that can be added to
I'm trying to write a c++ program that uses classes, starts and listens for
I'm trying to create a container that would accept specific classes only. Using generics
Basically I am trying to create a new class as long as the continue
I am trying to create a base class where I can inherit from it
This bit of code comes with new classes that are subclasses of UITableViewController... -
New to FluentNHibernate =D I have a parent/children classes as follows: public class Parent
When I use new[] to create an array of my classes: int count =
My code: myobj = new Object(); classes.testegy = Class.extend({ init: function (token) { console.log(test
I'm trying to create two method to serialize/deserialize classes using Json.Net and save/load them

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.