Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can swiftMailer be used on localhost to send emails?

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();
}
like image 347
con322 Avatar asked Mar 01 '15 01:03

con322


2 Answers

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('[email protected]')
    ->setSubject('This is a test mail ' )
    ->send();
like image 197
ankitr Avatar answered Oct 13 '22 01:10

ankitr


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);

    ?>
like image 27
Vinit Jain Avatar answered Oct 13 '22 02:10

Vinit Jain