Weird behavior #2498
Replies: 6 comments 12 replies
-
The following is in production code working perfectly under the old version of PHPMailer. It is located in an external file invoked into each page by a require_once() statement: So why is it working there and not in my new code using the selfsame mechanism? |
Beta Was this translation helpful? Give feedback.
-
I've removed all the name space statements as well as the require statements for PHPMailer, Exception, and SMTP. I have the following line in my config file:
PHPMailer has been installed in the VENDOR_DIR directory. No joy! my revised error msg says The class file PHPMailer cannot be found using .inc or .php file extensions. What am I missing here? Sorry for the stupid questions. I struggle using frameworks. |
Beta Was this translation helpful? Give feedback.
-
So I am stuck with those 'use' statements in the body of every single page and cannot put them into a reusable code file which can be invoked all the pages in 5+ websites sharing that same code? What a ridiculous limitation! This is my statement defining VENDOR_DIR: I indeed have an excellent handle on my loading process. I got into the habit of using require_once() when I was a beginner and simply use it out of habit. Finally, if I am sending an email using SMTP, why would I not need to use PHPMailer's SMTP object? |
Beta Was this translation helpful? Give feedback.
-
Once I put the use statements back into the file, it works flawlessly! Thanks for the clarifications! I wrote an Email object which facilitates the creation and sending of our organization-specific HTML emails via various mail-merge-type templates. Right now, I have to pass it an instantiated PHPMailer object in the constructor as follows: Here is the test code which I just successfully ran using my Email object + PHPMailer:
After I have built the HTML email body and its generic text counterpart using private methods in this object and selected the appropriate Subject line for said template, its own send() methodI transfers all the necessary values to the PHPMailer object which then uses its own send() method to actually send the msg. Like I said, once we resolved the PHPMailer calls, it works great! You mentioned placing the use statements and PHPMailer instantiation inside a function to make them reusable. Is there a way to somehow use my object's constructor as that function? Something along the following lines:
-OR-
instead of the current code:
One final question regarding the use statement: I thought whenever you include() or require() an external file, the code contained therein is inserted into the calling file and the resulting aggregate code is then executed after all inclusions have been completed. If this the way things happen, why does the use statement break when placed in an include() file? How would the interpreter even know an include() file was in play once the code has been inserted? |
Beta Was this translation helpful? Give feedback.
-
With your ‘all the pages in 5+ websites sharing that same code’ you might find the decomplexity/SendOauth2 wrapper for PHPMailer worth a look, especially if you use (or plan to use) OAuth2 authentication, although basic authentication is also supported. It is installed with Composer and has its own namespace. |
Beta Was this translation helpful? Give feedback.
-
As it turns out, this one actually works!
but I would still appreciate an answer to my question about include() files. |
Beta Was this translation helpful? Give feedback.
-
when using the old version of PHPMailer for PHP 5.x, I've always had the require() and use namespace statements contained in a config file required at the top of every page. This is what these statements look like in the config file:
`
require_once( CLASSES . "PHPMailer/src/PHPMailer.php" ); // PHPMailer framework object
require_once( CLASSES . "PHPMailer/src/Exception.php" ); // PHPMailer framework exceptions object
require_once( CLASSES . "PHPMailer/src/SMTP.php" ); // PHPMailer framework SMTP object
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
`
Since updating to your newest version, I have isolated the following behavior:
When I put the use statements in my config file, I get a error stating _"The class file /home/lcudev/master/globals/classes/PHPMailer.inc cannot be found."
That error message is triggered by my own autoloader code whenever it fails to find an object class.
Yet when I move the use statements to the calling page and place them underneath the require() statement loading the aforementioned config page as shown below, everything works perfectly.
`
require_once( "../_globals/_config.inc" );
`
What's up with that and how can I fix it?
PHP 7.3.30
PHPMailer 6.5.0
Linux server
Beta Was this translation helpful? Give feedback.
All reactions