WebMail Pro 7 documentation

Sending mail

The following sample demonstrates the use of PHP API for sending mail message out.

The below function accepts a number of parameters, including sender's account email and password, recipient email address, optional CC/BCC lists and read confirmation request flag.

<?php

include_once("/var/www/webmail/libraries/afterlogic/api.php"); 

function sendEmailMessage($sEmail, $sPassword, $sSubject, $sText, $sTo, $sCc = '', $sBcc = '', $bReadingConfirmation = false) 
{
  if (class_exists('CApi') && CApi::IsValid()) 
  { 
    try 
    { 
      $oApiIntegratorManager = CApi::Manager('integrator'); 
      $oAccount = $oApiIntegratorManager->LoginToAccount($sEmail, $sPassword); 
      if ($oAccount) 
      { 
        $oApiMailManager = \CApi::Manager('mail'); 
        $oMessage = \MailSo\Mime\Message::NewInstance(); 
        $oMessage->RegenerateMessageId(); 
        $oFrom = \MailSo\Mime\Email::NewInstance($oAccount->Email, $oAccount->FriendlyName); 
        $oMessage 
          ->SetFrom($oFrom) 
          ->SetSubject($sSubject); 

        $oToEmails = \MailSo\Mime\EmailCollection::NewInstance($sTo); 
        if ($oToEmails && $oToEmails->Count()) 
        { 
          $oMessage->SetTo($oToEmails); 
        } 
        $oCcEmails = \MailSo\Mime\EmailCollection::NewInstance($sCc); 
        if ($oCcEmails && $oCcEmails->Count()) 
        { 
          $oMessage->SetCc($oCcEmails); 
        } 

        $oBccEmails = \MailSo\Mime\EmailCollection::NewInstance($sBcc); 
        if ($oBccEmails && $oBccEmails->Count()) 
        { 
          $oMessage->SetBcc($oBccEmails); 
        } 
        
        if ($bReadingConfirmation) 
        { 
          $oMessage->SetReadConfirmation($oAccount->Email); 
        } 
        $sTextConverted = \MailSo\Base\HtmlUtils::ConvertHtmlToPlain($sText); 
        $oMessage->AddText($sTextConverted, false); 
          
        $mFoundDataURL = array(); 
        $aFoundedContentLocationUrls = array(); 
        $aFoundCids = array(); 
          
        $htmlTextConverted = \MailSo\Base\HtmlUtils::BuildHtml($sText, $aFoundCids, $mFoundDataURL, $aFoundedContentLocationUrls); 
        $oMessage->AddText($htmlTextConverted, true); 
          
        $sSentFolder = "Sent Items";
        $oFolders = $oApiMailManager->getFolders($oAccount);
        $aFolders = array();
        $oFolders->foreachWithSubFolders(function ($oFolder) use (&$aFolders,&$sSentFolder) {
          if ($oFolder->getType() === EFolderType::Sent) {
            $sSentFolder = $oFolder->getFullName();
          }
        }
        );
      	return $oApiMailManager->sendMessage($oAccount, $oMessage, null, $sSentFolder); 
      }
      else 
      {
        echo $oApiIntegratorManager->GetLastErrorMessage(); 
      } 
    } 
    catch (Exception $oException) 
    { 
      echo $oException->getMessage(); 
    } 
  } 
  else 
  { 
    echo 'API is not available'; 
  } 
}

The function is called as follows:

$sEmail = "user@domain.com";
$sPassword = "MyPassword123";
$sRecipient = "sales@otherdomain.com";

$sSubject = "Test Message"; 
$sText = "This is a Test Message"; 

sendEmailMessage($sEmail, $sPassword, $sSubject, $sText, $sRecipient, '', '', '');