gpt4 book ai didi

php - 使用 phpmailer 在路由中调用函数

转载 作者:行者123 更新时间:2023-12-04 16:59:57 25 4
gpt4 key购买 nike

让我看看如何最好地解释我正在尝试做的事情以及代码示例。我只是在清理我的代码,并想找出移动事物的最佳方式。我有一个表单,当我点击提交时,它会发送给我并发送电子邮件,效果很好(使用 phpmailer,使用 Composer 安装它)

工作代码如下:
这是我的帖子,在提交后调用,效果很好。我想将 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';
}
}

它工作正常。有什么建议?抱歉问了这么长的问题各位

最佳答案

这是因为您试图调用类 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;

关于php - 使用 phpmailer 在路由中调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44557718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com