如何通过电子邮件发送多个附件

时间:2020-10-22 07:51:20

标签: javascript google-apps-script gmail-api

我有一个项目,可以从Google表单中创建PDF并通过Google脚本中的电子邮件发送多个PDF,

相关的CODE段如下,

func encAndDec(){
        do {
            // Encryption start
            let data =  Data.init(base64Encoded: "12345678901234567890123456789012".base64Encoded()!)
            let iv : Array<UInt8> = [0,0,0,0,0,0,0,0,0,0,0,0]
            let nIv = Data(iv)
            let gcmEnc = GCM(iv: nIv.bytes, mode: .detached)
            var enc = try? AES(key: data!.bytes, blockMode: gcmEnc, padding: .noPadding).makeEncryptor()
            let arrStr = ["My name is tarun"] // Working
            //let arrStr = ["tarun"] // Not working for this string
            var ciphertext = Array<UInt8>()
            for txt in arrStr{
                 let ciperText = try? enc?.update(withBytes: Array(txt.utf8)) // Getting nil for small string.
                 ciphertext.append(contentsOf: ciperText!)
            }
            var res = try? enc?.finish()
            gcmEnc.authenticationTag = self.randomGenerateBytes(count: 16)?.bytes
            res?.append(contentsOf: (gcmEnc.authenticationTag)!)
            let cipherData = Data(ciphertext) + Data(res!)
            let strEnc = String(decoding: cipherData, as: UTF8.self)
            print(strEnc)
        
            // Decryption start from here
            do {
                let gcmDec = GCM.init(iv: nIv.bytes, additionalAuthenticatedData: nil, tagLength: 16, mode: .detached)
                var aesDec = try! AES(key: data!.bytes, blockMode: gcmDec, padding: .noPadding).makeDecryptor()
                let tag_length = 16
                let encData = cipherData.subdata(in: 0..<cipherData.count - tag_length)
                let tag = cipherData.subdata(in: encData.count ..< cipherData.count)
                
                let decData = try? aesDec.update(withBytes: encData.bytes) //Getting nil here for small string
                let strData = String(decoding: decData!, as: UTF8.self)
                print(strData)
                
                do{
                    var res = try? aesDec.finish(withBytes: tag.bytes)
                    res?.append(contentsOf: tag)
                    
                }catch{
                    
                }
                
            } catch {
                // failed
            }
    }
    }
    func randomGenerateBytes(count: Int) -> Data? {
        let bytes = UnsafeMutableRawPointer.allocate(byteCount: count, alignment: 1)
        defer { bytes.deallocate() }
        let status = CCRandomGenerateBytes(bytes, count)
        guard status == kCCSuccess else { return nil }
        return Data(bytes: bytes, count: count)
    }

我设法发送了带有单个附件的电子邮件,但是由于我在这里循环浏览for循环,所以我不知道该怎么做。

2 个答案:

答案 0 :(得分:0)

创建一个全局数组,并在每个循环中push将文件迭代到该数组中

示例修改:

...
var files = [];
for (var Sample_Number in TF_Samples) {      
  ...
  Report_PDF_Version = Report_Folder_PDF.createFile(Blob_PDF).setName(data[0]+"-SPORE TRAPE");
  files.push(Report_PDF_Version);
}
GmailApp.sendEmail(recipient, subject, body, {attachments:files});
...

答案 1 :(得分:0)

根据documentation中的规定,附件参数必须是斑点数组。

因此,不要将blob转换为驱动器文件,而只是像这样发送blob:

Blob_PDF = Report_File_Word.getAs(MimeType.PDF);

并将这些blob逐一推送到数组中。

该参数将有效。