duandushang5148 2015-11-13 15:41
浏览 74

Symfony2 swift邮件如何发送

I'm new on Symfony.. I would send an email with swift mailer but i don't understand if this service is enabled from FOSUserbundle or not..

I write this code in 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 i have this for 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

How can i do for send and email? Thanks

Edit with 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条回答 默认 最新

  • douri4459 2015-11-13 18:08
    关注

    Symfony2 have an important principe, all components are standalone.

    For each piece of Symfony you'll use, if you can see an available configuration for it in your config.yml, you must configure it. Especially if the configuration is empty.

    As you can see in your config.yml, parameters are called instead of raw expressions.

    In fact, a parameters.yml configuration file exists in the same directory (config/). Your config.yml call it automatically to get parameters it need. Set your e-mail configuration in your parameters.yml(password, user, host ..), and then you can use it in your bundles, including FOSUserBundle and other external bundles.

    An example of swiftmailer configuration for local postfix :

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

    All you need to do the trick is in the documentation. https://symfony.com/doc/current/cookbook/email/email.html

    Have a good experience with Symfony !

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作