使用phpmailer调用路由中的函数

使用phpmailer调用路由中的函数,php,controller,namespaces,slim,Php,Controller,Namespaces,Slim,让我看看如何最好地解释我试图做的事情以及代码示例。我只是在清理我的代码,想找出最好的方法来移动东西。我有一个表单,当我单击“提交”时,它会发送给我并发送一封工作正常的电子邮件(使用phpmailer,使用composer安装) 工作代码如下: 这是我的帖子,在提交后被称为,效果很好。我想将php邮件程序代码移动到我创建的一个单独的名称空间中 $app->post('/', function ($request, $response) { $mail = new PHPMail

让我看看如何最好地解释我试图做的事情以及代码示例。我只是在清理我的代码,想找出最好的方法来移动东西。我有一个表单,当我单击“提交”时,它会发送给我并发送一封工作正常的电子邮件(使用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';
  }
});
然后我将代码移动到一个名为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';
    }
  }
}
我只是想弄清楚我哪里出了问题,以及为什么在移动代码后,当我从Mailer.php调用函数时,它不起作用。我知道我的psr-4和名称空间起作用了,因为当我删除所有代码并放在下面时:

   <?php

namespace App\Controllers;

class Mailer
{
  public function sendMail()
  {
    return 'Working';
  }
}

这是因为您正试图调用类
PHPMailer
,而您的应用程序将试图在
App\Controllers\PHPMailer
中找到该类

您需要添加或添加一个,然后它应该可以正常工作

导入命名空间:
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
,而您的应用程序将试图在
App\Controllers\PHPMailer
中找到该类

您需要添加或添加一个,然后它应该可以正常工作

导入命名空间:
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;