Action

MailRoute list

Posted by Cory Dransfeldt, Last update about 6 years ago

Add email addresses to your mailroute whitelist or blacklist. One address, domain or tld per line.

Steps

  • script

    /*
     * @title: To MailRoute list
     * @author: Cory Dransfeldt
     * @email: cory@coryd.me
     * @notes: add email addresses to your 
      * mailroute whitelist or blacklist. 
      * One address, domain or tld per line.
     */
    var credential = Credential.create("MailRoute", "MailRoute API");
    
    credential.addTextField("email", "Email address");
    credential.addTextField("key", "API key");
    
    var result = credential.authorize();
    
    if (!result) {
    	alert("Failed to obtain credentials. Please check and retry");
    	context.cancel("Failed to obtain credentials");
    } else {
        if (typeof(credential.getValue("email")) === 'undefined' || String(credential.getValue("email")).length == 0) {
    		alert("Mailroute email is blank. Please rerun action and enter email again");
    		credential.forget();
    		context.cancel("Mailroute email was blank");
    
    	} else if (typeof(credential.getValue("key")) === 'undefined' || String(credential.getValue("key")).length == 0) {
    		alert("Mailroute key is blank. Please rerun action and enter token again");
    		credential.forget();
    		context.cancel("Mailroute key was blank");
    	}
    	else {
    		var content = draft.content,
    		    check = content.length,
    		    newDraft = '',
    		    prompt = Prompt.create();
    
            prompt.title = 'Blacklist or whitelist?';
            prompt.addSelect('list', 'Whitelist or blacklist?', ['blacklist','whitelist'], ['blacklist'], false);
            prompt.addButton('Ok');
            prompt.show();
    
    		if (content.length == 0) {
    			alert('Draft is blank');
    			context.cancel('Draft was blank');
    		} else {
    			var lines = draft.content.split("\n"),
    			    http = HTTP.create(),
    			    list;
    
    			if (prompt.fieldValues['list'] == 'blacklist') {
    			    list = 'B';
    			} else {
    			    list = 'W';
    			}
    
    			for (var line of lines) {
    				if (line.length == 0) {
    					newDraft+="\n"
    				} else {
    				    var headers = {"Authorization": "ApiKey " + credential.getValue("email") + ":" + credential.getValue("key")},
    				        params = {"email_account":"/api/v1/email_account/" + credential.getValue("email") + "/","wb":list,"email":line},
    				        response = http.request({
    						  	"url": "https://admin.mailroute.net/api/v1/wblist/",
    					  		"method": "POST",
    					  		"headers": headers,
    						  	"data": params
    						});
    
    						if (response.statusCode === 201) {
    							newDraft+= line + " - OK\n"
    						}
    						else {
    							newDraft+= line + " - Failed\n"
    						  	console.log(response.statusCode);
    						  	console.log(response.error);
    						}
    					}
    				}
    			}
    			editor.setText(newDraft);
    		}
    	}

Options

  • After Success Trash
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.