dscbxou1900343 2019-05-01 20:07
浏览 55
已采纳

为什么我在尝试使用phpmailer发送电子邮件激活新用户帐户时收到此错误

i am trying to understand why my subject method in index.php triggers an error of not being defined.i am using phpmailer 5.2.7 with php 7.2 and wampserver 3.1.7

//here is my extended class from phpmailer//

<?php
include('phpmailer.php');

class Mail extends PhpMailer
{

    // Set default variables for all new objects

    public $From     = 'xxxxxx@gmail.com';
    public $FromName =  MM;
    public $Host     = 'smtp.gmail.com';
    public $Mailer   = 'smtp';
    public $SMTPAuth =  true;
    public $Username = 'xxxxxxx@gmail.com';
    public $Password = 'xxxxxx';
    public $SMTPSecure = 'ssl';
    public $WordWrap = 75;



    public function subject($subject)
    {
        $this->Subject = $subject;
    }

    public function body($body)
    {
        $this->Body = $body;
    }

    public function send()
    {
        $this->AltBody = strip_tags(stripslashes($this->Body))."

";
        $this->AltBody = str_replace("&nbsp;", "

", $this->AltBody);
        return parent::send();
    }
}

and here is part of my index page where i have defined my variables

$to = $_POST['email'];
            $subject = "Registration Confirmation";
            $body = "<p>Thank you for registering at demo site.</p>
            <p>To activate your account, please click on this link: <a href='".DIR."activate.php?x=$id&y=$activasion'>".DIR."activate.php?x=$id&y=$activasion</a></p>
            <p>Regards Site Admin</p>";

            $mail = new PHPMailer(true);
            $mail->setFrom(SITEEMAIL);
            $mail->addAddress($to);
            $mail->subject($subject);
            $mail->body($body);
            $mail->send();

            //redirect to index page
            header('Location: index.php?action=joined');
            exit;
  • 写回答

1条回答 默认 最新

  • donglou1866 2019-05-01 21:26
    关注

    Firstly, why are you using a version of PHPMailer that's literally years out of date? Get the latest, which has new features, fixed bugs, and fewer security holes. While you're upgrading, consider switching to using composer to manage your dependencies.

    The problem you're having is quite straightforward: you have created a subclass that adds the subject() method, but the instance you've created in your script is of the original PHPMailer class, not your subclass. Do this instead:

    $mail = new Mail(true);
    

    Naming your class with a very generic "Mail" name is very likely to bring you an unexpected lesson on why namespacing is a good idea, so I'd recommend adding a namespace for your app to avoid name clashes.

    While it's a good idea to subclass like this to set default values easily, it's also inviting you to check in credentials to your source repo, which is usually a bad idea. Better to use your child class to read those values from an environment file ("dot env") using a package like this.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?