Example of sending mail using smtp implemented by Laravel framework

  • 2021-11-29 23:20:20
  • OfStack

In this paper, an example is given to describe the function of sending mail using smtp realized by Laravel framework. Share it for your reference, as follows:

1. Configuration in the. env file

MAIL_DRIVER=smtp
MAIL_HOST=smtp. Mailbox suffix
MAIL_PORT= Mail Server Send Port
MAIL_USERNAME= Sender Mail Address
MAIL_PASSWORD= the third party login code generated by the sender mailbox
MAIL_FROM_ADDRESS= Send mailbox address
MAIL_FROM_NAME= Sender name

2. mail. php file configuration under config directory

It can not be configured because it will be overwritten by the. env file. (Values will only be taken from. env if they are not available.)

3. app/console/commonds/sendMail. php


namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
class SendMailCommand extends Command
{
  /**
   * The name and signature of the console command.
   *
   * @var string
   */
  protected $signature = 'demo:SendMail';
  /**
   * The console command description.
   *
   * @var string
   */
  protected $description = ' Test script SendMail';
  /**
   * constructor
   */
  public function __construct()
  {
    parent::__construct();
  }
  /**
   * Execute the console command.
   *
   * @return mixed
   */
  public function handle()
  {
    $content = ' This is 1 Test mail .';
    $toMail = ' Target mailbox ';
    Mail::raw($content, function ($message) use ($toMail) {
      $message->subject('[  Test  ]  Test mail SendMail - ' .date('Y-m-d H:i:s'));
      $message->to($toMail);
    });
  }
}

4. Test

cmd Switch to the project root directory and execute


php artisan demo:SendMail

For more readers interested in Laravel related content, please check the topics on this site: "Introduction and Advanced Tutorial of Laravel Framework", "Summary of Excellent Development Framework of php", "Introduction Tutorial of php Object-Oriented Programming", "Introduction Tutorial of php+mysql Database Operation" and "Summary of Common Database Operation Skills of php"

I hope this article is helpful to the PHP programming based on Laravel framework.


Related articles: