Sending large HTML emails with Mailgun in XSJS

I’m trying to send emails with big HTML content using Mailgun in XSJS. My current code works for short HTML, but I get a ‘414 Request-URI Too Large’ error when the content is over 2000 characters. Here’s what I have so far:

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

mailRequest.parameters.set('domain', 'mydomain.com');
mailRequest.parameters.set('from', '[email protected]');
mailRequest.parameters.set('to', '[email protected]');
mailRequest.parameters.set('subject', 'Email Subject');
mailRequest.parameters.set('html', emailContent);

How can I modify this to handle larger HTML files? I’m new to XSJS, so a detailed explanation would be super helpful. Thanks!

I’ve encountered this issue before when working with Mailgun and XSJS. The problem stems from trying to send the entire HTML content as a URL parameter, which has size limitations. Instead, you should use the request body to send the HTML content.

Here’s how you can modify your code:

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

var body = '[email protected]&[email protected]&subject=Email Subject&html=' + encodeURIComponent(emailContent);
mailRequest.body = body;

This approach allows you to send much larger HTML content without hitting the URL length limit. Remember to properly encode the HTML to avoid any issues with special characters. Also, ensure your Mailgun API endpoint is correctly set up in your XSJS configuration.

hey mate, i had similar probs with mailgun. try using the request body instead of params. something like this:

var body = 'from=' + encodeURIComponent('[email protected]') + '&to=' + encodeURIComponent('[email protected]') + '&subject=' + encodeURIComponent('Email Subject') + '&html=' + encodeURIComponent(emailContent);
mailRequest.body = body;

this should handle larger html. good luck!