LoginSignup
6
4

More than 5 years have passed since last update.

Laravel 5.4でiso-2022-jpエンコードのメールを送信する

Posted at

とりあえず動きますが、やっつけ感が否めないです。もっと綺麗に書けそうです。

app/Providers/AppServiceProvider.php
<?php

namespace App\Providers;

...

use Swift;
use Swift_DependencyContainer;
use Swift_Preferences;

class AppServiceProvider extends ServiceProvider
{
    ...

    public function register()
    {
        ...

        // ここを追加
        Swift::init(function () {
            Swift_DependencyContainer::getInstance()
                ->register('mime.qpheaderencoder')
                ->asAliasOf('mime.base64headerencoder');
            Swift_Preferences::getInstance()->setCharset('iso-2022-jp');
        });
    }
}
app/Mail/OrderShipped.php
<?php

namespace App\Mail;

...

use Swift_Mime_ContentEncoder_PlainContentEncoder;

class OrderShipped extends Mailable
{
    ...

    public function build()
    {
        return $this->from('example@example.jp')
                    ->subject('日本語サブジェクト')
                    ->text('emails.order_shipped')
                    ->withSwiftMessage(function ($message) {
                        $message->setCharset('iso-2022-jp')
                                ->setEncoder(new Swift_Mime_ContentEncoder_PlainContentEncoder('7bit'));
                    });
    }
}

参考

6
4
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
6
4