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

标签 php controller namespaces slim

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

相关文章:

php - 外键约束失败

javascript - 使用 Ajax 仅检索数据库中的数据之间如何更好地从 html 结构中的数据库检索数据?

c++ - 单例实现 : Is a namespace approach often preferable over a singleton class?

c# - 命名空间 'Nmo' 中不存在类型或命名空间名称 'Microsoft.SqlServer.Management'

java - Java 和 PHP 中相同的哈希算法给出不同的结果

c# - 将大 List<T> 从 View 传递到 Controller 的最佳方法

php - MVC : Where should I format data?

javascript - AngularJS : pass data from controller to controller without factory, 服务或广播?

.net - 命名空间名称中术语的首选顺序

php - 从 php 传递到 AJAX