KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: A comprehensive Send Email as HTML method
PRODUCT: 4D Internet Commands | VERSION: 14.3 | PLATFORM: Mac & Win
Published On: January 12, 2015

This Tech Tip includes a 4D Project Method SEND_MAIL_HTML. It makes use of the 4D Language Objects to provide flexibility in send the necessary settings and contents for an email to be sent in HTML format. It also accounts for sending the email through an secure SMTP server, such as Exchange server and Apple mail, that requires that the email be sent using TLS.

The code snippet below demonstrates how to send an email using the method.

OB SET($Settings_O;"host";$smtpServer_T;"from";$From_T;"to";$To_T;"subject";$Subject_T)
OB SET($Settings_O;"authUser"; $AuthUserName_T;"authPass";$AuthPassword_T;"protocol";$Protocol_L;"port";$Port_L)
OB SET($Settings_O;"bodyHTML";$EmailHTML_T;"bodyText";$EmailTEXT_T)

$Errors_O:=SEND_MAIL_HTML ($Settings_O)
If (OB Is defined($Errors_O))
    // FAILURE
End if



Project method SEND_MAIL_HTML


If (True)
   If (False)
      Begin SQL
      /*
       Name: SEND_MAIL_HTML
       Path: SEND_MAIL_HTML

       Purpose: Send HTML email using 4D IC Plugin

       $0 - C_OBJECT - Success if undefined, otherwise Error Message
       $1 - C_OBJECT - contains email settings and content
      */
      End SQL
   End if
   C_TEXT($MethodName_T)
   $MethodName_T:=Current method name
     //=====================    Declare Variables    ==================================
     //method_parameters_declarations
   C_OBJECT($0;$Result_O)
   C_OBJECT($Settings_O;$1)
     //----------------------------------------------------------------------------
     //method_wide_constants_declarations
     //----------------------------------------------------------------------------
     //local_variable_declarations
   C_LONGINT($Ndx;$SOA;$RIS;$Params_L;$SMTP_ID_L;$Error_L;$Protocol_L;$Port_L)
   C_TEXT($Host_T;$From_T;$To_T;$ReplyTo_T;$AuthUserName_T;$AuthPassword_T)
   C_TEXT($Subject_T;$To_T;$BodyText_T;$Body_HTML_T;$cidKeyBase_T;$ImagesFolder_T)
   C_TEXT($EHTML_T;$WAHTML_T)
   ARRAY LONGINT($Paths_aT;0)
End if
  //======================    Initialize and Setup    ================================

$Params_L:=Count parameters

$Settings_O:=$1
$Host_T:=OB Get($Settings_O;"host")
$From_T:=OB Get($Settings_O;"from")
$To_T:=OB Get($Settings_O;"to")
$Subject_T:=OB Get($Settings_O;"subject")

If (OB Is defined($Settings_O;"replyTo"))
   $ReplyTo_T:=OB Get($Settings_O;"replyTo")
End if
If (OB Is defined($Settings_O;"bodyText"))
   $BodyText_T:=OB Get($Settings_O;"bodyText")
End if
If (OB Is defined($Settings_O;"bodyHTML"))
   $Body_HTML_T:=OB Get($Settings_O;"bodyHTML")
End if
If (OB Is defined($Settings_O;"imagesFolder"))
   $ImagesFolder_T:=OB Get($Settings_O;"imagesFolder")
   $EHTML_T:=OB Get($Settings_O;"ehtml")
   $WAHTML_T:=""
   $cidKeyBase_T:="Image" //The cid key base used in all image tag sources

    // See Tech Tip "Sending HTML Emails" for
    // documentation and code to this project method
    //
   ProcessImagePaths(->$EHTML_T;$ImagesFolder_T;$cidKeyBase_T;->$Paths_aT;->$WAHTML_T)
End if
If (OB Is defined($Settings_O;"authUser"))
   $AuthUserName_T:=OB Get($Settings_O;"authUser")
End if
If (OB Is defined($Settings_O;"authPass"))
   $AuthPassword_T:=OB Get($Settings_O;"authPass")
End if
If (OB Is defined($Settings_O;"protocol"))
   $Protocol_L:=OB Get($Settings_O;"protocol")
End if
If (OB Is defined($Settings_O;"port"))
   $Port_L:=OB Get($Settings_O;"port")
Else
   $Port_L:=0
End if

//========================    Method Actions    ==================================

$Error_L:=SMTP_New ($SMTP_ID_L)
If ($Error_L=0)
   $Error_L:=SMTP_Host ($SMTP_ID_L;$Host_T)
   If ($Error_L=0)
      $Error_L:=SMTP_From ($SMTP_ID_L;$From_T)
      If ($Error_L=0)
         If ($ReplyTo_T#"")
            $Error_L:=SMTP_ReplyTo ($SMTP_ID_L;$ReplyTo_T)
       End if
         If ($Error_L=0)
            If (($AuthUserName_T#"") & ($AuthPassword_T#""))
               $Error_L:=SMTP_Auth ($SMTP_ID_L;$AuthUserName_T;$AuthPassword_T)
            End if
            If ($Error_L=0)
               $Error_L:=SMTP_Subject ($SMTP_ID_L;$Subject_T)
               If ($Error_L=0)
                  $Error_L:=SMTP_To ($SMTP_ID_L;$To_T;1)
                  If (($Error_L=0) & ($BodyText_T#""))
                     $Error_L:=SMTP_Body ($SMTP_ID_L;$BodyText_T;0) //TEXT email body (0 = replace)
                  End if
                  If (($Error_L=0) & ($Body_HTML_T#""))
                     $Error_L:=SMTP_Body ($SMTP_ID_L;$Body_HTML_T;4) //HTML email body (4 = HTML)
                  End if
                  If (($Error_L=0) & ($ImagesFolder_T#""))
                     For ($Ndx;1;Size of array($Paths_aT))
                          // Now we just attach each document using base64 encoding (the 2)
                          // Each cid: reference used the element number of the $Paths_aT array
                          //
                        $Error_L:=SMTP_Attachment ($SMTP_ID_L;$Paths_aT{$Ndx};2;0;$cidKeyBase+String($Ndx))
                        If ($Error_L#0)
                           $Ndx:=Size of array($Paths_aT)
                        End if
                     End for
                  End if
                  If ($Error_L=0)
                     If ($Port_L#0)
                       // Required for STARTTLS is should be IT_SetPort (0;587)
                       // See Tech Tip "Send SMTP using Office 365"
                       // Though this Tech Tip is aimed at Office 365 it is also applies to most mail servers that
                       // use secure smtp such as Apple mail since SS1, SS2 and SS3 are no longer considered safe
                       //
                     $Error_L:=IT_SetPort ($Protocol_L;$Port_L)
                     End if
                     If ($Error_L=0)
                        $Error_L:=SMTP_Send ($SMTP_ID_L)
                        If ($Error_L=0)
                           $Error_L:=SMTP_Clear ($SMTP_ID_L)
                           If ($Error_L#0)
                              OB SET($Result_O;"errorClear";$Error_L)
                              $Error_L:=0
                           End if
                        End if
                     End if
                  End if
               End if
            End if
         End if
      End if
   End if
End if

If ($Error_L#0)
   OB SET($Result_O;"errorExecution";$Error_L)
   $Error_L:=SMTP_Clear ($SMTP_ID_L)
   If ($Error_L#0)
      OB SET($Result_O;"errorClear";$Error_L)
      $Error_L:=0
   End if
End if

  //========================    Clean up and Exit    =================================

$0:=$Result_O