0
0

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 1 year has passed since last update.

メールワイズで受信したメール内容をkintoneアプリに登録する

Posted at

要望背景

CS業務でメールワイズを使っているがkintoneアプリに内容を転記している。
メールワイズで受信したメール内容をkintoneアプリと連携したい。

参考サイト

以下サイトを参考に作りました。
https://qiita.com/YasutakaShinomiya/items/55f0d01c8a82f391a009

コード内容に改善点はあると思いますが、運用で使えるようになりました。

'use strict';
var coop_kintone = {
    usePage: ['MailView'],
    init: function () {
        this.param = this.getQuery();
        if ($.inArray(this.param.page, this.usePage) < 0) return;  //メール詳細画面のときだけ表示

        this.initDom();  //kintone登録ボタンの設置
    },
    initDom: function () {
        var btnCheckMail = $('<button>').addClass('post_kintone').css({
            width: 'auto',
            color: '#000000',
        }).text('kintoneへ登録').css({
            display: 'inline-block',
            verticalAlign: 'middle',
            fontSize: '1em',
            lineHeight: '1em',
            padding: '10px 10px 10px 10px',
        });

        var btn_div = $('<div>').css({
            margin: '5px 5px 5px 5px',
        });

        btnCheckMail.on('click', function () {
            coop_kintone.post_kintone();
        });
        btn_div.append(btnCheckMail);

        $('div.mailBody').before(btn_div);  //本文の上部にボタンを設置
        return;
    },
    post_kintone: function () {
        var app_id = x;  //kintoneの登録先アプリID

        var hostname = window.location.hostname;
        var mail_body = "";
        var inquiry_way = "";
        var data_UserName = "";
        var data_Phone = "";
        var data_MailTo = "";
        var data_InquiryDatetime = "";
        var tt_el = $('div.mailBody tt:first');

        if (tt_el) {  //プレーンテキストメールの場合、mailBodyの要素(本文)を取り出す。
            mail_body = tt_el.html();
        }

        if (mail_body.indexOf('特定のキーワード') !== -1) {

            inquiry_way = "xxフォーム";

            //本文データを " [ " でスプリット
            var body_ary = mail_body.split('[');

            //データを " ] "でスプリット
            var tmp_ary1 = body_ary[1].split(']'); //問い合わせID
            var tmp_ary2 = body_ary[2].split(']'); //email
            var tmp_ary3 = body_ary[3].split(']'); //名前
            var tmp_ary4 = body_ary[9].split(']'); //電話番号

            //	諸々のデータ整形
            tmp_ary2 = tmp_ary2[1].replace('<br>', '\n');
            tmp_ary2 = tmp_ary2.trim();
            data_MailTo = tmp_ary2.replace('</a>', '');

            tmp_ary3 = tmp_ary3[1].replace('<br>', '\n');
            data_UserName = tmp_ary3.trim().replace(/\s/g, '');

            tmp_ary4 = tmp_ary4[1].replace('<br>', '\n');
            data_Phone = tmp_ary4.trim();

            var body_ary2 = mail_body.split('特定のキーワード');
            var tmp_ary7 = body_ary2[0].match(/(\d+)\-(\d+)\-(\d+) (\d+):(\d+)/); //日時
            data_InquiryDatetime = tmp_ary7[1] + '-' + String(tmp_ary7[2]).padStart(2, '0') + '-' + String(tmp_ary7[3]).padStart(2, '0') + 'T' + tmp_ary7[4] + ':' + tmp_ary7[5] +':00+09:00';

        } else if (mail_body.indexOf('特定のキーワード') !== -1) {

            inquiry_way = "xxフォーム";

            //本文データをbrでスプリット
            var body_ary = mail_body.split('<br>');

            //	データを「:」でスプリット
            var tmp_ary1 = body_ary[7].split(':');
            var tmp_ary2 = body_ary[11].split(':');
            var tmp_ary3 = body_ary[12].split(':');

            //	諸々のデータ整形
            data_UserName = tmp_ary1[1].trim().replace(/\s/g, '');
            data_Phone = tmp_ary2[1].trim();
            data_MailTo = tmp_ary3[1].replace(/(<([^>]+)>)/gi, '').trim();

            var mail_datetime = $('div.mailContentDateTime');
            var mail_datetime_v2 = mail_datetime.html();
            mail_datetime_v2 = mail_datetime_v2.replace(/<..*;/, ' ');
            mail_datetime_v2 = mail_datetime_v2.replace(' ', '/');
            mail_datetime_v2 = mail_datetime_v2.split('/');
            data_InquiryDatetime = mail_datetime_v2[0] + '-' + String(mail_datetime_v2[1]).padStart(2, '0') + '-' + String(mail_datetime_v2[2]).padStart(2, '0') + 'T' + mail_datetime_v2[3] +':00+09:00';

        } else {

            inquiry_way = "test";
        }

        //POST用JSON
        var post_body = {
            app: app_id,
            record: {
                InquiryDatetime: { value: data_InquiryDatetime },
                Inquiry_way: { value: inquiry_way },
                UserName: { value: data_UserName },
                Phone: { value: data_Phone },
                MailTo: { value: data_MailTo },
                Inquiry: { value: mail_body }
            }
        };
        var url = 'https://' + hostname + '/k/v1/record.json';
        var xhr = new XMLHttpRequest();
        xhr.open('POST', url);
        xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
        xhr.setRequestHeader('X-Cybozu-API-Token', 'test'); //kintone APIトークン
        xhr.setRequestHeader('Content-Type', 'application/json');
        xhr.onload = function () {
            if (xhr.status === 200) {
                alert('kintoneに登録しました');
            } else {
                // error
                alert('kintoneへの登録に失敗しました');
                console.log(JSON.parse(xhr.responseText));
            }
        };
        xhr.send(JSON.stringify(post_body));
    },
    getQuery: function () {
        var param = {};
        var _param = $(location).attr('search').substr(1).split('&');
        for (var i = 0; i < _param.length; i++) {
            var v = _param[i].split('=');
            param[v[0]] = v[1];
        }
        return param;
    }
};
$(function () { coop_kintone.init() });
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?