dongtan1009 2017-11-27 04:03
浏览 52
已采纳

使用include语句后,新手到OOP无法访问类属性

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 In tclass.php I am trying to pass variables to the class, this is a test.

tclass.php

<?php
   include_once('C:\xampp\htdocs
space3.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;
}

?>
  • 写回答

1条回答 默认 最新

  • drsl90685154 2017-11-27 04:24
    关注

    You create the $mail object and sent the email in nspace3.php, only after that you are calling the setFrom which will not work since the mail already sent. So i suggest pass those data to nspace3 instead.

    <?php
       $from = "jperson19468@gmail.com" ;
       $mailer = "Mailer" ;
       include_once('C:\xampp\htdocs
    space3.php');
    ?>
    

    in nspace3.php

    //Recipients
    $mail->setFrom($from, $mailer ); <==== Line trying to access in tclassfile
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化