Sending Webmail with wsCurl

In the following example, we will demonstrate how to use the wsCurl functionality in WS to send Web mail.
  1. Open the script file you will use to build the wsCurl function. You can create a new script file to call from the original script file if necessary.
  2. Load the wsCurl files as shown in the following example:
    load('wscurl.dll');
  3. Create a function. In our example, we will name the function 'translate'. All the code will be contained in this function. An example is shown below:
    function sendmail()
    { }
  4. Set the applicable variables and identifiers as shown in the following list.
    Initialize the wscurl object
    var wsCurl = new Curl();
    Set Mailserver URL

    In this step, we will set the URL for the mail server. Note that in our example, we are using port 587 as opposed to the normal SMTP port 25. We are using 587 because we are using secure mail submission as defined in RFC4403. However, you should set your port to match the port on your mail server. The code is as follows:

    wsCurl.setopt(Curl.CURLOPT_URL, "smtp://smtp.gmail.com:587");
    Upgrade Connection to TLS

    In this step, we will upgrade the connection from plain text to Transport Layer Security (TLS). To do this, we will use the STARTTLS command.

    Note: We recommend using Curl.USESSL_ALL as shown below, not Curl.USESSL_TRY. This is because if the TLS upgrade fails, the transfer will proceed.
    wsCurl.setopt(Curl.CURLOPT_USE_SSL, Curl.USESSL_ALL);
    Disable Peer and Host Verification

    There may be times when your server does not have a valid certificate. IN these cases, you can disable part of the TLS protection by using the following code to set the CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST options to 0.

    wsCurl.setopt(Curl.CURLOPT_SSL_VERIFYPEER, 0);
    wsCurl.setopt(Curl.CURLOPT_SSL_VERIFYPEER, 0);
    Note: We do not recommend disabling any part of your security, but having partial security is better than sending detauils in plain text. However, we recommend adding the issuer certificate (or the host certificate if the server certificate is self-signed), to the libcurl using the CURLOPT_CAINFO option.

    To add the certificate to libcurl, use the following code:

    wsCurl.setopt(Curl.CURLOPT_CAINFO, "/path/to/certificate.pem");
    Set Password and Username

    The most common reason to have transport security is to protect your users'login credentials from theft. Use the following code to set user names and passwords.

    wsCurl.setopt(CURLOPT_USERNAME, "user@example.com");
     wsCurl.setopt(CURLOPT_PASSWORD, "Password");
    Set Envelope Reverse Path

    Use the following code to set the envelope reverse path for the email. Please note that you must use brackets around the email address as shown in the example below. .

    var translateLangIdentifier = "&target=";
    Set

    Set the target language code itself. This will be a two-digit code and in our example, we will use English. A complete list of available language codes can be found in the Language Code Reference.

    var translateLang = "en";
    Build the URL

    The following code will create the complete URL that we will use to make the call for a translation request. This is demonstrated as follows:

    var completeURL = baseURL + textToConvert + sourceLangIdentifier + sourceLang + translateLangIdentifier + translateLang;
    Make a Translation Request

    Now that you have the complete URL, you can make a request for translation as shown in the below example:

    Note: This example is for requests that return a JSON object in a string.
    wsCurl.setopt(Curl.CURLOPT_URL, completeURL);
    Call the Execute function

    The final step in the process is to call the execute function. This will dispatch a response to your request. An example is shown below:

    Note: You can check the return value for errors with the error codes that are explained in the Error Codes section. A return value of 0 means the operation succeeded.
    var response = wsCurl.exec();
    Response

    In this example, the response will be in JSON, although you can set the response to be in HTML as well. A sample response based on the example iss shown below:

                      {
      "data": {
        "translations": [
          {
            "translatedText": "Bonjour tout le monde"
            }
          ]
        }
      }
                    
    Close wsCurl

    To close the HTTP connection for wsCurl, use the following code:

    wsCurl.close();
    Remove References

    Your last step will be to remove any references for the garbage collection as shown below:

    wsCurl=NULL;
  5. Save your changes and call your new function from your script file.
  6. The translated text should be returned to the calling function.