LoginSignup
11
11

More than 5 years have passed since last update.

CakePHP3.1.xでContent-Tranfer-Encoding:base64のEmailを送る

Posted at

CakePHP3のEmailクラス

CakePHP3系ではEmailを送信を行うために、Emailクラスが用意されている。
3.0.xと3.1.xでnamespaceが異なり、3.0.xではCake/Network\Email\Email、3.1.xではCake\Mailer\Emailとなっている。

設定ファイル

config/app.phpに設定を記述する。
app.phpの中でEmailクラスの動作に関する設定のキーは'EmailTransport''Email'の2つに分かれている。

EmailTransportに設定する内容

  • 使用するTransport (Mail | Smtp | debug)
  • Smtpを使用する際の各種パラメータ

Emailに設定する内容

  • 送信するメールに関する各種パラメータ

設定の記述例

config/app.php
'EmailTransport' => [
    'test-transport' => [ //任意の設定名
        'className' => 'Mail', //Mail or SMTP
        // SMTPの場合には、接続先に関する設定を記述
    ],
],

'Email' => [
    'default' => [ //任意の設定名
        'transport' => 'test-transport', // 'EmailTransport'で指定した設定名
        'from' => 'from@example.com',
        'bcc' => 'bcc@example.com',
    ],
],

Emailクラスを使用したメールの送信例

コード

<?php
namespace App\Shell;

use Cake\Console\Shell;
use Cake\Mailer\Email;

class MailShell extends Shell
{

    public function main()
    {
        $email = new Cakephp3Base64Email('default');
        $mail = $email->to('to@example.com')
                      ->subject('日本語のSubject')
                      ->send('こんにちは');
        debug($mail);
    }
}

実行結果

########## DEBUG ##########
[
        'headers' => 'From: from@example.com
To: to@example.com
Date: Sat, 19 Dec 2015 05:47:01 +0000
Message-ID: <1539be147f13422484f56c9797600168@localhost>
Subject: =?UTF-8?B?5pel5pys6Kqe44GuU3ViamVjdA==?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit',
        'message' => 'こんにちは

'
]
###########################

Content-Transfer-Encoding:base64 でメールを送りたい

Emailの環境は様々で、Emailクラスを使用したの例のようにcharset=UTF-8でContent-Transfer-Encoding:8bitのメールを正常に受信・表示できない環境も存在する。
Content-Transfer-EncodingはBase64にしたいというケースはまだまだあるだろうと思う。

Cakephp3Base64Emailクラス

メールの本文をBase64エンコードし、Content-Transfer-Encoding: Base64 で送信をするCakehphp3Base64Emailクラスを作成した。
Packagistで公開しているので、comporserでインストールできる。

インストール

how-to-install
composer require oh-sky/cakephp3-base64-email

設定

Emailクラスと共通

使用例

コード

<?php
namespace App\Shell;

use Cake\Console\Shell;
use OhSky\Cakephp3Base64Email;

class MailShell extends Shell
{

    public function main()
    {
        $email = new Cakephp3Base64Email('default');
        $mail = $email
                      ->to('to@example.com')
                      ->subject('日本語のSubject')
                      ->send('こんにちは');
        debug($mail);
    }

}

実行結果

########## DEBUG ##########
[
        'headers' => 'From: from@example.com
To: to@example.com
Date: Sat, 19 Dec 2015 06:12:52 +0000
Message-ID: <641683d921924f7ea3006b8cc7f95475@localhost>
Subject: =?UTF-8?B?5pel5pys6Kqe44GuU3ViamVjdA==?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: base64',
        'message' => '44GT44KT44Gr44Gh44GvCg==

'
]
###########################

このように、本文がBase64でエンコードされ、ヘッダのContent-Transfer-Encodingの値がbase64になる。

11
11
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
11
11