【问题标题】:Symfony2 swift mailer how sendSymfony2 swift mailer如何发送
【发布时间】:2015-11-13 15:41:34
【问题描述】:

我是 Symfony 的新手.. 我会用 swift mailer 发送一封电子邮件,但我不知道是否从 FOSUserbundle 启用了此服务..

我在Controller中写了这段代码:

        $this->get('mailer')->send($message);
$mailer = Swift_Mailer::newInstance();

$message = Swift_Message::newInstance()
->setSubject('Hello Email')
    ->setFrom('----@---.com')
    ->setTo('----@---.com')
    ->setBody('You should see me from the profiler!')
;

// Pass a variable name to the send() method
if (!$mailer->send($message, $failures))
{
var_dump($failures);
}

在 config.yml 我有这个用于 FOSUserBundle

# Swiftmailer Configuration
swiftmailer:
    transport: "%mailer_transport%"
    host:      "%mailer_host%"
    username:  "%mailer_user%"
    password:  "%mailer_password%"
    spool:     { type: memory }   

# Routing
be_simple_i18n_routing: ~

# FOSUserBundle Configuration
fos_user:
    db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
    firewall_name: main
    user_class: Dt\EcBundle\Entity\User 
    profile:
        form:
            type: dt_ec_profile
    registration:
        confirmation:
           enabled:    true
        form:
            type: dt_ec_registration
    from_email:
        address:        -----@----.com
        sender_name:    consulent
    service:
        mailer: fos_user.mailer.twig_swift
    resetting:
        email:
            template: DtEcBundle:User:resetting.email.html.twig

如何发送和发送电子邮件?谢谢

使用 parameters.yml 进行编辑

# This file is auto-generated during the composer install
parameters:
    database_driver: pdo_mysql
    database_host: -----
    database_port: null
    database_name: ------
    database_user: root
    database_password: -------
    mailer_transport: smtp
    mailer_host: 127.0.0.1
    mailer_user: null
    mailer_password: null
    locale: it
    secret: ThisTokenIsNotSoSecretChangeIt
    domain: -----
    opentok_key: ----
    opentok_secret: -----

【问题讨论】:

  • 您的代码似乎适合发送电子邮件。有什么问题?
  • @chalas_r for FOSUSERBUNDLE 我的代码很好,但我不明白因为这个代码不能与另一个控制器一起工作。我该怎么办?我看到了日志,但电子邮件无法发送

标签: php symfony email swiftmailer


【解决方案1】:

Symfony2 有一个重要的原则,所有组件都是独立的。

对于您将使用的每一块 Symfony,如果您可以在 config.yml 中看到可用的配置,则必须对其进行配置。 特别是如果配置为空。

正如您在config.yml 中所见,调用的是参数而不是原始表达式。

其实在同一个目录(config/)下存在一个parameters.yml的配置文件。 您的config.yml 会自动调用它以获取所需的参数。 在parameters.yml(密码、用户、主机..)中设置您的电子邮件配置, 然后你可以在你的包中使用它,包括 FOSUserBundle 和其他外部包。

本地后缀的swiftmailer配置示例:

mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: your@email.fr
mailer_password: yourpwd

您需要做的只是在文档中。 https://symfony.com/doc/current/cookbook/email/email.html

有一个很好的 Symfony 体验!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 2013-10-13
    • 2011-01-30
    • 2011-08-11
    • 1970-01-01
    相关资源
    最近更新 更多