php - 电子邮件无法在 Swiftmailer Symfony2 中发送

标签 php email symfony swiftmailer

我在 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

中有 swiftmailer
$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);

来源:Symfony2 documentation .

关于php - 电子邮件无法在 Swiftmailer Symfony2 中发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27204717/

相关文章:

mysql - Symfony 在 MySQL 数据库中设置日期时间

php - 如何使用 Twig 变量作为表单字段名称

php - Symfony/Doctrine - 根据现有数据库值查询日期值

symfony - 检查是否定义了功能/扩展

php - 实现 smiliar_text 或 Levenshtein 查询

android - 单击按钮时从 Android 应用程序发送电子邮件

java - 如何检查 Gmail 的身份验证?

html - HTML 电子邮件中的 PayPal 按钮/隐藏输入

php - 如何在 php 中回显斜杠?

php - 防止 xdebug 在索引文件的第一行中断