简体   繁体   English

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

[英]Why am i getting this error when trying to send email for activation of new user account with 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 我试图理解为什么我的index.php中的主题方法触发了一个没有被定义的错误。我正在使用phpmailer 5.2.7与php 7.2和wampserver 3.1.7

//here is my extended class from phpmailer// //这是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))."\n\n";
        $this->AltBody = str_replace("&nbsp;", "\n\n", $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;

Firstly, why are you using a version of PHPMailer that's literally years out of date? 首先,为什么你使用的PHPMailer版本已经过时了? 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. 在升级时,请考虑切换到使用composer来管理依赖项。

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. 您遇到的问题非常简单:您创建了一个添加subject()方法的子类,但您在脚本中创建的实例是原始的PHPMailer类,而不是您的子类。 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 . 最好使用你的子类使用像这样的包从环境文件(“dot env”)中读取这些值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我在尝试发送 email 时使用 phpmailer 收到此错误? - Why do I get this error with phpmailer when trying to send an email? 当我使用phpmailer()发送电子邮件时,在Gmail帐户中收到警告消息 - Getting warning message in Gmail Account when I send email by using phpmailer() 为什么在尝试使用图形 API 发送电子邮件时出现以下不受支持的媒体错误? - Why I am getting the following unsupported media error while trying to send the email using graph api? 我在尝试发送电子邮件验证时遇到错误 - I am getting error while trying to send email verification 为什么 SMTP 错误:无法连接到服务器:尝试使用 PhpMailer 发送电子邮件时没有到主机的路由 (65)? - Why SMTP ERROR: Failed to connect to server: No route to host (65) when trying to send email with PhpMailer? 当我尝试使用 phpmailer 发送邮件时,出现错误 - When i tried to send mail using phpmailer, i am getting error 尝试使用PHPMAILER发送电子邮件时出错 - Error while trying to send email using PHPMAILER 为什么我在 PHPMailer 的代码中出现错误? - Why am I getting an error in the code from PHPMailer? 为什么在尝试根据用户 ID 返回集合 model 时出现未找到列错误? - Why am I getting a column not found error when trying to return a collection model based on user ID? 向用户发送激活电子邮件 - Send activation email to user
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM