简体   繁体   中英

sender mail isn't the chosen with symfony2

I am trying to send an email from some user to the site owner. I use swiftmailer Bundle of Symfony. this is my code:

Controller

$mailTo = $this->container->getParameter('mailer_user');
    $mailFrom = $message->getEmail();
    $mail = Swift_Message::newInstance()
            ->setSubject('Contact ' . $message->getCountry() . ' from Lemarquis')
            ->setFrom($mailFrom)
            ->setTo($mailTo)
            ->setBody($this->renderView(
                            'WsMailerBundle:Email:message1.html.twig', array(
                        'message' => $message)))
            ->setContentType('text/html');

    $this->get('mailer')->send($mail);

app/config.yml

swiftmailer:
    transport: %mailer_transport%
    encryption: %mailer_encryption%
    auth_mode: %mailer_auth_mode%
    port:      %mailer_port%
    host:      %mailer_host%
    username:  %mailer_user%
    password:  %mailer_password%

app/parameters.yml

parameters:
    database_driver: pdo_mysql
    database_host: localhost
    database_port: null
    database_name: ...
    database_user: ...
    database_password: ...

    mailer_transport: smtp
    mailer_encryption: ssl
    mailer_port: 465
    mailer_auth_mode: login
    mailer_host: smtp.gmail.com
    mailer_user: someaddress@somedomain.tn
    mailer_password: somepwd

The problem is that e-mail is sent from %mailer_user% to %mailer_user% , even with different user addresses.

->setFrom($mailFrom)

You can use this to overwrite %mailer_user% in "FROM" field, however email will still be send from %mailer_user%. This field should be used only to add name of the sender, not to change his email address.

If You still want to change shown email You can try, but You can have problems on many servers (for example GMAIL is blocking that option: How to set 'FROM' property using Swiftmailer with Gmail? ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM