1
Sending an email via the SmarterMail API
Question asked by Doug Erickson - 4/27/2025 at 11:52 AM
Unanswered
Hello -- I am new to the SmarterMail API but have written software to use other APIs in the past and cannot figure out what I'm doing wrong here. 

What I'm doing is in the context of a third-party web hosting environment that uses SmarterMail for its email hosting. My client's website & accompanying web application were recently moved to a virtual private server and I'm trying to add a module to the web application that will enable sending auto-generated emails.

I've got authorization working via the API and am getting the access token. I've also got a function in my API controller object for creating a new user in SmarterMail working successfully. The guts of my function for sending an email are very similar, which unless I'm missing something, I think they should be -- it's just sending different data to a different endpoint. 

In any case, every time I test my sendEmail( ) function, I receive the following JSON reply back with an HTTP 400 Bad Request status: 

{ success: "false", message: "Index was outside the bounds of the array." } 

which leads to predictable questions like "What array?" and "What index?"

Here is the code for sendEmail( ) function, which as you can see is dead simple; you can assume that the "sender" and "recipient" data members of the objEmail argument to the function are valid email addresses, and that objEmail.subject and objEmail.body are simple strings with no special characters or anything else unusual about them, as I made sure of all that in my testing:

----------------------------------------------------------------

public struct function sendEmail( required struct objEmail ) {

        var requestBody = { "from": objEmail.sender,
                                        "to": objEmail.recipient,
                                        "subject": objEmail.subject,
                                        "messageHTMLText": objEmail.body
                                      };

        var httpSvc = new http( method = "post", 
                                              url = this.baseURL & "mail/message-put" );
        
        httpSvc.addParam( type = "header",
                                        name = "Accept",
                                        value = "application/json" );
                          
        httpSvc.addParam( type = "header",
                                        name = "Content-type",
                                        value = "application/json" );
        
        httpSvc.addParam( type = "header",
                                        name = "Authorization", 
                                        value = "Bearer " & this.accessToken );
         
        httpSvc.addParam( type = "body", 
                                        value = SerializeJSON(requestBody) );
                
        try {
        
            objReply = httpSvc.send().getPrefix();            
            objResponse = DeserializeJSON( objReply.filecontent );

        } catch ( any objExcpt ) {
            
            objResponse = { success = false,
                                       message = objExcpt.message & "<br>" & objExcpt.detail };
        }
        
        return objResponse;
    } 

-----------------------------------------------------------------------------

Again, the only differences between this and my createUser( ) function are the endpoints and the name/value pairs in the requestBody variable. 

I'm at a dead end and if anyone has any suggestions, I would be very grateful. 

Thanks,
Doug

2 Replies

Reply to Thread
0
Andrew Barker Replied
Employee Post
One issue I see with your code is that it is sending the body using the parameter "messageHTMLText". Looking at the documentation, that should just be "messageHTML".

Andrew Barker Software Developer SmarterTools Inc. www.smartertools.com
0
Doug Erickson Replied
Andrew, thanks for looking at this. The incorrect parameter name you spotted was a result of my reading the documentation too quickly; I originally was sending the "messagePlainText" parameter and getting the same error I described in the original post, and I mistakenly changed that parameter to "messageHTMLText", which as you noted should have been "messageHTML", to see if I'd get a different result. 

Unfortunately, having just updated the parameter name to "messageHTML" and re-tested, I'm getting the same 400 Bad Request status code and the same JSON object is being returned with success = false and the "array index out of bounds" message. 

According to the API documentation, the "to" parameter is the only one that is required by the SendMessage function, so I just tried removing the other parameters from the requestBody object in my code to make my API request as simple as possible & eliminate possibilities of what could be causing these requests to fail. Theoretically, I guess, this should cause an email where the subject & body are both empty strings to be sent to the email address in the "to" parameter. Still got the same result described above. 

The result is the same whether I test from within my web application or by creating & sending equivalent requests using Postman. 

Given how simple this code is, and how I've gotten calls to the CreateUser and GetMessages API functions working in my API controller object via methods which differ from my SendEmail( ) method only in their endpoints and the data in their internal requestBody structs, I'm really stumped. The 400 Bad Request status implies that something in my request may be formatted incorrectly, but especially now that I've pared the request body down to one parameter whose value I know to be a valid email address string, I can't imagine what the problem could be. Also, given that the error message says an array index is out of bounds, I checked the API documentation for any parameters whose data type is array; I found three ("excludeFiles", "inlineToRemove", and "attachedMessages")  and tried adding them to the request, explicitly assigning them empty arrays as their values, and the result did not change.

If you see any other possibilities of what might be wrong in my code, or if any other ways I could go about troubleshooting this occur to you, I'm all ears. And thank you again for already having taken some time to look at this.

-Doug


Reply to Thread

Enter the verification text