CodeIgniter Forums
Best practice Send Mail - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Best practice Send Mail (/showthread.php?tid=83126)



Best practice Send Mail - pippuccio76 - 09-23-2022

Hi , if i have several control to send the same mail wich is the best way to do this ? a method in every controller or ?


RE: Best practice Send Mail - captain-sensible - 09-24-2022

I have a class called Utility.php ; funny enough as you might guess it does stuff like checking submitted text from a form by users for spam. The class is located in a directory called Andy and that directory, with its classes is in the app directory of CI4 ; and so in any controller i reference it by:

Code:
use \App\Andy\Utility;

The Utility class has several methods and any controller can use them. Its done by simply creating an instance of the class in any
other Controller or other class using:

Code:
$utilityHandle= new Utility();

//then any of the methods can be used by:

$utilityHandle->method();
Thats not a massive amount of code and of course you can feed in paramters(appropriate to the method such as


Code:
$utilityHandle->method($parem1, $parem2);

So there's probably other approaches but; i'm thinking create a class that does all the heavy lifting such as using SMTP and password creds, etc
This kind of stuff
Code:
$mail->Host = 'smtp.xxxx.com';
                            $mail->Port = 587;
                            $mail->SMTPSecure = 'tls';        
                            $mail->SMTPAuth = true;

In any controller , you will have a small method that only needs a couple of lines .