I want pass a variable to a property inside a class I just used in a a include statement
How do I do that. This is what I have (not working)
tclass.php mail program calls the class is nspace.php
Well, first off. You aren’t using OO correctly. You should be putting all the use keywords at the top before the includes. I’m not sure if it matters. But I know that sometimes, PHP will complain about non-existing classes. Second, there’s an autoloader to call the PHPMailer class. Third, the tclass.php file does really nothing because everything you are trying to achieve was all bunched up in the nspace3.php file.
Another thing to note is that using try-catch blocks shouldn’t be used every where. try-catch blocks only catch some systematic errors. You should be debugging your code every inch of the way. try-catch blocks can only do you so much.
I have put all the use statements before the includes:
I have uploaded the file to show this nspace3.php (2.1 KB)
I do have the program(nspace3) working without using the autoloader
In tclass.php I am trying to pass variables to the class, this is a test.
The PHPMailer class is the example the PHPMailer people have on their github page
Users are reluctant to download files for numerous reasons.
Can you enclose the two file contents within two lines both containing only three backticks.
When developing I have these DEBUG lines at the top of the PHP file which manage to reduce programming problems once the errors and warnings are eliminated.
# SET ERRORS and VALIDATION
# PHP7 FILE SPECIFIC
declare(strict_types=1);
# SET CONSTANT TO TEST ENVIRONMENT
# USE ONE FILE FOR ALL PLATFORMS
define('LOCALHOST', 'localhost' === $_SERVER['SERVER_NAME']);
# APPLIES TO ALL INCLUDE FILES
if( LOCALHOST ):
error_reporting(-1);
ini_set('display_errors', '1');
ini_set('display_html_errors', '1');
endif;
Are these correct? I know you might have renamed the files:
use C\xampp\htdocs\PHPMailer\src\SMPT;
...
require 'C:\xampp\htdocs\PHPMailer\src\SMPT.php';
but wouldn’t it normally be SMTP?
Can you expand please on “not working”? As I see it, your include file sets up the $mail object, sets some variables and tries to send an email. Just after that, you try to set the from-address. So that won’t take effect until after the mail has been sent, surely?
You should be using composer and autoloading rather than direct include statements. Putting that aside all your require calls are going to break when you move this project to your server.
Although there are lots of comments we could make about the code, I think droopsnoot’s comment addressed the immediate issue. By the time tclass.php sets the “from” address, the mail has already been sent by nspace3.php.
droopsnoot & jeff are correct. After looking at the code for sometime I had my “ah ha moment” I was making the substitution after the program has executed.
What I am trying to do is pass variables to the PHPMailer class from another php program before the
PHPMailer runs. Any ideas or thoughts?
@jperson19468 Short answer… you need to put your mail stuff in a function so you can invoke it from different places and with different arguments.
Longer answer… you’ll probably want to create a third file, let’s call it mailer.php, and all your mailer stuff will go in there, wrapped in a function. (Mostly all your code stayed the same, but notice that $mail->send() is commented out.)
// mailer.php
use PHPMailer\PHPMailer\Exception;
use C\xampp\htdocs\PHPMailer\src\SMPT;
use PHPMailer\PHPMailer\PHPMailer;
require 'C:\xampp\htdocs\PHPMailer\src\PHPMailer.php';
require 'C:\xampp\htdocs\PHPMailer\src\SMPT.php';
require 'C:\xampp\htdocs\PHPMailer\src\Exception.php';
function makeMailer() {
$mail = new PHPMailer;(true);
try {
//Server settings
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxxxxxxxxx'; // SMTP username
$mail->Password = 'xxxxxxx'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
// $mail->setFrom('xxxx@gmail.com', 'Mailer'); <==== Line trying to access in tclassfile
$mail->addAddress('xxxx@gmail.com', 'Joe User'); // Add a recipient
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is a Test3';
$mail->Body = 'I hope this works <b>It works!!</b>';
// $mail->send(); // not sending from here; just creating mailer; caller will send
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
return $mail;
}