我想将变量传递给我刚才在a中使用的类中的属性 包括声明 我怎么做。这就是我所拥有的(不工作) tclass.php邮件程序调用的类是nspace.php 在tclass.php中我试图将变量传递给类,这是一个测试。
tclass.php
<?php
include_once('C:\xampp\htdocs\nspace3.php');
$mail->setFrom("jperson19468@gmail.com", "Mailer");
?>
nspace3.php
<?php
namespace MyProject;
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';
$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('jperson19468@gmail.com', 'Mailer'); <==== Line trying to access in tclassfile
$mail->addAddress('jperson19468@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();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>
答案 0 :(得分:1)
您创建$ mail对象并在nspace3.php中发送电子邮件,之后您才调用setFrom,因为邮件已经发送,因此无效。 所以我建议将这些数据传递给nspace3。
<?php
$from = "jperson19468@gmail.com" ;
$mailer = "Mailer" ;
include_once('C:\xampp\htdocs\nspace3.php');
?>
nspace3.php中的
//Recipients
$mail->setFrom($from, $mailer ); <==== Line trying to access in tclassfile