Can swiftMailer be used on localhost to send emails?
Asked Answered
I

3

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();
}
Interradial answered 1/3, 2015 at 1:25 Comment(2)
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 #4486135 or google.com.au/#q=swiftmailer+localhostHartwell
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.Hartwell
U
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('[email protected]')
    ->setSubject('This is a test mail ' )
    ->send();
Ungodly answered 3/3, 2015 at 5:43 Comment(1)
How is this localhost? The transport you've listed is gmail.Calabrese
O
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);

    ?>
Ommatidium answered 12/9, 2016 at 8:19 Comment(0)
R
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

Rebec answered 18/7, 2017 at 23:36 Comment(1)
Exactly. Here's a quote from the documentation about useFileTransport: "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)Lazaro

© 2022 - 2024 — McMap. All rights reserved.