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.