Drafts Action Directory

List email addresses

Posted by @davenicholls, Last update about 1 month ago

This action uses the XMLRPC object and the Webfaction API to pull a current list of email addresses associated with a Webfaction account.

A new draft is created with a MultiMarkdown table of email addresses and their targets (mailboxes and/or forwarding addresses)

Install

Steps

  • script

    // List Webfaction email addresses
    //
    // Lists all email addresses in a Webfaction account, giving the address and the target
    // mailboxes and/or forwarding addresses
    // Output is formatted as a MultiMarkdown table
    //
    
    const url = "https://api.webfaction.com/";
    
    function API_login(username,password) {
    	let methodName = "login";
    	let params = [username,password];
    	return XMLRPC.request(url, methodName, params);
    }
    
    function API_listEmails(sessionID,mailbox) {
    	let methodName = "list_emails";
    	let params = [sessionID];
    	return XMLRPC.request(url, methodName, params);
    }
    
    var credential = Credential.createWithUsernamePassword("Webfaction", "Webfaction API");
    
    var result = credential.authorize();
    
    // Function to sort email addresses
    // Sorts by domain, then address
    function sortEmail(e1,e2) {
    	e1parts = e1[0].split("@");
    	e2parts = e2[0].split("@");
    	if (e1parts[1] != e2parts[1]) {
    		if (e1parts[1]<e2parts[1]) 
    			return -1;
    		if (e1parts[1]>e2parts[1])
    			return 1;
    	}
    	if (e1parts[0] < e2parts[0])
    		return -1;
    	if (e1parts[0] > e2parts[0])
    		return 1;
    	return 0;
    }
    
    if (!result) {
    	alert("Failed to obtain credentials. Please check and retry");
    	context.cancel("Failed to obtain credentials");
    }
    else {
    
    	let response = API_login(credential.getValue("username"),credential.getValue("password"));
    	if (response.success) {
    		sessionID = response.params[0][0];
    		response = API_listEmails(sessionID);
    		if (response.success) {
    			let addresses = [];
    			for (let entry of response.params[0]) {
    							addresses.push([entry["email_address"],entry["targets"]]);
    			}
    			addresses.sort(sortEmail);
    			
    			var d = Draft.create();
    			d.content = "|email address|targets|\n|:-------------|:-------|\n"
    			for (let entry of addresses) { 				d.content+=`|${entry[0]}|${entry[1]}|\n`
    			}
    			d.update();
    			editor.load(d);
    		}
    		else {
    			alert("Fault: " + response.faultCode + ", " + response.error);
    			context.fail();
    		}
    	}
    	else {
    		alert("Fault: " + response.faultCode + ", " + response.error);
    		context.fail();
    	}
    }
    
    

Options

  • After Success Default
    Notification Info
    Log Level Info

Comments

Actions available in the Action Directory are uploaded by community members. Use appropriate caution reviewing downloaded actions before use.