LoginSignup
1
1

More than 5 years have passed since last update.

TransmitMail 2 で入力内容を Google スプレッドシートに保存する

Posted at

TransmitMail 2 で入力内容を Google スプレッドシートに保存するカスタマイズ例をご紹介します。

開発および動作確認環境

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

  • 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>
    <tr>
        <th>年齢</th>
        <td><input type="text" name="年齢" value="{$年齢}"></td>
    </tr>
    <tr>
        <th>メールアドレス</th>
        <td><input type="text" name="メールアドレス" value="{$メールアドレス}"></td>
    </tr>
</table>

Google Developers Console の設定をする

プロジェクトの作成

下記 URL にアクセスして、プロジェクトを作成します。

「プロジェクト名」と「プロジェクト ID」は任意の名前を設定します。

Google API の設定

「Google API を利用する」をクリックします。

「Drive API」で検索をして、「Drive API」リンクをクリックします。

「API を有効にする」ボタンをクリックします。

「認証情報」->「認証情報の追加」->「サービスアカウント」をクリックします。

「P12」を選択して、「作成」ボタンをクリックする。

「xxx.p12」という名前のファイルがダウンロードされるので、 config ディレクトリ以下に配置します。

「メールアドレス」をクリックすると「クライアント ID」が確認できます。

「メールアドレス」と「クライアント ID」はあとで必要になるので、メモしておきます。

Google スプレッドシートを用意する

入力フィールドにあわせて下記のような Google スプレッドシートを用意します。

スプレッドシートに権限を追加する

画面右上の「共有」ボタンをクリックして、共有設定画面を開きます。

「Google API の設定」でメモしたメールアドレスに「編集者」権限を与えます。

composer.json を作成する

composer.json を作成して必要なパッケージを記述します。(Composer については、詳しくはググってください。)

composer.json
{
    "require": {
        "asimlqt/php-google-spreadsheet-client": "2.3.*",
        "google/apiclient": "1.1.2"
    }
}

composer install を実行して、パッケージをインストールします。

$ composer install

設定情報を config.yml に追加する

各種設定情報を config/config.yml に追加します。

config/config.yml
config:
    google_client_id: {クライアント ID}
    google_client_email: {メールアドレス}
    google_client_key: {P12 ファイルのファイル名}
    google_client_key_password: notasecret

exTransmitMail.php を作成する

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

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

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

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

lib/exTransmitMail.php
/**
 * メール送信のあとの処理
 */
public function afterSetTemplateAndSendMail()
{
    if ($this->page_name === 'finish') {
        require_once('vendor/autoload.php');

        $google_client = new Google_Client();
        $google_client->setApplicationName('TransmitMail');
        $google_client->setClientId($this->config['google_client_id']);
        $google_client->setAssertionCredentials(
            new Google_Auth_AssertionCredentials(
                $this->config['google_client_email'],
                ['https://spreadsheets.google.com/feeds','https://docs.google.com/feeds'],
                file_get_contents(dirname(__FILE__) . '/../config/' . $this->config['google_client_key']),
                $this->config['google_client_key_password']
            )
        );

        $google_client->getAuth()->refreshTokenWithAssertion();
        $token  = json_decode($google_client->getAccessToken());
        $access_token = $token->access_token;

        $service_request = new Google\Spreadsheet\DefaultServiceRequest($access_token);
        Google\Spreadsheet\ServiceRequestFactory::setInstance($service_request);

        $spreadsheet_service = new Google\Spreadsheet\SpreadsheetService();
        $spreadsheet_feed = $spreadsheet_service->getSpreadsheets();

        $spreadsheet = $spreadsheet_feed->getByTitle('TransmitMailの入力内容を保存するスプレッドシート');
        $worksheet_feed = $spreadsheet->getWorksheets();
        $worksheet = $worksheet_feed->getByTitle('シート1');
        $list_feed = $worksheet->getListFeed();

        $row = [
            'お名前' => $this->post['お名前'],
            '年齢' => $this->post['年齢'],
            'メールアドレス' => $this->post['メールアドレス'],
            '登録日' => date('Y/m/d H:i:s')
        ];

        $list_feed->insert($row);
    }
}

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 で入力内容を Google スプレッドシートに保存するカスタマイズ例をご紹介でした。TransmitMail 2 は Google スプレッドシートへの保存も実装しやすくなりました。

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

dounokouno/TransmitMail at example_google_spreadsheet

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

入力内容を Google スプレッドシートに保存するカスタマイズ例 · dounokouno/TransmitMail@779ef09

参考

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