7

I am currently not receiving emails using the below configuration and was wondering if's it something to do with my set up maybe missing something or it doesnt work on MAMP localhost?

main-local.php in common config directory

    'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'viewPath' => '@common/mail',
        // 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' => true,
    ],

And then to send the email (which does display a success message)

public function submitreview($email)
{
    //return Yii::$app->mailer->compose(['html' => '@app/mail-templates/submitreview'])
    return Yii::$app->mailer->compose()
        ->setTo($email)
        ->setFrom([$this->email => $this->name])
        ->setSubject($this->title)
        ->setTextBody($this->description)
        ->attachContent($this->file)
        ->send();
}
ankitr
  • 5,992
  • 7
  • 47
  • 66
con322
  • 1,119
  • 7
  • 16
  • 29
  • swiftMailer should work on localhost, but you will need to specifier your ISP mail servers and possibly use a username and password with them. I would refer to http://stackoverflow.com/questions/4485635/php-swiftmailer-localhost-test-setup or https://www.google.com.au/#q=swiftmailer+localhost – Angry 84 Mar 01 '15 at 02:30
  • But if you mean using localhost as a mail server, not hosting your site on localhost and sending mail through say gmail mail servers.. Then you will need something like mercury mail to act as a mail server. – Angry 84 Mar 01 '15 at 02:31

3 Answers3

4

You can send mail through localhost in Yii2 with following config.

'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'useFileTransport' => false,
        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => 'smtp.gmail.com',  
            'username' => 'ENTER_EMAIL_ADDRESS_HERE',
            'password' => 'ENTER_PASSWORD',
            'port' => '587', 
            'encryption' => 'tls', 
        ],
    ]

and in your controller

\Yii::$app->mail->compose('your_view', ['params' => $params])
    ->setFrom([\Yii::$app->params['supportEmail'] => 'Test Mail'])
    ->setTo('to_email@xx.com')
    ->setSubject('This is a test mail ' )
    ->send();
ankitr
  • 5,992
  • 7
  • 47
  • 66
2

I simply m using gmail for testing, used this php file to send mail from local host. When you're going for production, replace the transport file with your original credentials.

the $result will echo 1, if the mail is successfully sent

<?php
            $subject="Testing Mail";
            $body="<h2>This is the body</h2>";
            $to="*******@gmail.com";  //this is the to email address

            require_once 'swiftmailer/swift_required.php';
            // Create the mail transport configuration

            $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')->setUsername('######')->setPassword('######');
            //$transport = Swift_MailTransport::newInstance();
            $message = Swift_Message::newInstance()
            ->setSubject($subject)
            ->setFrom(array('######@gmail.com'))
            ->setTo(array($to))
            ->setBody($body,'text/html');

            //send the message 
            $mailer = Swift_Mailer::newInstance($transport);         
            $result=$mailer->send($message);

    ?>
Vinit Jain
  • 23
  • 4
1

When useFileTransport is set to true (Default in development environment) then mails are saved as files in the 'runtime' folder.

For example, if you are using the advanced starter template and signup as a user when in the backend of the site (And using an extension that sends user registration emails), the registration email will be saved in /backend/runtime/mail

Robert Went
  • 2,944
  • 2
  • 17
  • 18
  • Exactly. Here's a quote from the [documentation about `useFileTransport`](https://www.yiiframework.com/doc/api/2.0/yii-mail-basemailer#$useFileTransport-detail): "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." (emphasis mine) – flaviovs Jun 28 '19 at 19:08