Symfony2 swift邮件如何发送

时间:2015-11-13 15:41:34

标签: php symfony email swiftmailer

我是Symfony的新人.. 我会发送一封包含swift邮件的电子邮件,但我不明白这项服务是否是从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);
}

IN config.yml我有这个用于FOSU​​serBundle

# 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: -----

1 个答案:

答案 0 :(得分:1)

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

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

正如您在config.yml中看到的那样,调用参数而不是原始表达式。

实际上,parameters.yml配置文件存在于同一目录(config/)中。 您的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有很好的经验!