1
Sending an email via the SmarterMail API
Question asked by Doug Erickson - Yesterday 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

Reply to Thread

Enter the verification text