LoginSignup
0
1

More than 5 years have passed since last update.

TransmitMail 2 の送信メールの件名に POST パラメータを利用する

Last updated at Posted at 2015-11-04

TransmitMail 2 の送信メールの件名に POST パラメータを利用するカスタマイズ例をご紹介します。

開発および動作確認環境

開発および動作確認環境は下記の通りです。

  • TransmitMail: 2.0.0
  • PHP: 5.6.10

input.html に入力フィールドを追加する

input.html に下記のような入力フィールドを追加します。

input.html
<table>
    <tr>
        <th>お名前</th>
        <td>
            <input type="text" name="お名前" value="{$お名前}">
        </td>
    </tr>
</table>

config.yml を修正する

config/config.ymlto_subjectお問い合わせ とします。

config/config.yml
config:
    to_subject: お問い合わせ

exTransmitMail.php を作成する

TransmiMail クラスを継承した exTransmiMail クラスを lib/exTransmitMail.php に作成します。

lib/exTransmitMail.php
<?php
class exTransmitMail extends TransmitMail
{
}

exTransmitMail クラスに機能を実装する

メール送信の前の処理となる afterSetTemplateProperty メソッドを追加し、そこに処理を書いていきます。( afterXXX メソッドについては Wiki をご参照ください。)

lib/exTransmitMail.php
/**
 * メール送信の前の処理
 */
public function afterSetTemplateProperty()
{
    if ($this->page_name === 'finish') {
        if (!empty($this->post['お名前'])) {
            $this->config['to_subject'] = $this->post['お名前'] . '様からの' . $this->config['to_subject'];
        }
    }
}

index.php を修正する

index.phplib/exTransmitMail.php の読み込みと TransmitMail クラスではなく exTransmitMail クラスを利用するように修正します。

index.php
<?php
require_once 'lib/TransmitMail.php';
require_once 'lib/exTransmitMail.php';
$tm = new exTransmitMail('config/config.yml');
$tm->run();

動作確認

メールの件名に お名前 の内容が入っていることが確認できました。

まとめ

以上、TransmitMail 2 の送信メールの件名に POST パラメータを利用するカスタマイズ例をご紹介でした。こんな感じでカスタマイズすることで、TransmitMail本体には手を加えずにカスタマイズすることが可能です。

今回のカスタマイズのコードは下記ブランチで公開をしています。

dounokouno/TransmitMail at example_dynamic_mail_subject

修正ファイルは下記のコミットをご参照ください。

送信メールの件名に「お名前」フィールドの値を挿入するカスタマイズ例 · dounokouno/TransmitMail@85d82f2

0
1
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
1