Question

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.

Was it helpful?

Solution

->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? ).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top