How to send email in slim framework with controllers

I am building an applincation using slim 3 framework, and I want to be able to send verification mail with a link and validate the email on click of the link in the email

Also send other emails when the user performs some event in the application.

Can someone help me with some example codes using controllers with PHPMailer

The implementation will depend on things like if you are using any sort of event handler, or if you just want to fire off the email directly from within the controller/action. But it could be something as simple as this.

class UserStoreAction
{
   public function __invoke($request, $response, $args) {
        // ... your other code to store a user
        // ... now send the user an email
        $mail = new PHPMailer(true);
        try {
            $mail->isSendmail();
            $mail->setFrom('from@example.com', 'First Last');
            $mail->addAddress('whoto@example.com', 'John Doe');
            $mail->Subject = 'PHPMailer sendmail test';
            $mail->msgHTML(file_get_contents('contents.html'), __DIR__);
            $mail->AltBody = 'This is a plain-text message body';
            $mail->send();
            echo 'Message has been sent';
        } catch (Exception $e) {
            echo 'Message could not be sent.';
            echo 'Mailer Error: ' . $mail->ErrorInfo;
        }

        // ... builtdand return Slim's response
        return $response;
   }
}

… or you can use mailgun.com like a email sending provider; they are provider simple PHP solution you can use to implement in Slim. I had done this way and i’m very happy with this solution.

Try running this locally.

Include the Autoloader (see “Libraries” for install instructions)

require ‘vendor/autoload.php’;
use Mailgun\Mailgun;

Instantiate the client.

$mgClient = new Mailgun(‘key-3ax6xnjp29jd6fds4gc373sgvjxteol0’);
$domain = “samples.mailgun.org”;

Make the call to the client.

$result = $mgClient->sendMessage("$domain",
array(‘from’ => ‘Excited User excited@samples.mailgun.org’,
‘to’ => ‘Mailgun Devs devs@mailgun.net’,
‘subject’ => ‘Hello’,
‘text’ => ‘Testing some Mailgun awesomeness!’));

1 Like

thank you @tflight for your response and apologies for get back to you late.

Your solution is quite straight forward and simple, thank you. But before i posted this question i googled and found something close to what i am looking for on codecourse.com (see below link):

https://www.codecourse.com/forum/topics/adding-mail-function-in-slim-3-authentication-series

but i am kind of stuck with an error, which i have tried resolving but to no avail.

Warning: Creating default object from empty value in C:\xampp\htdocs\wfn\app\Mail\Message.php on line 17

Fatal error: Uncaught Error: Call to undefined method stdClass::addAddress() in C:\xampp\htdocs\wfn\app\Mail\Message.php:9 Stack trace: #0 C:\xampp\htdocs\wfn\app\Controllers\ResourcesController.php(54): App\Mail\Message->to('example@yaho...') #1 [internal function]: App\Controllers\ResourcesController->App\Controllers\{closure}(Object(App\Mail\Message)) #2 C:\xampp\htdocs\wfn\app\Mail\Mailer.php(23): call_user_func(Object(Closure), Object(App\Mail\Message)) #3 C:\xampp\htdocs\wfn\app\Controllers\ResourcesController.php(57): App\Mail\Mailer->send('\\welcome.twig', Array, Object(Closure)) #4 [internal function]: App\Controllers\ResourcesController->getInformation(Object(Slim\Http\Request), Object(Slim\Http\Response), Array) #5 C:\xampp\htdocs\wfn\vendor\slim\slim\Slim\Handlers\Strategies\RequestResponse.php(41): call_user_func(Array, Object(Slim\Http\Request), Object(Slim\Http\Response), Array) #6 C:\xampp\htdocs\wfn\vendor\slim\slim\Slim\Route.php(325): Slim\Handlers\Strategies\RequestResponse->__invoke(Array, Object(S in C:\xampp\htdocs\wfn\app\Mail\Message.php on line 9

if you can kindly take a look at the above link and suggest how to go about this solution without errors i’ll be very grateful.