Search The ForumSearch   RegisterRegister  LoginLogin

AfterLogic WebMail Lite 7

 AfterLogic Forum : AfterLogic WebMail Lite 7
Subject Topic: WebMail Lite API to Send Emails Post ReplyPost New Topic
Author
Message << Prev Topic | Next Topic >>
foxdirect
Newbie
Newbie


Joined: 24 July 2016
Online Status: Offline
Posts: 12
Posted: 24 July 2016 at 7:57am | IP Logged Quote foxdirect

Hi Igor! First off, thanks for a GREAT product!

I am trying to send an email using the API, but I can't seem to make it work. Here is what I have so far:

     session_start();

     // Example of logging into WebMail account using email and
     // password for incorporating into another web application

     // utilizing WebMail Lite API
     include_once("/home/shipped/public_html/mail/libraries/afterlogic/api.php");

     if (class_exists('CApi') && CApi::IsValid())
     {
          // data for logging into account
          $sEmail = 'CORRECT LOGIN EMAIL HERE';
          $sPassword = 'CORRECT PASSWORD HERE';

          try
          {
               // Getting required API class
               $oApiIntegratorManager = CApi::Manager('integrator');

               // attempting to obtain object for account we're trying to log into
               $oAccount = $oApiIntegratorManager->LoginToAccount($sEmail, $sPassword);
               if ($oAccount)
               {
                    // populating session data from the account
                    $oApiIntegratorManager->SetAccountAsLoggedIn($oAccount);

               }
               else
               {
                    // login error
                    echo $oApiIntegratorManager->GetLastErrorMessage();
               }
          }
          catch (Exception $oException)
          {
               // login error
               echo $oException->getMessage();
          }
     }
     else
     {
          echo 'WebMail API isn\'t available';
     }



// THIS AUTO LOGIN SCRIPT WORKS FINE...



// NOW I TRY TO SEND THE EMAIL...

function sendEmailMessage($oAccount, $sSubject, $sText, $sTo, $sCc = '', $sBcc = '', $bReadingConfirmation = false)
{
    if (class_exists('CApi') && \CApi::IsValid()) {
      $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);
       
      return $oApiMailManager->sendMessage($oAccount, $oMessage, null, 'INBOX.Sent');
    }
    return false;
     
}

$oAccount = "1";
$sSubject = "TEST";
$sText = "THIS IS A TEST";
$sTo = "test@example.com";

sendEmailMessage($oAccount, $sSubject, $sText, $sTo, '', '', '');











....... BUT I GET AN ERROR:

Fatal error: Uncaught exception 'MailSo\Base\Exceptions\InvalidArgumentException' with message 'MailSo-Base-Exceptions-InvalidArgumentException (Email.php ~ 46)' in /home/shipped/public_html/mail/libraries/MailSo/Mime/Email.php:46 Stack trace: #0 /home/shipped/public_html/mail/libraries/MailSo/Mime/Email.php(65): MailSo\Mime\Email->__construct(NULL, NULL, '') #1 /home/shipped/public_html/mail/foxy/send-mail-api.php(56): MailSo\Mime\Email::NewInstance(NULL, NULL) #2 /home/shipped/public_html/mail/foxy/send-mail-api.php(103): sendEmailMessage('1', 'TEST', 'THIS IS A TEST', 'test@example.com') #3 {main} thrown in /home/shipped/public_html/mail/libraries/MailSo/Mime/Email.php on line 46
Back to Top View foxdirect's Profile Search for other posts by foxdirect
 
Igor
AfterLogic Support
AfterLogic Support


Joined: 24 June 2008
Location: United States
Online Status: Offline
Posts: 6038
Posted: 25 July 2016 at 3:06am | IP Logged Quote Igor

Actually, we can confirm your sample is fully operational, you just need to remove the line:

Code:
$oAccount = "1";


so that $oAccount value received upon authentication stays intact to be sent as a function parameter.

--
Regards,
Igor, AfterLogic Support
Back to Top View Igor's Profile Search for other posts by Igor
 
foxdirect
Newbie
Newbie


Joined: 24 July 2016
Online Status: Offline
Posts: 12
Posted: 25 July 2016 at 3:14am | IP Logged Quote foxdirect

Wow, that's exciting. I will go try it now.

Thank you for you FAST help!
Back to Top View foxdirect's Profile Search for other posts by foxdirect
 
foxdirect
Newbie
Newbie


Joined: 24 July 2016
Online Status: Offline
Posts: 12
Posted: 25 July 2016 at 6:36am | IP Logged Quote foxdirect

Igor, it works perfect. :)

Is there any way to automatically include the users signature at the end of the message body?

Thank you again for your help.
Back to Top View foxdirect's Profile Search for other posts by foxdirect
 
Igor
AfterLogic Support
AfterLogic Support


Joined: 24 June 2008
Location: United States
Online Status: Offline
Posts: 6038
Posted: 25 July 2016 at 6:38am | IP Logged Quote Igor

You would need to modify body text in your code to achieve that. In WebMail, signature is added when the message is built within message editor window.

--
Regards,
Igor, AfterLogic Support
Back to Top View Igor's Profile Search for other posts by Igor
 
Igor
AfterLogic Support
AfterLogic Support


Joined: 24 June 2008
Location: United States
Online Status: Offline
Posts: 6038
Posted: 25 July 2016 at 6:49am | IP Logged Quote Igor

We've published a slightly modified version of your code in our documentation: Sending mail

Thank you!

--
Regards,
Igor, AfterLogic Support
Back to Top View Igor's Profile Search for other posts by Igor
 

If you wish to post a reply to this topic you must first login
If you are not already registered you must first register

  Post ReplyPost New Topic
Printable version Printable version

Forum Jump

Powered by Web Wiz Forums version 7.9
Copyright ©2001-2004 Web Wiz Guide