driuwt9557 2019-03-25 14:07
浏览 26

将PHPMailer与Slim Framework一起使用

I'm using the Slim Framework to create several web services, now I want to create a service like: www.example.com/public/feedback This service must send a mail with data from a form, then I can't find the way to use PHPMailer library in the Slim structure. Does I must include the library in the index.php and it can be used in the routes.php file? Thanks in advance.

Edit: I hope that this can clarify my concerns:

require __DIR__ . '/../vendor/autoload.php';

require __DIR__ . '/../../PHPMailer/Exception.php';
require __DIR__ . '/../../PHPMailer/PHPMailer.php';
require __DIR__ . '/../../PHPMailer/SMTP.php';

session_start();

// Instantiate the app
$settings = require __DIR__ . '/../src/settings.php';
$app = new \Slim\App($settings);

The above code is in my index.php file in public folder and in the routes.php in the src folder I have the next code:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
$app->get('/feedback',function(Request $request,Response $response,array $args){
  $mail = PHPMailer(true);
  try {
      //Server settings
      $mail->SMTPDebug = 2;                                       // Enable verbose debug output
      $mail->isSMTP();                                            // Set mailer to use SMTP
      $mail->Host       = 'example.com';         // Specify main and backup SMTP servers
      $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
      $mail->Username   = 'user@example.com';// SMTP username
      $mail->Password   = 'secret';                         // SMTP password
      $mail->SMTPSecure = 'tls';                                  // Enable TLS encryption, `ssl` also accepted
      $mail->Port       = 587;                                    // TCP port to connect to

      //Recipients
      $mail->setFrom('user@example.com','WebApp Contact');
      $mail->addAddress('mmucino@yahoo.com','Marco Muciño');               // Name is optional

      // Content
      $mail->isHTML(true);                                  // Set email format to HTML
      $mail->Subject = 'Here is the subject';
      $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
      $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

      $mail->send();
      echo 'Message has been sent';
  } catch (Exception $e) {
      echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
  }
  return $response->withJSON(array(
    'status' => 'OK'
  ));

With my real data instead of example.com

But the if I call the service with the url:

example.com/public/feedback

I get the error:

Slim Application Error
The application could not run because of the following error:

Details
Type: Error
Message: Call to undefined function PHPMailer()
File: /home/tecallia/public_html/gabriel/src/routes.php
Line: 405

What am I doing wrong? Thanks in advance.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 matlab计算中误差
    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料
    • ¥15 使用R语言marginaleffects包进行边际效应图绘制
    • ¥20 usb设备兼容性问题
    • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