Easy-to-follow guide for sending emails from PHP Slim Framework. You can try it out using a fake SMTP account.

Step 1: Install PHPMailer library by composer from Packagist

Go to php slim root folder by terminal then execute the following command. It will automatically install the email library from Packagist.

composer require phpmailer/phpmailer

Step 2: Add the Following Code in Route/Controller

PHP Slim Framework Email

You can easily test email functions with fake SMTP testing servers like MailMug. Just create a free account. Then go to Inbox and copy credentials from the settings page. Then add credentials to the following code.

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
use PHPMailer\PHPMailer\PHPMailer;

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

$app = AppFactory::create();

$app->get('/send', function (Request $request, Response $response, $args) {
    $phpmailer = new PHPMailer();
    $phpmailer->Host = 'smtp.mailmug.net';
    $phpmailer->SMTPAuth = true;
    $phpmailer->Port = 2525;
    $phpmailer->SMTPSecure = '';
    $phpmailer->Username = 'lz5ebosxdq0uruxb'; // https://mailmug.net/ create an account
    $phpmailer->Password = 'kbs6m4hifn8di3gi';
    $phpmailer->setFrom('from@example.com', 'Mailer');
    $phpmailer->addAddress('info@example.net', 'Example');     //Add a recipient

    $phpmailer->isHTML(true);                                  //Set email format to HTML

    $phpmailer->Subject = 'Here is the subject';
    $phpmailer->Body    = 'This is the HTML message body <b>in bold!</b>';
    $phpmailer->AltBody = 'This is the body in plain text for non-HTML mail clients';


    $response->getBody()->write("email sent");
    return $response;


Step 3: Test it

Start the server and navigate to http://your-domain/send from your browser. Then you can check test emails from MailMug admin panel.

Step 4: Configure Production Email Server

Copy and paste production email server credentials. You will get email server SMTP credentials from cPanel or Plesk. Go to cPanel > Emails > create an email.

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *