本文實例講述了Laravel框架實現的使用smtp發送郵件功能。分享給大家供大家參考,具體如下:
1、.env文件中配置
MAIL_DRIVER=smtp
MAIL_HOST=smtp.郵箱后綴
MAIL_PORT=郵件服務器發送端口
MAIL_USERNAME=發送方郵件地址
MAIL_PASSWORD=發送方郵箱生成的第三方登陸碼
MAIL_FROM_ADDRESS=發送郵箱地址
MAIL_FROM_NAME=發送方名稱
2、config目錄下mail.php文件配置
可以不配置,因為會被.env文件覆蓋掉。(只有在.env中沒有的時候才會去該文件中取值)
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 = '測試腳本SendMail';
/**
* constructor
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$content = '這是一封的測試郵件.';
$toMail = '目標郵箱';
Mail::raw($content, function ($message) use ($toMail) {
$message->subject('[ 測試 ] 測試郵件SendMail - ' .date('Y-m-d H:i:s'));
$message->to($toMail);
});
}
}
4、測試
cmd切換到項目根目錄下,執行
php artisan demo:SendMail
更多關于Laravel相關內容感興趣的讀者可查看本站專題:《Laravel框架入門與進階教程》、《php優秀開發框架總結》、《php面向對象程序設計入門教程》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》
希望本文所述對大家基于Laravel框架的PHP程序設計有所幫助。
您可能感興趣的文章:- Laravel中利用隊列發送郵件的方法示例
- 在Laravel框架里實現發送郵件實例(郵箱驗證)
- laravel5.4利用163郵箱發送郵件的步驟詳解
- Laravel5.0+框架郵件發送功能實現方法圖文與實例詳解
- PHP的Laravel框架中使用消息隊列queue及異步隊列的方法
- 淺析Laravel5中隊列的配置及使用
- Laravel使用消息隊列需要注意的一些問題
- 淺談Laravel隊列實現原理解決問題記錄
- Laravel框架隊列原理與用法分析
- Laravel 隊列使用的實現
- laravel5.6 框架郵件隊列database驅動簡單demo示例