google-apps-script
  • mandrill
  • 2013-10-14 51 views 0 likes 
    0

    我想在我的谷歌應用程序腳本中使用mandrill電子郵件發送api。在谷歌腳本中,我必須使用JSON代碼,但我沒有得到我將如何使用它。我在Google apps腳本中很新。在Google Apps腳本中使用Mandrill API

    var m = new mandrill.Mandrill('XXXXXXXXXXX'); 
    var from_email = "[email protected]"; 
    var to = '[ 
    { 
        "email": "[email protected]", 
        "name": "Recipient Name" 
    } 
    ],'; 
    
    // create a variable for the API call parameters 
    var params = { 
        "message": { 
        "from_email":from_email, 
        "to":[{"email":to}], 
        "subject": "Sending a text email from the Mandrill API", 
        "text": "I'm learning the Mandrill API at Codecademy, it's very difficult." 
        } 
    }; 
    
    function sendTheMail() { 
        // Send the email! 
        alert('this is a mail script'); 
        m.messages.send(params, function(res) { 
        log(res); 
        }, function(err) { 
        log(err); 
        }); 
    } 
    

    我不明白如何在Google Apps腳本中使用此代碼。

    回答

    2

    你需要使用urlfetchapp。

    var url = "https://mandrillapp.com/api/1.0/messages/send.json"; 
    var your_key = "xxxxxxxxxxxxx"; 
    var from_email = "[email protected]"; 
    var to = [{ 
        "email": "[email protected]", 
        "name": "Recipient Name" 
    }]; 
    
    var params = { 
        "key": your_key, 
        "message": { 
         "from_email":from_email, 
         "to":[{"email":to}], 
         "subject": "Sending a text email from the Mandrill API", 
         "text": "I'm learning the Mandrill API at Codecademy, it's very difficult." 
        } 
    }; 
    
    var payload = JSON.stringify(params); 
    
    var options = { 
        'method': 'post', 
        'payload': payload, 
        'contentType' : 'application/json' 
    }; 
    
    var response = UrlFetchApp.fetch(url, options); 
    

    未測試此代碼,但應該是這樣的。

    +0

    提取我得到這個錯誤'要求失敗,返回https://mandrillapp.com/api/1.0/messages/send.json代碼500.' – Aakanksha

    +1

    請參閱我的代碼更改。你需要將json串聯起來。嘗試使用Chrome擴展「郵遞員」嘗試發佈到API。 – AshClarke

    +0

    Hey thnx a tone ...現在它的工作... – Aakanksha

    -2

    我粘貼示例代碼示例以發送Mandrill發送的電子郵件,其中附件文件來自Google雲端硬盤。

    function sendEmail() { 
    
        var MANDRILL_API_KEY = "<<your key here>>"; 
    
        var files = [ 
        "<<Google Drive File ID 1>>", 
        "<<Google Drive File ID 2>>", 
        "<<Google Drive File ID 3>>" 
        ]; 
    
        var recipients = [ 
        { 
         "email": "[email protected]", 
         "name": "Amit Agarwal", 
         "type": "to" 
        }, { 
         "email": "[email protected]", 
         "type": "cc" 
        }, { 
         "email": "[email protected]", 
         "type": "bcc" 
        } 
        ]; 
    
        var attachments = []; 
    
        for (var f in files) { 
        var file = DriveApp.getFileById(files[f]); 
        attachments.push({ 
         "type": file.getMimeType(), 
         "name": file.getName(), 
         "content": Utilities.base64Encode(file.getBlob().getBytes()) 
        }); 
        } 
    
        var params = { 
        "key": MANDRILL_API_KEY, 
        "message": { 
         "from_email": "<<Sender's Email Address>>", 
         "from_name": "<<Sender Name>>", 
         "to": recipients, 
         "attachments": attachments, 
         "headers": { 
         "Reply-To": "[email protected]" 
         }, 
         "subject": "Enter email subject", 
         "text" : "Enter email body in plain text", 
         "html" : "Enter HTML content with <b>tags</b>" 
        } 
        }; 
    
        var response = UrlFetchApp.fetch(
        "https://mandrillapp.com/api/1.0/messages/send.json", { 
         'method': 'POST', 
         'payload': JSON.stringify(params), 
         'contentType': 'application/json' 
        }); 
    
        Logger.log(response.getContentText()); 
    } 
    

    示例代碼是從website ctrlq of Amit Agarwal

    相關問題