$ ls crystal_folder

Laravel(主要是5)純文字寄信考究

想要寄信但不想寫blade模板!! 來!!

就用Mail::raw()這個方法

記得先在.env這新增SMTP相關資訊,可以參考最下方的"完整乾貨"

<?php
        Mail::raw('內文', function ($message) {
            $message
            ->from('有些SMTP要求要跟設定檔的信箱吻合')
            ->to('要寄的人的信箱')
            ->subject('標題');
        });

參考資料


寄信沒反應?


完整乾貨

.env

MAIL_MAILER=smtp
MAIL_HOST=sandbox.smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=你可以去mailtrap.io註冊一個免費SMTP的帳號
MAIL_PASSWORD=跟密碼
MAIL_ENCRYPTION=tls
# 還是要用office365或gmail,看組織規定/個人喜好

Command版

<?php

namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;

class TestSMTPMail extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'test:smtpmail'; // php artisan test:smtpmail


    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        Mail::raw('測試信內文', function ($message) {
            $message
            ->from('有些SMTP要求要跟設定檔的信箱吻合')
            ->to('要寄的人')
            ->subject('測試信標題');
        });

        if (Mail::failures()) {
            dump("寄件失敗");
        }

        dump("寄件成功");
    }

}

route API版,但是Laravel 9

<?php
// 記得use Mail
Route::get('sendmail', function () {
  try {
    Mail::raw('測試信內文', function ($message) {
        $message
        ->to('要寄的人')
        ->subject('測試信標題');
  });
  } catch (\Exception $e) {  // Mail::failures()在新版是過時的
    dump($e->getMessage());
  }
  return '寄信成功';
});

#laravel #php