Home php php - how to use class?

php – how to use class?

Author

Date

Category

class class.send_mail_func.php

class phpmailer {
    Public Function Sendmail ($ Message, $ SUBJECT) {
Use phpmailer \ phpmailer \ phpmailer;
use phpmailer \ phpmailer \ exception;
REQUIRE_ONCE ('./Phpmailer/vendor/Phpmailer/Phpmailer/SRC/Exception.php');
Require_ONCE ('./phpmailer/vendor/PhPmailer/Phpmailer/src/phpmailer.php');
Require_ONCE ('./phpmailer/vendor/Phpmailer/Phpmailer/src/smtp.php');
 // send mail
 $ Mail = New phpMailer;
 // $ Mail- & gt; SMTPDEBUG = 3; // Enable Verbose Debug Output
 $ Mail- & gt; SetLanguage ('RU', './Phpmailer/Language/');
 $ Mail- & gt; Charset = 'UTF-8';
 $ Mail- & GT; ISSMTP (); // Set Mailer To Use SMTP
 $ Mail- & GT; Host = 'Host'; // Specify Main and Backup SMTP Servers
 $ Mail- & gt; smtpauth = true; // Enable SMTP Authentication
 $ Mail- & GT; Username = 'Mail @ MailRu'; // SMTP Username.
 $ Mail- & gt; password = '123'; // SMTP Password.
 $ Mail- & gt; smtsecure = 'ssl'; // Enable TLS Encryption, `SSL` Also Accepted
 $ Mail- & gt; port = 465; // TCP PORT TO CONNECT TO
 $ Mail- & gt; setfrom ('[email protected] ',' text ');
 $ mail- & gt; addaddress ('user'); // Add a recipient
 $ Mail- & gt; ISHTML (TRUE); // Set Email Format to HTML
 $ Mail- & gt; subject = $ subject;
 $ Mail- & GT; MSGHTML ($ Message);
 If (! $ Mail- & gt; send ()) {
   Echo 'Message Could Not Be Sent.';
   Echo 'Mailer Error:'. $ Mail- & gt; errorinfo;
 } else {
   Echo 'Message Has Been Sent';
 }
}
}

I use so

include './class.send_mail_func.php';
 $ email_send = new phpmailer ();
 $ email_send- & gt; Sendmail ($ message, $ subject);

I get an error: Parse Error: Syntax Error, UNEXPECTED ‘USE’ (T_USE) in /home/class.send_mail_func.php on line 7

How to use Use inside the class?


Answer 1, Authority 100%

If this PHPMailer package, which you took on Packagist or GitHub-E, then, on the same githabe there is a great example. I understand that you are trying to wrap this class inside your?

use phpmailer \ phpmailer \ phpmailer;
Use phpmailer \ phpmailer \ SMTP;
use phpmailer \ phpmailer \ exception;
// Require 'vendor / autoload.php';
//t.k. autoload is not configured connect directly classes
REQUIRE_ONCE ('./Phpmailer/vendor/Phpmailer/Phpmailer/SRC/Exception.php');
Require_ONCE ('./phpmailer/vendor/PhPmailer/Phpmailer/src/phpmailer.php');
Require_ONCE ('./phpmailer/vendor/Phpmailer/Phpmailer/src/smtp.php');
Class MyMail {
 Public Function Sendmail ($ Message, $ SUBJECT) {
// Your code here
$ Mail = New Phpmailer (TRUE);
$ Mail- & gt; smtpdebug = SMTP :: debug_server; // Enable Verbose Debug Output
  $ Mail- & GT; ISSMTP (); // send using SMTP
  $ Mail- & gt; host = 'smtp1.example.com'; // Set The SMTP Server to Send Through
  $ Mail- & gt; smtpauth = true; // Enable SMTP Authentication
  $ Mail- & gt; username = '[email protected]'; // SMTP Username.
  $ Mail- & gt; password = 'secret'; // SMTP Password.
  $ Mail- & gt; smtpecure = phpmailer :: encryption_starttls; // Enable TLS Encryption; `Phpmailer :: encryption_smtps` Also Accepted
  $ Mail- & gt; port = 587; // TCP PORT TO CONNECT TO
// etc ...
  }
  }

Call you correct

include './class.send_mail_func.php';
 $ email_send = new mymail ();
 $ email_send- & gt; Sendmail ($ message, $ subject);

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions