简体   繁体   中英

Using PHPMailer without Composer

I just tried this example:

// for use PHP Mailer without composer : 

// ex. Create a folder in root/PHPMAILER
// Put on this folder this 3 files find in "src" 
// folder of the distribution : 
// PHPMailer.php , SMTP.php , Exception.php
  
  // include PHP Mailer
  use PHPMailer\PHPMailer\PHPMailer;
  use PHPMailer\PHPMailer\Exception;
  include dirname(__DIR__) .'/PHPMAILER/PHPMailer.php';
  include dirname(__DIR__) .'/PHPMAILER/SMTP.php';
  include dirname(__DIR__) .'/PHPMAILER/Exception.php';

  // i made a function 

   /*
    *
    * Function send_mail_by_PHPMailer($to, $from, $subject, $message);
    * send a mail by PHPMailer method
    * @Param $to -> mail to send
    * @Param $from -> sender of mail
    * @Param $subject -> suject of mail
    * @Param $message -> html content with datas
    * @Return true if success / Json encoded error message if error 
    * !! need -> classes/Exception.php - classes/PHPMailer.php - classes/SMTP.php
    *
    */
    function send_mail_by_PHPMailer($to, $from, $subject, $message){

          // SEND MAIL by PHP MAILER
          $mail = new PHPMailer();
          $mail->CharSet = 'UTF-8';
          $mail->isSMTP(); // Use SMTP protocol
          $mail->Host = 'your_host.com'; // Specify  SMTP server
          $mail->SMTPAuth = true; // Auth. SMTP
          $mail->Username = 'my_mail@your_host.com'; // Mail who send by PHPMailer
          $mail->Password = 'your_passord_of_your_box'; // your pass mail box
          $mail->SMTPSecure = 'ssl'; // Accept SSL
          $mail->Port = 465; // port of your out server
          $mail->setFrom($from); // Mail to send at
          $mail->addAddress($to); // Add sender
          $mail->addReplyTo($from); // Adress to reply
          $mail->isHTML(true); // use HTML message
          $mail->Subject = $subject;
          $mail->Body = $message;

          // SEND
          if( !$mail->send() ){

              // render error if it is
              $tab = array('error' => 'Mailer Error: '.$mail->ErrorInfo );
              echo json_encode($tab);
              exit;
          }
          else{
              // return true if message is send
              return true;
          }

    }
    /*
    *
    * END send_mail_by_PHPMailer($to, $from, $subject, $message)
    * send a mail by PHPMailer method
    *
    */
  
    // use function :
    send_mail_by_PHPMailer($to, $from, $subject, $message);

When calling new PHPMailer() the system throws a error:

Can't find class PHPMailer.

The include of the files works fine. What is wrong?

You could try reading the readme (which should be the first place you look anyway), which tells you the right way to load PHPMailer without composer.

I expect the problem is that you are using include instead of require , and the classes are failing to load, so that when you try to make an instance, it's not defined, giving you the error you're seeing. Your script will not work without those files, so you want it to fail if they can't be loaded.

There's a lesson though - learn to use composer and understand how it works. As a PHP novice, it's the single best thing you can do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM