Board logo

標題: [技術討論] 想用 Google Docs 作為 Email template 咁 send email, 應該點做? [打印本頁]

作者: jacky_chua    時間: 2022-4-24 17:53     標題: 想用 Google Docs 作為 Email template 咁 send email, 應該點做?

我有一份 Google Doc 文件作為示範, 想用 Google Script api 去 get 到個 Document header 做 title, body 做 email content 去 放落我個 email 度之後 send 出去, 我可以點做?

[attach]2316974[/attach]

我有一個 Google Sheet 用來控制 send email, 目標想係上面打上收件人, CC, BCC, 再寫返 Document ID 同 title, 之後就會自己搵返上面我份 document 放落我自己個 email 度
[attach]2316975[/attach]
作者: jacky_chua    時間: 2022-4-24 18:17

回覆 1# jacky_chua

[attach]2316976[/attach]

呢個係我暫時寫既 code
作者: javacomhk    時間: 2022-4-25 12:13

本帖最後由 javacomhk 於 2022-4-25 04:31 編輯

Check the Docs reference to locate the document
https://developers.google.com/apps-script/reference/document

and the GmailApp method is used to send email
https://developers.google.com/ap ... nce/gmail/gmail-app
作者: jacky_chua    時間: 2022-4-25 12:45

我有睇過下, 不過我想將個 Docs 轉成我 email body 可以點做?
作者: javacomhk    時間: 2022-4-25 19:12

本帖最後由 javacomhk 於 2022-4-25 11:27 編輯
我有睇過下, 不過我想將個 Docs 轉成我 email body 可以點做?
jacky_chua 發表於 2022-4-25 04:45


其實就好似Spreadsheet咁整個 table 出來,跟住 app script 攞個 Table Data 來用。最後個 doc 嘅  table 就將個 table property 的 border 變 0 pt 。tables[0].setBorderWidth(0);
[attach]2317139[/attach]
[attach]2317140[/attach]
作者: javacomhk    時間: 2022-4-26 00:05

或者用 getParagraphs() 如果唔係 table 的話。
[attach]2317220[/attach]
[attach]2317221[/attach]
作者: jacky_chua    時間: 2022-4-26 01:07

Okay, 係唔係最好都係Google Docs 用一個大表格裝哂我既資料, 之後就逐個 cell 咁樣 call 返 data 出來會好D?
因為我個 SendEmail Google sheet 係 共用 (姐係諗住只要打個 document ID, 個 script 就會去對應既 Google Docs 自己生成 HTML body 再放落我個 email body 度), 咁我係唔係要去 Google Docs 寫個 script 自己將個 Google Docs content 砌做一個 html ?
作者: javacomhk    時間: 2022-4-26 10:20

本帖最後由 javacomhk 於 2022-4-26 03:03 編輯
Okay, 係唔係最好都係Google Docs 用一個大表格裝哂我既資料, 之後就逐個 cell 咁樣 call 返 data 出來會好 ...
jacky_chua 發表於 2022-4-25 17:07

咁即係mail merge 功能啫。你google 搵吓,好多sample。

https://developers.google.com/ap ... omations/mail-merge
作者: jacky_chua    時間: 2022-4-26 21:05

咁即係mail merge 功能啫。你google 搵吓,好多sample。
javacomhk 發表於 2022-4-26 10:20


佢呢個係用我 Gmail draft 做 template, 不過我係想用 Google Docs, 可唔可行?
作者: justiceice    時間: 2022-4-27 10:04

Google Docs 係文書用,冇預你用嚟做 email 排版,格式複雜少少對方收到隨時會走樣,尤其是 Gmail 及 Outlook

建議放棄 Google Docs,用 MJML
作者: javacomhk    時間: 2022-4-27 11:47

本帖最後由 javacomhk 於 2022-4-27 03:51 編輯
佢呢個係用我 Gmail draft 做 template, 不過我係想用 Google Docs, 可唔可行?
jacky_chua 發表於 2022-4-26 13:05


可以嘅,網上都有的例子係將個 doc template 用 findtext 之後再 replace 完再將佢 save as pdf 檔案再email 出去。你如果唔用 pdf 咪 save 做 google doc ,不過 google doc 個 format 沒咁通用,所以多數的人佢 save 做 pdf 先 email 出去。

