我在 Symfony 2.5 项目中使用 Swiftmailer 发送电子邮件。这是我的相关代码
parameters.yml
parameters:
mailer_transport: smtp
mailer_host: mail.domain.com
mailer_username: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f085839582b0949f9d91999ede939f9d" rel="noreferrer noopener nofollow">[email protected]</a>
mailer_password: password
mailer_auth_mode: login
config.yml
swiftmailer:
transport: %mailer_transport%
auth_mode: %mailer_auth_mode%
host: %mailer_host%
username: %mailer_username%
password: %mailer_password%
spool: { type: memory }
Controller :
$message = \Swift_Message::newInstance()
->setSubject('mana')
->setFrom('***@gmail.com')
->setTo('***@yahoo.com')
->setBody('this is test');
$this->get('mailer')->send($message);
我在 appKernel.php
$bundles = array(
...
new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
...
);
电子邮件未发送。你能帮我吗?
最佳答案
以下代码在命令中有效,但我针对 Controller 进行了调整:
$message = \Swift_Message::newInstance()
->setSubject('mana')
->setFrom('****@gmail.com')
->setTo('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5538343b34152c343d3a3a7b363a38" rel="noreferrer noopener nofollow">[email protected]</a>')
->setBody('this is test');
# I removed this line: $this->get('mailer')->send($message);
$mailer = $this->get('mailer');
$mailer->send($message);
$spool = $mailer->getTransport()->getSpool();
$transport = $this->get('swiftmailer.transport.real');
$spool->flushQueue($transport);
关于php - 电子邮件无法在 Swiftmailer Symfony2 中发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27204717/