LoginSignup
5
3

More than 3 years have passed since last update.

LaravelでHTMLメールに画像を添付する

Posted at

あんまり記事がなかったので忘れないように書いておく

前提条件

  • Homestead
  • Mac
  • Laravel5.8

注意事項

.envファイルの設定とかもないのであくまでメールは飛ぶ前提の記事です。
メール機能はふんわりしか説明しないので、その辺は他の記事みてください(投げやり)

Mail

ではメール送信機能の実装をザックリ進めていきます。
まずはターミナルからプロジェクトディレクトリに移動して以下のコマンドでMailableクラスを生成します。

php artisan make:mail MailSend

MailSendのところは任意

Mailable

コンストラクトにControllerから呼び出す時に引数として渡す$request$contactという変数名で宣言しておきます。

app/Mail/MailSend.php
<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class MailSend extends Mailable
{
    use Queueable, SerializesModels;

    protected $_params;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($contact)
    {
        $this->contact = $contact;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from('test@test.com')
        ->subject('テストメールです。')
        ->with(['contact' => $this->contact])
        ->view('mails/mail_send');
    }
}

今回HTMLメールとして送信するので必ず->view();を使用してください。

Routing

じゃあ後から作成するControllerのルーティングを設定しておきます。

routes/web.php
Route::post('/send', 'MailSendsController@send')->name('send');

Controller

フォームから$requestの中にfileというnameで画像のアップロード情報が入っているという前提で書いていきます。

ファイルのアップロードはstorage/public/uploadというディレクトリに保存します。
あとメール送信が終わったら適当に/thanksというviewファイルにリダイレクトかけておきます。

バリデーションは画像データで5MBまで許容しておきます。

app/Htp/Controllers/MailSendsController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Mail\MailSend;

class MailSendsController extends Controller
{

    public function send(Request $request)
    {
        $this->validate($request, [
            'file' => 'image|mimes:png,jpg,jpeg|max:500000|',
        ]);

        $file_name = $request->file('file')->store('public/upload/');

        $request->file = basename($file_name);

        try {
            Mail::to('test@test.com')->send(new MailSend($request));
        } catch(Exception $e) {
            echo "エラー:" . $e->getMessage();
        }

        return redirect('/thanks');
    }
}

シンボリックリンク

publicからstorageにアクセス出来るようにシンボリックリンクを設置します。
仮想環境のプロジェクトフォルダで以下のコマンドを実行してください。

php artisan storage:link

この時ちゃんとシンボリックリンク先のパスがあってるかどうか確認しないとつまります。。

cd public
ls -la

~~~~~~~~~~省略~~~~~~~~~~
lrwxr-xr-x 1 vagrant vagrant   37 Sep 22 03:32 storage -> /home/vagrant/code/storage/app/public

View(メール文)

ではViewファイルを作成しましょう。

mkdir resources/views/mails
touch resources/views/mails/mail_send.blade.php

メール文↓

resources/views/mails/mail_send.blade.php

<!DOCTYPE html>
<html lang="ja">
    <body>
        <div>
            <p>[添付ファイル]</p>
            <img src="{{ $message->embed('storage/upload/' .$contact->file) }}">
        </div>
    </body>
</html>

これで添付できたはず。

5
3
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
5
3