Can swiftMailer be used on localhost to send emails?

I am currently not receiving emails using the configuration below, and wondered if this is related to my setup, maybe something is missing or does not work on the local MAMP host?

main-local.php in the shared configuration 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 send an email (which displays 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(); } 
+5
source share
3 answers

You can send mail through localhost in Yii2 with the following configuration.

 '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(); 
+2
source

I just use gmail for testing, used this php file to send mail from the local host. When you are about to start production, replace the transport file with your original credentials.

the result of $ will be an echo of 1 if the mail was sent successfully

 <?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); ?> 
+2
source

If useFileTransport set to true (by default in the development environment), then messages are saved as files in the runtime folder.

For example, if you use the advanced starter template and register as a user, when in the backend of the site (and using the extension sending emails to register users), the registration letter will be saved in /backend/runtime/mail

0
source

Source: https://habr.com/ru/post/1214403/


All Articles