前提・実現したいこと
GASでメールを送信する本文に画像を挿入し、ファイルを添付して送信したいです。
挿入画像と、添付ファイルは毎回添付する数が異なる(0~5個程度)ことから、配列指定して取得しています。
配列により取得したログは、それぞれ下記のようになっています(各ファイルが3個の場合です)。
console.log(tempuList); → tempu1,tempu2,tempu3
console.log(inImgList); → blob1:imgg1,blob2:imgg2,blob3:imgg3
最後に、メールを送るため、optionの設定をしているのですが、
下記の「送信がうまくいく場合」ように記載すれば送信できるのですが、
配列で指定して実行配列指定の場合すると、エラーになってしまいます。
エラー Exception: Invalid argument: attachments
書き方や配列の考え方で間違えているところがあると思うのですが解決できず。。。
わかりづらい説明で恐縮なのですが、ご指導いただけると大変助かります。
該当のソースコード
GAS
1//送信がうまくいく場合 2MailApp.sendEmail(address, subject, body, { 3 inlineImages: {blob1:imgg1,blob2:imgg2,blob3:imgg3}, 4 attachments: [ tempu1,tempu2,tempu3] 5}); 6 7//配列指定の場合(GASを実行してもうまくいかない。) 8let options = { inlineImages:inImgList ,attachments : tempuList}; 9MailApp.sendEmail(address, subject, body, options ) 10 11//optionsの console.log(options)は、下記のようになります。 12{ inlineImages: 'blob1:imgg1,blob2:imgg2,blob3:imgg3', attachments: 'tempu1,tempu2,tempu3' } 13
追記
部分的な箇所の質問でわかりづらいと思いましたのでソース全体を追記しました。
まだエラーがあったり、かなり泥臭いコードだったりで恐縮ですが、ご指導いただけますとありがたいです。
どうぞよろしくお願いします。
該当のソースコード(全体)
GAS
1 2function sendMail() { 3 4 var ss = SpreadsheetApp.getActiveSpreadsheet(); 5 var dtsheet = ss.getSheetByName('シート1'); 6 var d = dtsheet.getLastRow(); //ラスト行取得 7 8 var name = dtsheet.getRange(d, 1).getValues(); 9 var subject = dtsheet.getRange(d, 2).getValues(); 10 var mail = dtsheet.getRange(d, 3).getValues(); 11 var pic1 = dtsheet.getRange(d, 4).getValues(); 12 var pic2 = dtsheet.getRange(d, 5).getValues(); 13 var pic3 = dtsheet.getRange(d, 6).getValues(); 14 var shiryou1 = dtsheet.getRange(d, 7).getValues(); 15 var shiryou2 = dtsheet.getRange(d, 8).getValues(); 16 var shiryou3 = dtsheet.getRange(d, 9).getValues(); 17 18 var attached_files = []; 19 var inlineimg_files = []; 20 21 //挿入画像処理 22 if (dtsheet.getRange(d, 4).isBlank()){ ; } 23 else{ 24 var img1 = DriveApp.getFilesByName(pic1).next(); 25 var imgg1 = DriveApp.getFileById(img1.getId()).getBlob(); 26 var blob_inline1 = '<img src=cid:blob1>'; 27 inlineimg_files.push('blob1:imgg1'); 28 } 29 30 if ( dtsheet.getRange(d, 5).isBlank()){ ; } 31 else{ 32 var img2 = DriveApp.getFilesByName(pic2).next(); 33 var imgg2 = DriveApp.getFileById(img2.getId()).getBlob(); 34 var blob_inline2 = '<img src=cid:blob2>'; 35 inlineimg_files.push('blob2:imgg2'); 36 } 37 38 if ( dtsheet.getRange(d, 6).isBlank()){ ; } 39 else{ 40 var img3 = DriveApp.getFilesByName(pic3).next(); 41 var imgg3 = DriveApp.getFileById(img3.getId()).getBlob(); 42 var blob_inline3 = '<img src=cid:blob3>'; 43 inlineimg_files.push('blob3:imgg3'); 44 } 45 console.log(inlineimg_files) 46 47 //添付ファイル処理 48 if(dtsheet.getRange(d, 7).isBlank()){ ; } 49 else{ 50 var tempu1 = DriveApp.getFilesByName(shiryou1).next(); 51 var tmpu1ID = tempu1.getId(); 52 attached_files.push('tempu1'); 53 } 54 if(dtsheet.getRange(d, 8).isBlank()){ ; } 55 else{ 56 var tempu2 = DriveApp.getFilesByName(shiryou2).next(); 57 var tempu2ID = tempu2.getId(); 58 attached_files.push('tempu2'); 59 } 60 if (dtsheet.getRange(d, 9).isBlank()){ ; } 61 else{ 62 var tempu3 = DriveApp.getFilesByName(shiryou3).next(); 63 var tempu3ID = tempu3.getId(); 64 attached_files.push('tempu3'); 65 } 66 console.log(attached_files) 67 68 var body = 69 name + "様" + "<br>" + 70 "画像挿入" + "<br>" + 71 "写真1" + "<br>" + blob_inline1 + "<br>" + 72 "写真2" + "<br>" + blob_inline2 + "<br>" + 73 "写真3" + "<br>" + blob_inline3 + "<br>" + 74 "添付ファイル:" + attached_files+ "<br>"; 75 76 77 var options = { 78 inlineImages : inlineimg_files, 79 attachments: attached_files,}; //本当はこのoptions で指定したい。 80 81 MailApp.sendEmail({ 82 to: mail, 83 subject: subject, 84 htmlBody: body, 85 options: options, 86 87 //inlineImages: { blob1:imgg1, blob2:imgg2, blob3:imgg3}, 88 //attachments: [ tempu1,tempu2,tempu3 ] 89 90 }); 91 92} 93
回答1件
あなたの回答
tips
プレビュー