使用 phpmailer 在路由中调用函数
Calling a function in a route with phpmailer
让我看看如何用代码示例最好地解释我正在尝试做的事情。我只是在清理我的代码,想找出移动东西的最佳方法。我有一个表格,当我点击提交时,它会发送给我和电子邮件,效果很好(使用 phpmailer,使用 composer 安装它)
下面的工作代码:
这是我的 post,在提交后调用,效果很好。我想将 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';
}
}
它工作正常。有什么建议么?抱歉问了这么长的问题
这是因为您正在尝试调用 class PHPMailer
并且您的应用程序将尝试在 App\Controllers\PHPMailer
.
中找到它
您需要 import the namespace or add a 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;
让我看看如何用代码示例最好地解释我正在尝试做的事情。我只是在清理我的代码,想找出移动东西的最佳方法。我有一个表格,当我点击提交时,它会发送给我和电子邮件,效果很好(使用 phpmailer,使用 composer 安装它)
下面的工作代码: 这是我的 post,在提交后调用,效果很好。我想将 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';
}
}
它工作正常。有什么建议么?抱歉问了这么长的问题
这是因为您正在尝试调用 class PHPMailer
并且您的应用程序将尝试在 App\Controllers\PHPMailer
.
您需要 import the namespace or add a 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;