简体   繁体   中英

Symfony swiftmail won't send from code

Been struggling with swiftmail for a while now. I have a linux server and properly working sendmail. Sending mail from command line works. And sending with swiftmail via command line works as well so the configuration must be OK. But from code it doesn't. Must have read all the issues of this and I'm totally confused what's going on.

The token is created OK and everything seems to work. No errors in logs. But the mail just doesn't come. I have tried to send to different domains as well. And as I said, it works from command line with mail and swiftmail commands like this:

bin/console swiftmailer:email:send --subject="Test" --body="test" --from="info@domain.com" --to="test@anotherdomain.com"

Here is the current code:

$mailer = $this->get('mailer');
$message = $mailer->createMessage()
    ->setSubject('You forgot password')
    ->setFrom('info@domain.com')
    ->setTo($username)
    ->setBody('You forgot your password. Go and change it from http://url.com/change-password/'.$user->getConfirmationToken());

    $mailer->send($message);

Here is config.yml:

swiftmailer:
transport:  sendmail
host:       /usr/sbin/sendmail -bs

I had the same issue and fixed it by disabling spool on swift mailer.

You didn't specify the Symfony version you are using. If you are using Symfony 4, there should be a config file in /config/packages/swiftmailer.yaml

Here is mine:

swiftmailer:
url: '%env(MAILER_URL)%'
#spool:
    #type: 'memory'
    #type: file
    #path: '%kernel.project_dir%/var/spool'

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