2016-08-27 59 views
2

我爲日曆使用ics獲得成功的事件,但我想用.ics附件發送一些文本,爲此我嘗試此代碼,只是看到並告訴我在哪裏必須更改。 我得到event.ics文件,但想發送郵件文本/ html內容的文件。如何在節點js中將email文件附加到.ics文件中?

var ics = require('ics'); 
var options = { 
    eventName: 'Fingerpainting lessons', 
    filename: 'event1.ics', 
    dtstart: date, 
    location: 'Here and there', 
    email: { 
     name: 'alex bob', 
     email: email 
    } 
}; 
ics.createEvent(options, null, function(err, calendar) { 
    if (!err) 
    { 
      console.log('Event file',calendar); 
      var filePath = path.join('vabo_email', 'email.html'); 
       fs.readFile(filePath, {encoding: 'utf-8'}, 
       function(err, data) { 
         if (! err) 
         { 

          var helper = require('sendgrid').mail; 

          from_email = new helper.Email('[email protected]') 
          to_email = new helper.Email(email) 
          subject = subject 
          //Dynamic content 
          var data = data.replace("$content", bodycontent) 

          content = new helper.Content('text/html', data) 
          //content = new helper.Content("text/Calendar",calendar) 
          mail = new helper.Mail(from_email, subject, to_email,content); 

          var sg = require('sendgrid')('secretekey'); 

          var requestBody = mail.toJSON(); 
          var request = sg.emptyRequest(); 
          request.method = 'POST'; 
          request.path = '/v3/mail/send'; 
          request.body = requestBody; 
          data = new helper.Content("text/Calendar",calendar) 
          request.attachments = [{'filename': 'calendar.ics', 'content': data}] 

          sg.API(request, function (error, response) { 
           if (! error) 
           { 
             console.log('mail send Successfully to',email); 
           } 
           else 
           { 
             console.log('Error for send mail',error); 
           } 
          }); 
         } 
         else 
         { 
           console.log('Error for calendar',err); 
         } 
         }); 
    } 
    else 
    { 
      console.log('Error for creating Event',err); 
    } 
}); 
+0

你會想要刪除並重新創建一個新的API密鑰(在將其包含在上面的代碼片段中後)。如果我是你,我會盡快做到這一點。 –

+0

我看到你編輯了上面的代碼。請確保您仍然銷燬該密鑰並創建了一個新密鑰,因爲任何人都可以查看編輯歷史記錄以查看更改內容以及獲取該API密鑰。 –

+0

遐我改變了它 – Alex

回答

2

日曆只包含文件路徑和名稱。你需要讀入文件內容,然後你需要Base64對文件內容進行編碼最後request.attachments是不正確的。將附件添加到郵件對象或requestBody。

mail = new helper.Mail(from_email, subject, to_email,content); 
var base64Content = fs.readFileSync(calendar).toString('base64'); 
mail.attachments = [{'filename': 'calendar.ics', 'content': base64Content, 'type': 'text/Calendar'}] 

var sg = require('sendgrid')('secretekey'); 

var requestBody = mail.toJSON(); 
var request = sg.emptyRequest(); 
request.method = 'POST'; 
request.path = '/v3/mail/send'; 
request.body = requestBody; 

sg.API(request, function (error, response) { 
+0

謝謝@Justin Steele – Alex

+1

@Alex是否解決了您的問題? –

+0

仍然無法發送文件作爲附件 – Alex