php - How to define a service for mailer with a specific transport - Stack Overflow

I am using Monolog with HTML email formatter to send some alerts.monolog:# ....symfony_mailer:type:

I am using Monolog with HTML email formatter to send some alerts.

monolog:
    # ....
    symfony_mailer:
        type:           symfony_mailer
        from_email:     "%admin_email%"
        to_email:       "%admin_email%"
        subject:        "A problem occurred"
        level:          info
        content_type:   text/html
        formatter:      monolog.formatter.html

I want sending email with a specific email account defined as transport "monolog"

# config/packages/mailer.yaml
framework:
    mailer:
        transports:
            main: '%env(MAILER_DSN)%'
            monolog: '%env(MAILER_DSN_IMPORTANT)%'

For that, I can specify a service to use in the monolog config like mailer: 'monolog_mailer_transport' but I am not able to define the service to use the "monolog" transport.

service:
    # ....
    # NOT WORKING : HOW TO DEFINE THE SERVICE TO USE THE TRANSPORT 'MONOLOG' ?
    monolog_mailer_transport:
        class: Symfony\Component\Mailer\Transport\TransportInterface
        factory: ['@mailer.transports', 'get']
        arguments: ['monolog']

I am using Monolog with HTML email formatter to send some alerts.

monolog:
    # ....
    symfony_mailer:
        type:           symfony_mailer
        from_email:     "%admin_email%"
        to_email:       "%admin_email%"
        subject:        "A problem occurred"
        level:          info
        content_type:   text/html
        formatter:      monolog.formatter.html

I want sending email with a specific email account defined as transport "monolog"

# config/packages/mailer.yaml
framework:
    mailer:
        transports:
            main: '%env(MAILER_DSN)%'
            monolog: '%env(MAILER_DSN_IMPORTANT)%'

For that, I can specify a service to use in the monolog config like mailer: 'monolog_mailer_transport' but I am not able to define the service to use the "monolog" transport.

service:
    # ....
    # NOT WORKING : HOW TO DEFINE THE SERVICE TO USE THE TRANSPORT 'MONOLOG' ?
    monolog_mailer_transport:
        class: Symfony\Component\Mailer\Transport\TransportInterface
        factory: ['@mailer.transports', 'get']
        arguments: ['monolog']
Share Improve this question edited Nov 20, 2024 at 8:59 yivi 47.7k18 gold badges130 silver badges154 bronze badges asked Nov 20, 2024 at 8:43 sdespontsdespont 14k9 gold badges60 silver badges99 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1 +200

Unfortunately, at the moment, there's no out-of-the-box way to use a alternative transport for sending emails with logs. However, you can "hack" the system to achieve this. Here's how you can do it:

First, define the transport service:

monolog_transport:
    class: Symfony\Component\Mailer\Transport\TransportInterface
    factory: 'Symfony\Component\Mailer\Transport::fromDsn'
    arguments:
        - 'sendmail://default' // replace it to your ENV var

Then, define the mailer service:

monolog_mailer:
    class: Symfony\Component\Mailer\Mailer
    arguments:
        $transport: '@monolog_transport'
        $bus: null

Important: We set $bus to null. If you don't do this, the email will be sent asynchronously, and you may lose the transport information, causing the wrong transport to be used. This is a bug, and there's a corresponding GitHub issue: https://github/symfony/symfony/issues/57871

Configure Monolog to use the custom mailer:

mail:
    type: symfony_mailer
    level: error  # Set the desired log level
    formatter: monolog.formatter.html  # Or any other format you need
    to_email: '[email protected]'  # Replace with your email address
    from_email: '[email protected]'  # Sender's email
    subject: 'Symfony Application Error Logs'
    mailer: monolog_mailer // changed

If disabling asynchronicity is not an option (and just a more clean way):

In this case, you will need to create a custom service for Monolog that interacts with the mailer inside the service. This will allow you to manage the transport behavior directly from Monolog without the asynchronicity issue.

Or you can try to change mailer to something else, like SwiftMailer, maybe it doesn't have this kind of bug.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742370869a4431233.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信