javascriptmailgunhana-xs

Mailgun in .xsjs


is there a way to send an email via Mailgun with html page as its content that is longer than ~2000 characters? I have this code, that works perfectly for short html as I believe it is sent in URL address:

var obj = $.request.body.asString();    
var req = new $.web.WebRequest($.net.http.POST, "/messages");
        req.headers.set('Content-Type', encodeURIComponent("application/x-www-form-urlencoded"));

        req.parameters.set("domain", "mailgundomain.com");
        req.parameters.set("from", "me@mailgundomain.com");
        req.parameters.set("to", 'to@email.com');
        req.parameters.set("subject", "subject");
        req.parameters.set("html", obj); //email content

In the code above I receive the file and save it to 'org' variable and then send it to mail. What I need is to probably get my "too large" .html file to the body and then show it as a content of the email. As you probably can see, I'm quite new in .xsjs so the more detailed answer the better. If you need any more info, feel free to ask. Thank you.

Edit1: I should add that when I try to send a larger file, the response I get is "414 Request-URI Too Large".


Solution

  • EDIT

    This seems to be the right approach, jointly figured out by the OP and myself:

    var obj = $.request.body.asString();    
    var req = new $.web.WebRequest($.net.http.POST, "/messages");
    
    // request headers
    req.headers.set('Content-Type', "application/x-www-form-urlencoded");
    
    // request URL parameters
    req.parameters.set("domain", "mailgundomain.com");
    req.parameters.set("from", "me@mailgundomain.com");
    req.parameters.set("to", 'to@email.com');
    req.parameters.set("subject", "subject");
    
    // request body
    req.setBody(encodeURIComponent(message));
    

    The $.web.WebRequest class sends everything you set in the .parameters collection as an URL parameter, even if the request method is POST. This is perfectly all-right, POST requests may have URL parameters. However, URLs are length-limited, as you have noticed.

    The body of a POST request is not length-limited, but you have to do the proper content encoding on your own. The body of a application/x-www-form-urlencoded type request follows the same rules as the URL - key=value pairs separated by & characters.

    var obj = $.request.body.asString();    
    var req = new $.web.WebRequest($.net.http.POST, "/messages");
    
    req.headers.set('Content-Type', "application/x-www-form-urlencoded");
    
    var message = {
        domain: "mailgundomain.com",
        from: "me@mailgundomain.com",
        to: "to@email.com",
        subject: "subject",
        html: obj
    };
    
    req.setBody(urlEncode(message));
    

    where urlEncodedFormat() is a little helper function:

    function urlEncode(obj) {
        return Object.keys(obj).map(function (key) {
            return encodeURIComponent(key) + "=" + encodeURIComponent(obj[key]);
        }).join("&");
    }
    

    Turning objects into an URL-encoded string is a pretty common operation. It's likely that one of the libraries you use already contains a function that does that.

    While the above function is is probably correct (there might be edge cases with undefined or null values), it's preferable not to use a hand-rolled variant. Spend some time looking for the right function in your libraries.

    Maybe WebRequest already does the right thing on its own, I have no way to test it, though. Try setting the message object as the body directly:

    req.setBody(message);