この問題に対する最近のアプローチ。
問題
たとえば、これは .getBody() で取得された電子メール本文です。
<div dir="ltr"><div><img src="?view=att&th=1401f70d4881e07f&attid=0.3&disp=emb&realattid=ii_1401f6fc7824ebe1&zw&atsh=1" alt="Inline image 4" width="200" height="180"><br></div><div><br></div><img src="?view=att&th=1401f70d4881e07f&attid=0.2&disp=emb&realattid=ii_1401f6e6c1d46c4b&zw&atsh=1" alt="Inline image 2" width="200" height="65"><div><br></div><div>
jtykuykyu</div><div><br></div><div><img src="?view=att&th=1401f70d4881e07f&attid=0.1&disp=emb&realattid=ii_1401f6e9df3a4b1c&zw&atsh=1" alt="Inline image 3" width="200" height="82"><br><div><br></div><div><br></div></div></div>
メールの添付ファイルのリストは次のとおりです (インライン画像も含まれます)。
[13-07-30 08:28:08:378 CEST] スクリーンショット 2013-07-12 1.54.31 PM.png
[13-07-30 08:28:08:379 CEST] スクリーンショット 2013-07-23 5.38.51 PM.png
[13-07-30 08:28:08:380 CEST] スクリーンショット 2013-07-25 9.05.15 AM.png
[13-07-30 08:28:08:381 CEST] test2.png
ご覧のとおり、これらの画像の名前と img タグで使用できる情報との間にリンクはありません。そのため、これらの情報だけで正しい電子メールを安全に再構築する方法はありません。
ソリューション
それを解決する方法は?メソッド .getRawContent() を使用して実際の電子メールを取得し、それを解析して必要な情報を取得できます。具体的には、このメソッドは、添付ファイルの名前と電子メール本文で使用可能な「realattid」との関係を示します。
コンテンツ タイプ: 画像/png; name="2013-07-25 午前 9.05.15 のスクリーン ショット.png"
コンテンツ転送エンコーディング: base64
コンテンツ ID:
X 添付ファイル ID: ii_1401f6e9df3a4b1c
コードスニペット
コード スニペットを次に示します。
-メールの本文と添付ファイルを取得する
-本文内のすべての img タグを取得し、メールの添付ファイルにリンクされているものを確認します
- 各画像の「realattid」を取得し、.getRawContent() を使用して、この「realattid」を正しい添付ファイルにリンクします
-imgタグを置き換えて、正しい添付ファイルに正しくリンクします
- この添付ファイルが単純な添付ファイルではなく、インライン画像であることを示します
-すべての作業が完了すると、正しいインライン画像が表示されたこのメールのコピーを送信するために必要なすべてのデータが揃います。
//////////////////////////////////////////////////////////////////////////////
// Get inline images and make sure they stay as inline images
//////////////////////////////////////////////////////////////////////////////
var emailTemplate = selectedTemplate.getBody();
var rawContent = selectedTemplate.getRawContent();
var attachments = selectedTemplate.getAttachments();
var regMessageId = new RegExp(selectedTemplate.getId(), "g");
if (emailTemplate.match(regMessageId) != null) {
var inlineImages = {};
var nbrOfImg = emailTemplate.match(regMessageId).length;
var imgVars = emailTemplate.match(/<img[^>]+>/g);
var imgToReplace = [];
if(imgVars != null){
for (var i = 0; i < imgVars.length; i++) {
if (imgVars[i].search(regMessageId) != -1) {
var id = imgVars[i].match(/realattid=([^&]+)&/);
if (id != null) {
var temp = rawContent.split(id[1])[1];
temp = temp.substr(temp.lastIndexOf('Content-Type'));
var imgTitle = temp.match(/name="([^"]+)"/);
if (imgTitle != null) imgToReplace.push([imgTitle[1], imgVars[i], id[1]]);
}
}
}
}
for (var i = 0; i < imgToReplace.length; i++) {
for (var j = 0; j < attachments.length; j++) {
if(attachments[j].getName() == imgToReplace[i][0]) {
inlineImages[imgToReplace[i][2]] = attachments[j].copyBlob();
attachments.splice(j, 1);
var newImg = imgToReplace[i][1].replace(/src="[^\"]+\"/, "src=\"cid:" + imgToReplace[i][2] + "\"");
emailTemplate = emailTemplate.replace(imgToReplace[i][1], newImg);
}
}
}
}
//////////////////////////////////////////////////////////////////////////////
var message = {
htmlBody: emailTemplate,
subject: selectedTemplate.getSubject(),
attachments: attachments,
inlineImages: inlineImages
}