0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Laravel: メールの送信

Last updated at Posted at 2023-12-19

こちらと同様のことを行いました。
【Laravel】Mailableを使ってメールを送信する方法。データや画像を添付する方法や入力データをメール本文に渡す方法

  1. プロジェクトの作成
  2. laravel new mailable
    
  3. .env の設定
  4. 次はただのサンプルです。実際に動作するものと書き換えて下さい。

    .env
    (省略)
    MAIL_MAILER=smtp
    MAIL_HOST="hi-ho.mose-mail.jp"
    MAIL_PORT=587
    MAIL_USERNAME="user@hi-ho.ne.jp"
    MAIL_PASSWORD="password"
    MAIL_ENCRYPTION=tls
    MAIL_FROM_ADDRESS="user@hi-ho.ne.jp"
    MAIL_FROM_NAME="${APP_NAME}"
    (省略)
    
  5. routes/web.php の編集
  6. routes/web.php
    省略
    最後に加える
    Route::get('contact2', 'App\Http\Controllers\MailableController@index');
    Route::post('contact2', 'App\Http\Controllers\MailableController@send');
    
  7. Controller の作成
  8. php artisan make:mail ContactReply
    php artisan make:controller MailableController
    
    app/Mail/ContactReply.php
    <?php
    
    namespace App\Mail;
    
    use Illuminate\Bus\Queueable;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Mail\Mailable;
    use Illuminate\Queue\SerializesModels;
    
    class ContactReply extends Mailable
    {
        use Queueable, SerializesModels;
    
        /**
         * Create a new message instance.
         *
         * @return void
         */
        public function __construct()
        {
            //
        }
    
        /**
         * Build the message.
         *
         * @return $this
         */
        public function build()
        {
            //以下追記
            return $this->view('emails.text')
                        ->subject('メールの標題');
        }
    }
    
    app/Http/Controllers/MailableController.php
    <?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    use Mail;                   //追記
    use App\Mail\ContactReply; //追記
    
    class MailableController extends Controller
    {
        //以下追記
        public function index() //コンタクトフォームの表示
        {
            return view('contact.index');
        }
    
        public function send(Request $request)  //メールの自動送信設定
        {
    	    $to_address = $request['to_address'];
    	error_log($to_address);
    
            Mail::to($to_address)
                  ->send(new ContactReply());
    
            return back()->withInput($request->only(['name']))
                         ->with('sent', '送信完了しました。');    //送信完了を表示
        }
    }
    
  9. View の作成
  10. mkdir resources/views/contact
    mkdir resources/views/emails
    
    resources/views/contact/index.blade.php
    @if ( Session::has('sent'))
    <div>
        <p>{{old('name')}}さん{{ session('sent') }}</p>
    </div>
    @endif
    
    <form action="{{ url('contact2') }}" method="POST">
        @csrf
    
        <p>名前<input type="text" name="name"></p>
        <p>to_address<input type="text" name="to_address"></p>
    
        <input type="submit" value="送信">
    </form>
    
    resources/views/emails/text.blade.php
    emailsディレクトリのtext.blade.phpの内容です
    
    2023年12月19日
    
    PM 19:53
    
    以上
    
  11. サーバーの起動
  12. php artisan serve
    
  13. ブラウザーで http://127.0.0.1:8000/contact2 にアクセス

    image.png

確認したバージョン

$ php artisan --version
Laravel Framework 10.37.3
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?