google-apps-script
  • mandrill
  • 2013-10-14 149 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

    相关问题