英文:
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_DSN
从doctrine://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())
->from("from@gmail.com")
->to("go@gmail.com")
->subject("Here is my subject")
->html("<p>Here is the content</p>");
$mailer->send($email);
I also edit the file .env
.
- I changed
MESSENGER_TRANSPORT_DSN
fromdoctrine://default?autosetup=false
todoctrine://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: '%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
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论