让我看看我能解释我想要做的最好的代码示例。我只是在清理我的代码,想要找出最好的方法来解决问题。我有一个表单,当我点击提交它发送给我和电子邮件工作正常(使用phpmailer,使用作曲家安装)
以下工作代码: 这是我在提交后调用的帖子,工作正常。我想将php邮件代码移动到我创建的单独命名空间中。
$app->post('/', function ($request, $response) {
$mail = new PHPMailer; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.stackmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'info@nicolauslawson.com'; // SMTP username
$mail->Password = 'miya1234'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('info@nicolauslawson.com', 'Mailer');
$mail->addAddress('nicolaus.lawson@gmail.com', 'Joe User'); // Add a recipient
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = ' <div class="container">
<p>Name: '.$request->getParam('name').'</p>
<p>Number: '.$request->getParam('number').'</p>
<p>Dept: '.$request->getParam('dept').'</p>
<p>Date of last leave: '.$request->getParam('singedate1').'</p>
<p>Date of last resume: '.$request->getParam('singedate2').'</p>
<p>Date Request: '.$request->getParam('datefilter').'</p>
</div>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
});
使用psr-4
创建下面的命名空间 "autoload": {
"psr-4": {
"App\\": "app"
}
然后我将代码移动到名为Mailer.php的文件
<?php
namespace App\Controllers;
class Mailer
{
public function sendMail()
{
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.stackmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'info@nicolauslawson.com'; // SMTP username
$mail->Password = 'miya1234'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('info@nicolauslawson.com', 'Mailer');
$mail->addAddress('nicolaus.lawson@gmail.com', 'Joe User'); // Add a recipient
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = ' <div class="container">
<p>Name: '.$request->getParam('name').'</p>
<p>Number: '.$request->getParam('number').'</p>
<p>Dept: '.$request->getParam('dept').'</p>
<p>Date of last leave: '.$request->getParam('lastleave').'</p>
<p>Date of last resume: '.$request->getParam('lastresume').'</p>
<p>Date: '.$request->getParam('datefilter').'</p>
</div>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
}
最后在我的路线中我有
$app->post('/', '\App\Controllers\Mailer:sendMail');
只是想弄清楚我哪里出错了以及为什么在移动代码后,当我从Mailer.php调用该函数时它不起作用我知道我得到了psr-4和命名空间工作,因为当我删除所有的代码并放在下面:
<?php
namespace App\Controllers;
class Mailer
{
public function sendMail()
{
return 'Working';
}
}
工作正常。有什么建议?对不起这么长的问题人
答案 0 :(得分:2)
这是因为您尝试调用类PHPMailer
,并且您的应用程序将尝试在App\Controllers\PHPMailer
中找到它。
您需要import the namespace或添加global fallback,然后才能正常使用。
导入命名空间:
namespace App\Controllers;
use PHPMailer; // Import PHPMailer from global PHPMailer
class Mailer
{
public function sendMail()
{
$mail = new PHPMailer;
回归全球:
<?php
namespace App\Controllers;
class Mailer
{
public function sendMail()
{
// The leading \ tells PHP that the class is in the global namespace and not within this namespace
$mail = new \PHPMailer;