如何在Symfony中发送电子邮件而不运行消费命令?

huangapple go评论122阅读模式
英文:

How to send email from Symfony without running consume command?

问题

我想从Symfony 6.2发送电子邮件。我按照文档中的指南编写了以下代码(gmail地址为示例,我正在使用自己的域名):

$email = (new Email())
    ->from("from@gmail.com")
    ->to("go@gmail.com")
    ->subject("这是我的主题")
    ->html("<p>这是邮件内容</p>");
    
$mailer->send($email);

我还编辑了.env文件。

  • MESSENGER_TRANSPORT_DSNdoctrine://default?autosetup=false更改为doctrine://default
  • 添加了MAILER_DSN=smtp://from%40gmail.com:mdp@ssl0.ovh.net

实际上,电子邮件并不会自动发送,Symfony表示它们被排队了。这是正确的,因为它创建了带有排队标志的消息表。我应该使用php bin/console messenger:consume -vv async命令并让其运行,以查看电子邮件是否发送。

以下是我的config/packages/messenger.yaml文件:

framework:
    messenger:
        failure_transport: failed
        transports:
            async:
                dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
                options:
                    use_notify: true
                    check_delayed_interval: 60000
                retry_strategy:
                    max_retries: 3
                    multiplier: 2
            failed: 'doctrine://default?queue_name=failed'

        routing:
            Symfony\Component\Mailer\Messenger\SendEmailMessage: async
            Symfony\Component\Notifier\Message\ChatMessage: async
            Symfony\Component\Notifier\Message\SmsMessage: async

如何使电子邮件从PHP发送(或者至少不需要使用命令)?

英文:

I want to send email from Symfony 6.2. I followed the documentation and made this code (gmail address is example, I'm using own domain name):

$email = (new Email())
    -&gt;from(&quot;from@gmail.com&quot;)
    -&gt;to(&quot;go@gmail.com&quot;)
    -&gt;subject(&quot;Here is my subject&quot;)
    -&gt;html(&quot;&lt;p&gt;Here is the content&lt;/p&gt;&quot;);

$mailer-&gt;send($email);

I also edit the file .env.

  • I changed MESSENGER_TRANSPORT_DSN from doctrine://default?autosetup=false to doctrine://default
  • I added MAILER_DSN=smtp://from%40gmail.com:mdp@ssl0.ovh.net

And, actually it doesn't send email automatically, Symfony say they are queued. And it's right as it created the table with messages, put queued on them. I should use the command php bin/console messenger:consume -vv async and let it running to see email be sent.

Here is my config/packages/messenger.yaml file:

framework:
    messenger:
        failure_transport: failed
        transports:
            async:
                dsn: &#39;%env(MESSENGER_TRANSPORT_DSN)%&#39;
                options:
                    use_notify: true
                    check_delayed_interval: 60000
                retry_strategy:
                    max_retries: 3
                    multiplier: 2
            failed: &#39;doctrine://default?queue_name=failed&#39;

        routing:
            Symfony\Component\Mailer\Messenger\SendEmailMessage: async
            Symfony\Component\Notifier\Message\ChatMessage: async
            Symfony\Component\Notifier\Message\SmsMessage: async

How can I make email be sent from php (or at least not necessary to use the command)?

答案1

得分: 2

Symfony 6现在通过消息队列来发送电子邮件。这是一种更好的方法,但如果您想禁用此功能并直接发送电子邮件,您可以注释掉(或删除)Symfony\Component\Mailer\Messenger\SendEmailMessage: async,如下所示:

# config/packages/messenger.yaml
routing:
    # Symfony\Component\Mailer\Messenger\SendEmailMessage: async
    Symfony\Component\Notifier\Message\ChatMessage: async
    Symfony\Component\Notifier\Message\SmsMessage: async
英文:

Symfony 6 now sends email via messenger queuing. This is a better approach, however if you would like to disable this functionality and send email directly, you can comment out (or remove) Symfony\Component\Mailer\Messenger\SendEmailMessage: async like so:

# config/packages/messenger.yaml
routing:
    # Symfony\Component\Mailer\Messenger\SendEmailMessage: async
    Symfony\Component\Notifier\Message\ChatMessage: async
    Symfony\Component\Notifier\Message\SmsMessage: async

huangapple
  • 本文由 发表于 2023年3月9日 15:07:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75681408.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定