[英]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(" ", "\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.