Problem Sending Emails With Swiftmailer


(Mdeacs87) #1

Hey guys,

I am having problems sending emails using Yii2´s swiftmailer. I have tried using SMTP as well as PHP as transport, neither seem to be sending the emails.

I tried configuring swiftmailer according to the d0x: http://www.yiiframework.com/doc-2.0/yii-swiftmailer-mailer.html

My config:




    'components' => [

        ...

        'mail' => [

            'class' => 'yii\swiftmailer\Mailer',

            'viewPath' => '@common/mail',

            'transport' => [

                'class' => 'Swift_SmtpTransport',

                'host' => 'mail.server.com',

                'username' => 'myUsername',

                'password' => 'myPassword',

                'port' => '587',

            ],

        ],

    ],



Some important details to note:

  • These SMTP credentials work fine using swiftmailer with Yii 1.

  • According to the d0x, when I uncomment the transport property in my config, the default mail() function should be used. Mails are however still not being sent/received.

  • According to the Yii Debugger, the email is successfully sent (see attached image)

  • If I simply use php’s mail() function myself (without using swiftmailer), the email is sent and received fine.

Can anybody please help me out? I’m a noob and I havn’t the foggiest what to try.


(Popalexd) #2

Your emails are stored in runtime/mail folder (backend or frontend it depends on where you run your mail function script) .

If you want to send emails to real email address you have to set "useFileTransport" to false like this


 'components' => [

        ...

        'mail' => [

            'class' => 'yii\swiftmailer\Mailer',

             'useFileTransport'=>'false'

         ],

],

This is a short snippet from baseMailer class :


  /**

     * @var boolean whether to save email messages as files under [[fileTransportPath]] instead of sending them

     * to the actual recipients. This is usually used during development for debugging purpose.

     * @see fileTransportPath

     */

    public $useFileTransport = false;

    /**

     * @var string the directory where the email messages are saved when [[useFileTransport]] is true.

     */


(Mdeacs87) #3

Thanks for the help alexkb9, that was it!


(Serousse) #4

Hello,

I deployed last week Yii2 advanced application using Composer on a WAMP server.

I am also facing problems sending emails using SwiftMailer. I followed Yii2 documentation and I also added ‘useFileTransport’=>‘false’ as described in this post.

Unfortunately, even with this, the emails are not sent. Yii2 creates email files in /runtime/mail folder but doesn’t send the emails. I am not able to find any log reporting an error or a problem.

I searched through all project files to find an unexpected ‘useFileTransport’=>‘true’ but I was unable to find one.

Here is the content of my /common/config/main-local.php:





<?php

return [

    'components' => [


        ...


        'mailer' => [

			'class' => 'yii\swiftmailer\Mailer',

			'useFileTransport'=>'false',

			'transport' => [

				'class' => 'Swift_SmtpTransport',

				'host' => 'smtp.gmail.com',

				'username' => 'xxxxxx@gmail.com',

				'password' => 'yyyyyyyy',

				'port' => '465',

				'encryption' => 'ssl',

				],

		],

        

    ],

  

];




Question: for the name of the "email" component in the configuration file, sometimes we found "mail" and sometimes "mailer". Can we use both of them? As you can see in my config file, I used "mailer".

Thanks in advance for your help.


(Stefano Mtangoo) #5

You use whatever you define in config (you can call it anything, even "car")

So in your case is mailer. The most important is class name and config options!


(Tuga74) #6

For the name you can use whatever you want, but you have to use that name when using the component.

For the fileTransport try without single quotes


'useFileTransport'=>false,


(Serousse) #7

Great! Thanks for your answers. Removing the single quotes solved the issue.

Have a nice weekend.


(Illariose) #8

I have got the same problems.

just mail() function works good on the server, but I can’t send mails from swiftmailer.

This code works without errors:


 Yii::$app->mailer->compose()

            ->setTo('test@test.lol')

            ->setFrom('lol@lol.lo')

            ->setSubject('Invite')

            ->setTextBody('Hello!')

            ->send();

but I can’t see the messages into inbox.

What I must to do, Install swiftmailer on the server? I can’t do that, there is only shared hosting.

Can I use mail function, how to configure mailer to work with it?

my config is:




  'mailer' => [

            'class' => 'yii\swiftmailer\Mailer',

            // send all mails to a file by default. You have to set

            // 'useFileTransport' to false and configure a transport

            // for the mailer to send real emails.

            'useFileTransport' => false,

        ],




(Stefano Mtangoo) #9

Here is a sample config


'mailer' => [

        	'class' => 'yii\swiftmailer\Mailer',

        	'transport' => [

            	'class' => 'Swift_SmtpTransport',

            	'host' => 'mail.lol.co.tz',

            	'username' => 'info@lol.co.tz',

            	'password' => 'lolpasswd',

            	'port' => '25', 

            	'encryption' => 'tls', //depends if you need it

        	],

    	], 


(Kandalf01) #10

Did someone find any solution?
If I use swiftmailer v6 I cant send any email but with swiftmailer v5 it works