你 Google search 下點樣將個 doc template save as 跟住 email  就可以了。
作者: jacky_chua    時間: 2022-4-27 13:17

可以嘅,網上都有的例子係將個 doc template 用 findtext 之後再 replace 完再將佢 save as pdf 檔案再em ...
javacomhk 發表於 2022-4-27 11:47


有呀, 我見到係整左個 template, 之後用 Google Sheet 整好左D field, 之後搵到相對應既 field 就換成想要既 value 再save as PDF...

不過個 email 就變左好似一封郵件夾左個 attachment PDF 而 PDF 先係郵件既內容.... 好似袋中袋咁........
作者: jacky_chua    時間: 2022-4-27 13:17

Google Docs 係文書用,冇預你用嚟做 email 排版,格式複雜少少對方收到隨時會走樣,尤其是 Gmail 及 Outlo ...
justiceice 發表於 2022-4-27 10:04



Extension? Google Workspace 公司唔俾裝 extension 所以用唔到...
作者: javacomhk    時間: 2022-4-27 13:26

本帖最後由 javacomhk 於 2022-4-27 08:38 編輯
有呀, 我見到係整左個 template, 之後用 Google Sheet 整好左D field, 之後搵到相對應既 field 就換成想 ...
jacky_chua 發表於 2022-4-27 05:17


咁 pdf 就一定係 attachement 啦。Google doc 都係  attachement。
一係就用 html template https://developers.google.com/apps-script/guides/html/templates
跟住就 sendMail 用

MailApp.sendEmail(emailAddress, subject, message,{htmlBody:yourhtml});

HTML email message 都可以咁樣有 embedded image, link etc
[attach]2317523[/attach]
作者: jacky_chua    時間: 2022-4-28 18:44

咁 pdf 就一定係 attachement 啦。Google doc 都係  attachement。
一係就用 html template
跟住就 send ...
javacomhk 發表於 2022-4-27 13:26


我知PDF 係 attachment, 但 Google doc 唔係 attachment

我原本個目標係想咁樣
[attach]2317740[/attach]

不過有其他師兄話會走樣
作者: javacomhk    時間: 2022-4-29 04:01

本帖最後由 javacomhk 於 2022-5-1 01:19 編輯
我知PDF 係 attachment, 但 Google doc 唔係 attachment

我原本個目標係想咁樣


不過有其他師兄話會走 ...
jacky_chua 發表於 2022-4-28 10:44


咁你要將個 google doc 轉做 HTML template 黎用,find and replace 的 fields 之後先 send mail。
HTML 響 email 唔會走位。

轉HTML 可以在 Google doc download 手動轉。
或者用 app scripts 轉
參考
https://stackoverflow.com/questi ... le-document-as-html
作者: javacomhk    時間: 2022-4-29 11:06

本帖最後由 javacomhk 於 2022-4-29 03:52 編輯

例如個 google doc 係咁
[attach]2317818[/attach]

個 function 可以咁寫去測試 sendmail。
  1. function sendEmailfromdoc(){
  2.   // fill in google doc ID
  3.   var id = 'AABBCCDDEEffgghhiijjkkllmmnn' ;
  4.   var forDriveScope = DriveApp.getStorageUsed(); //needed to get Drive Scope requested
  5.   var url = "https://docs.google.com/feeds/download/documents/export/Export?id="+id+"&exportFormat=html";
  6.   var param = {
  7.     method      : "get",
  8.     headers     : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
  9.     muteHttpExceptions:true,
  10.   };
  11.   var html = UrlFetchApp.fetch(url,param).getContentText();
  12. // testing first email send out with replaced fields
  13.   var htmlout1 = html.replace("{{title_1}}","This is My Title for testing one");
  14.   htmlout1 = htmlout1.replace("{{heading_1}}","This is heading One");
  15.   htmlout1 = htmlout1.replace("{{heading_2}}","This is heading Two");
  16. //  Logger.log(htmlout1);
  17.   MailApp.sendEmail({
  18.     to: "myrecipient@gmail.com",
  19.     subject: "Testing EMail from Doc",
  20.     htmlBody: htmlout1
  21.     });
  22.   console.log('email sent !!')
  23. }
複製代碼
測試 email 結果:
[attach]2317819[/attach]





歡迎光臨 電腦領域 HKEPC Hardware (https://h0.hkepc.com/forum/) Powered by Discuz! 7.2