Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use swiftmailer handler with monolog

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.

like image 560
Firman Hidayat Avatar asked Jun 03 '14 10:06

Firman Hidayat


People also ask

How do I send an email using swiftmailerhandler?

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.

What is swift Mailer?

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:

How does a monolog logger work?

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.

How to use PHP monolog in your application?

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.


1 Answers

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.

like image 165
Ashley Schuett Avatar answered Sep 19 '22 17:09

Ashley Schuett