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”;
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):