GdoMail は、普段お使いの Gmail 画面から、独自ドメインのメールアドレスを差出人(From)に指定して直接メールを送信できる Chrome 拡張機能です。
ユーザー自身が管理する Google Apps Script(GAS)や Cloudflare Workers などのバックエンドを経由し、外部メール配信API(Mailtrap 等)と連携して動作します。
⚠️ 重要(Important)
本拡張機能は現在 Google Apps Script(GAS)にのみ対応しています。 Cloudflare Workers 等の他環境への対応は今後のアップデートで予定しています。
GdoMail を利用するには、メール配信サービスの準備と、中継役となるバックエンドの設置が必要です。
ここでは下記2つのサービス(無料)を利用する方法を説明しています。
※ 他の方法でシステムを独自に構築することも可能です。
/**
* GdoMail バックエンド連携サンプルスクリプト(Mailtrap対応版)
*
* 【セットアップ手順】
* 1. 下部の「★設定エリア」に任意の認証パスワードと Mailtrap の API Token を入力してください。
* 2. 左メニュー「サービス(+)」から「Gmail API」を追加してください。
* 3. 右上「デプロイ」>「新しいデプロイ」>種類「ウェブアプリ」を選択し、
* アクセスできるユーザーを「全員」にしてデプロイしてください。
*/
function doPost(e) {
const props = PropertiesService.getScriptProperties();
const AUTH_TOKEN = props.getProperty('AUTH_TOKEN');
const API_URL = props.getProperty('API_URL');
const API_KEY = props.getProperty('API_KEY');
try {
const data = JSON.parse(e.postData.contents);
const expectedHash = AUTH_TOKEN ? sha256HexGAS(AUTH_TOKEN) : '';
const isAuthorized =
(data.authTokenHash && data.authTokenHash === expectedHash) ||
(data.authToken && data.authToken === AUTH_TOKEN);
if (!isAuthorized) {
return createJsonResponse({ error: 'Unauthorized' });
}
const maxAttachmentMb = getMaxAttachmentMb(props);
// Chrome拡張機能用:設定値のみ取得
if (data.action === 'getConfig') {
return createJsonResponse({
success: true,
maxAttachmentMb: maxAttachmentMb
});
}
// 送信元アドレス データ取得(設定値もあわせて返却)
if (data.action === 'getSenders') {
const raw = props.getProperty('SENDERS');
return createJsonResponse({
success: true,
senders: raw ? JSON.parse(raw) : [],
maxAttachmentMb: maxAttachmentMb
});
}
if (!API_URL || !API_KEY) {
return createJsonResponse({ error: 'API_URL または API_KEY がスクリプトプロパティに設定されていません' });
}
// 添付ファイルサイズ上限チェック
validateAttachmentSize(data.attachments, maxAttachmentMb);
const requestSpec = buildProviderRequest(API_KEY, data);
const options = {
method: 'post',
headers: requestSpec.headers,
muteHttpExceptions: true
};
if (requestSpec.contentType) {
options.contentType = requestSpec.contentType;
}
options.payload = requestSpec.payload;
const response = UrlFetchApp.fetch(API_URL, options);
const statusCode = response.getResponseCode();
const result = response.getContentText() ? JSON.parse(response.getContentText()) : {};
if (statusCode >= 400) {
const errMsg = result.message || result.ErrorMessage || (Array.isArray(result.errors) ? result.errors.join(', ') : null) || 'API Error (' + statusCode + ')';
return createJsonResponse({ error: errMsg, details: result });
}
saveToGmailSent(data);
return createJsonResponse({ success: true});
} catch (err) {
return createJsonResponse({ error: err.message });
}
}
/**
* スクリプトプロパティから添付上限(MB)を取得(デフォルト: 10)
*/
function getMaxAttachmentMb(props) {
const raw = props.getProperty('MAX_ATTACHMENT_MB');
const parsed = Number(raw);
return (raw && !isNaN(parsed) && parsed > 0) ? parsed : 10;
}
/**
* 添付ファイルの合計サイズ検証(Base64長から元のバイト数を算出)
*/
function validateAttachmentSize(attachments, maxMb) {
if (!attachments || attachments.length === 0) return;
const maxBytes = maxMb * 1024 * 1024;
let totalBytes = 0;
attachments.forEach(function(att) {
if (!att.base64) return;
const padding = (att.base64.endsWith('==')) ? 2 : (att.base64.endsWith('=') ? 1 : 0);
const bytes = (att.base64.length * 3) / 4 - padding;
totalBytes += bytes;
});
if (totalBytes > maxBytes) {
const totalMb = (totalBytes / (1024 * 1024)).toFixed(2);
throw new Error('添付ファイルの合計サイズが上限(' + maxMb + 'MB)を超えています。(現在: ' + totalMb + 'MB)');
}
}
/**
* APIリクエスト(HTML / プレーンテキスト / 添付ファイル対応版)
*/
function buildProviderRequest(apiKey, data) {
const toList = data.to || [];
const ccList = data.cc || [];
const bccList = data.bcc || [];
const subject = data.subject || '(件名なし)';
const isPlain = Boolean(data.isPlainText);
const html = data.htmlContent || '';
const text = data.textContent || '';
const senderName = data.senderName || data.senderEmail;
const attachments = data.attachments || []; // [{ name, type, base64 }]
const payload = {
from: { email: data.senderEmail, name: senderName },
to: toList.map(function(e) { return { email: e }; }),
subject: subject
};
if (isPlain) { payload.text = text; } else { payload.html = html; }
if (ccList.length > 0) payload.cc = ccList.map(function(e) { return { email: e }; });
if (bccList.length > 0) payload.bcc = bccList.map(function(e) { return { email: e }; });
if (attachments.length > 0) {
payload.attachments = attachments.map(function(att) {
return { content: att.base64, filename: att.name, type: att.type || 'application/octet-stream', disposition: 'attachment' };
});
}
return {
contentType: 'application/json',
headers: { 'Authorization': 'Bearer ' + apiKey },
payload: JSON.stringify(payload)
};
}
/**
* Gmail 送信データ インポート(HTML / プレーンテキスト / 添付ファイル対応版)
*/
function saveToGmailSent(data) {
const fromHeader = data.senderName
? '=?UTF-8?B?' + Utilities.base64Encode(data.senderName, Utilities.Charset.UTF_8) + '?= <' + data.senderEmail + '>'
: data.senderEmail;
const subjectHeader = '=?UTF-8?B?' + Utilities.base64Encode(data.subject || '(件名なし)', Utilities.Charset.UTF_8) + '?=';
const attachments = data.attachments || [];
const isPlain = Boolean(data.isPlainText);
const bodyContentType = isPlain ? 'text/plain; charset=UTF-8' : 'text/html; charset=UTF-8';
const bodyRawText = isPlain ? (data.textContent || '') : (data.htmlContent || '');
const encodedBody = Utilities.base64Encode(bodyRawText, Utilities.Charset.UTF_8);
const lines = [
'From: ' + fromHeader,
'To: ' + data.to.join(', '),
];
if (data.cc && data.cc.length > 0) {
lines.push('Cc: ' + data.cc.join(', '));
}
if (data.bcc && data.bcc.length > 0) {
lines.push('Bcc: ' + data.bcc.join(', '));
}
lines.push('Subject: ' + subjectHeader);
lines.push('MIME-Version: 1.0');
if (attachments.length === 0) {
lines.push('Content-Type: ' + bodyContentType);
lines.push('Content-Transfer-Encoding: base64');
lines.push('');
lines.push(encodedBody);
} else {
const boundary = '----=_Part_' + Utilities.getUuid();
lines.push('Content-Type: multipart/mixed; boundary="' + boundary + '"');
lines.push('');
lines.push('--' + boundary);
lines.push('Content-Type: ' + bodyContentType);
lines.push('Content-Transfer-Encoding: base64');
lines.push('');
lines.push(encodedBody);
lines.push('');
attachments.forEach(function(att) {
const encodedFileName = '=?UTF-8?B?' + Utilities.base64Encode(att.name, Utilities.Charset.UTF_8) + '?=';
const contentType = att.type || 'application/octet-stream';
const foldedBase64 = att.base64.replace(/.{1,76}/g, '$&\r\n').trim();
lines.push('--' + boundary);
lines.push('Content-Type: ' + contentType + '; name="' + encodedFileName + '"');
lines.push('Content-Disposition: attachment; filename="' + encodedFileName + '"');
lines.push('Content-Transfer-Encoding: base64');
lines.push('');
lines.push(foldedBase64);
lines.push('');
});
lines.push('--' + boundary + '--');
}
const rawMime = lines.join('\r\n');
const encodedMessage = Utilities.base64EncodeWebSafe(rawMime);
Gmail.Users.Messages.insert(
{
raw: encodedMessage,
labelIds: ['SENT']
},
'me'
);
}
/**
* オブジェクト(配列)をJSON形式の文字列に変換
*/
function createJsonResponse(obj) {
return ContentService.createTextOutput(JSON.stringify(obj))
.setMimeType(ContentService.MimeType.JSON);
}
/**
* ハッシュ照合
*/
function sha256HexGAS(text) {
const rawHash = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, text, Utilities.Charset.UTF_8);
return rawHash.map(function(byte) {
const v = (byte < 0) ? byte + 256 : byte;
return ('0' + v.toString(16)).slice(-2);
}).join('');
}
APIトークンやパスワードをコード内に直接書かず安全に管理するため、GASの「プロジェクトの設定」から以下のプロパティを登録します。
| プロパティ名 | 必須/任意 | 設定する値(例) | 説明 |
|---|---|---|---|
AUTH_TOKEN |
必須 | my-secret-password-123 |
Chrome拡張機能側と一致させる任意の認証パスワード |
API_URL |
必須 | https://send.api.mailtrap.io/api/send |
本番送信用のURL ※Sandboxテスト時は下記 https://sandbox.api.mailtrap.io/api/send/{INBOX_ID} |
API_KEY |
必須 | a1b2c3d4e5f6... |
Mailtrap で発行した API Token |
MAX_ATTACHMENT_MB |
任意 | 10 |
添付ファイル合計サイズの上限(MB単位・未設定時はデフォルト 10) |
SENDERS |
任意 | [{"email":"info@example.com","name":"表示名"}] |
拡張機能側へ自動取得させる差出人リスト(JSON配列形式) |
AUTH_TOKEN と同じ文字列不具合の報告やご質問、よくある確認事項については、以下の専用ページをご確認ください。