i would like to know how exactly to use SwiftMailerHandler
within Monolog
packagist?
In the Monolog
documentation i don't see any usage example regarding SwiftMailerHandler
or maybe i missed out.
Here is the SwiftMailerHandler
constructor code:
/**
* @param \Swift_Mailer $mailer The mailer to use
* @param callable|\Swift_Message $message An example message for real messages, only the body will be replaced
* @param integer $level The minimum logging level at which this handler will be triggered
* @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
*/
public function __construct(\Swift_Mailer $mailer, $message, $level = Logger::ERROR, $bubble = true)
{
parent::__construct($level, $bubble);
$this->mailer = $mailer;
if (!$message instanceof \Swift_Message && is_callable($message)) {
$message = call_user_func($message);
}
if (!$message instanceof \Swift_Message) {
throw new \InvalidArgumentException('You must provide either a Swift_Message instance or a callable returning it');
}
$this->message = $message;
}
But i still don't know how to set the \Swift_Mailer $mailer
mentioned above.
Is there any other steps / configuration i should do?
Sorry if my question is very basic.
Thanks.
First, you need to install SwiftMailer (composer require swiftmailer/swiftmailer) to be able to use the SwiftMailerHandler. For you to be able to send emails, you need to use Swift_SmtpTransport, Swift_Message, and Swift_Mailer to join $mailer and $message to the SwiftMailerHandler instance.
Swift Mailer is used as the main mail option in frameworks like Yii2 and CMS like Drupal, Laravael’s email API is built on top of the Swift Mailer library as well. Let’s start with the installation. It’s recommended to make it via Composer:
A Monolog logger instance has a channel (name) and a stack of handlers . The handlers are responsible for saving the message to the file, database, or sending it to a mail.
This article talks about the step-by-step process of using PHP Monolog in your application. Troubleshooting and optimizing your code is easy with integrated errors, logs and code level performance insights. The basic way to use Monolog in PHP is to install the latest version of the Composer library in your project using this command.
You will need to look at the Swift_Mailer docs as to how to set up the mailer.
https://swiftmailer.symfony.com/docs/sending.html
Once you have your mailer set up that is what you should pass into the new SwiftMailerHandler() for the $mailer variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With