9
4

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 5 years have passed since last update.

Laravelで文字コードISO-2022-JPのメールを送る

Posted at

Laravel 5.4で動作確認

<?php

namespace App\Mail;

use Illuminate\Mail\Mailable as BaseMailable;
use Swift;
use Swift_DependencyContainer;
use Swift_Preferences;

class Mailable extends BaseMailable
{
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct()
    {
        Swift::init(function () {
            Swift_DependencyContainer::getInstance()
               ->register('mime.qpheaderencoder')
               ->asAliasOf('mime.base64headerencoder');
        });

        Swift_Preferences::getInstance()
            ->setCharset('ISO-2022-JP');
    }

    /**
     * {@inheritdoc}
     */
    public function subject($subject)
    {
        $this->subject = sprintf(
            '=?ISO-2022-JP?B?%s?=',
            base64_encode(mb_convert_encoding($subject, 'JIS', 'UTF8'))
        );

        return $this;
    }
}
9
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
9
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?