Mailer not working

Does anyone know why the mailer would not be working based on this code …

Setup …




'mailer' => [

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

],



Send …




return Yii::$app->mailer->compose()->setTo($toEmail)->setFrom($appFromEmail)->setSubject($this->subject)->setTextBody($this->body)->send();



Setup …




'mailer' => [

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

'useFileTransport' => false,

],



This is default


'useFileTransport' => false,

and not required.

Without the ‘transport’ property configured it should use PHP mail() function. If mail() does not work you have to check your server settings.

Hi,

Yes I am wanting to use the mail() function but the Yii 2 way etc.

It does not appear to be sending any emails, all of the variables are set correctly to, from, subject etc with no errors.

In the code above is how I have set everything up, are you confirming that it should work that way with no problems?

James.

Check again this tutorial http://www.yiiframework.com/doc-2.0/guide-tutorial-mailing.html

If everything is set properly and mails are still not sent prepare a plain php script with mail() function and check if it sends anything so you know which side is giving you errors.

try using straigh php mail function




  <?php

mail('youremailladdress@example.com', 'Test Email', 'Test Message body');

?>   	



if it sends then that means the setting are not correct for swiftmailer. I fought for two days to get this to work.

if it sends then who is your hosting provider and are you using cpanel?

If it doesn’t send then your hosting provider may have mail function blocked and they would have to unblock it if possible.

here are some settings that worked for me




'mailer' => [

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

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

        	//'useFileTransport' => false,

        	'transport' => [

            	'class' => 'Swift_SmtpTransport',

 				'host' => 'mail.mymainhostingdomain.com',//i.e the main site on mine is mysite.org but the site that is sending emails is mysite.com but it needs to be set to mail.mysite.org because that is the main account on the shared hosting.

            	'username' => '',

            	'password' => '',

            	'port' => '587',//need a port

            	'encryption' => 'tls',//must be lowercase for all encryption types or will throw cant find encryption type extension

        	],

    	],






'mailer' => [

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

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

        	//'useFileTransport' => false,

        	'transport' => [

            	'class' => 'Swift_SmtpTransport',

              	'host' => 'mail.mymainhostingdomain.com',//i.e the main  site on mine is mysite.org but the site that is sending emails is  mysite.com but it needs to be set to mail.mysite.org because that is the  main account on the shared hosting.

            	'username' => 'myCpanelAccount',//NOT  an email account

            	'password' => 'myCpanelPwForAboveAccount',

            	'port' => '587',//need a port

            	'encryption' => 'tls',//must be lowercase for all encryption types or will throw cant find encryption type extension

        	],

    	],



Also, if you are using Cpanel and your email is NOT hosted from your cpanel account then you need to make sure you have disabled cpanel from searching for the email inside of cpanel. This will not throw errors but your email won’t ever send.

Select the remote mail exchanger button under email-> mx options or if cpanel is hosting yoru email then use local mail exchanger

I personally use the second option but they both worked for me. Neither of them actually use a email account to send from either so you don’t need one to use those options.

Also, you can try host=localhost and port 25 if those don’t work if you are on shared hosting.

Here is a function to send test emails…i got so frustrated because i was changing the settings so much trying to get it to work i made this





//place in a controller 


	public function actionTestemail() {   

    	Yii::$app->mailer->compose(['html' => 'testemail'], ['logo' =>''])->setFrom('test@test.com')->setSubject(' Test Email')->setTo(Yii::$app->params['adminEmail'])->send();

    	Yii::$app->getSession()->setFlash('success', 

        	'message' => 'Please check the inbox for your admin email account "' . Yii::$app->params['adminEmail'] . '".  If everything went correclty there will be a message with the subject "' . Yii::$app->name . ' Test Email".  Please note it may take a few minutes for you to receive an email.']);

    	return $this->refresh();

	}


//place in a view


<?php echo Html::a('Send Test Email', ['controllerYouPlacedFunctionIn/testemail'], ['class' =>'btn  btn-primary', 'title' => 'Send test email']); ?>



Hope this helps or helps someone because it was super frustrating! But it has worked every time since i got the settings correct and works quickly.