Search The ForumSearch   RegisterRegister  LoginLogin

AfterLogic WebMail Lite 7

 AfterLogic Forum : AfterLogic WebMail Lite 7
Subject Topic: Send email with attachment through API Post ReplyPost New Topic
Author
Message << Prev Topic | Next Topic >>
rsantellan
Newbie
Newbie


Joined: 14 October 2015
Online Status: Offline
Posts: 11
Posted: 16 November 2015 at 4:56am | IP Logged Quote rsantellan

Hi,

I'm trying to send emails through the PHP Api. So far I was able to send messages with reading confirmation. But when trying to add the attachment it fails.

So far my function is:

public function sendEmailMessage($oAccount, $sSubject, $sText, $sTo, $sCc = '', $sBcc = '', $attachments = array(), $bReadingConfirmation = false)
{
    if (class_exists('CApi') && \CApi::IsValid()) {
      $oApiMailManager = \CApi::Manager('mail');
      $oApiFileStorage = \CApi::Manager('filestorage');
      $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);
      foreach($attachments as $attachment)
      {
        $info = new \SplFileInfo($attachment);
        $rResource = fopen($attachment, 'r');// $oApiFileStorage->getFile($oAccount, \EFileStorageType::Shared, $attachment, $info->getFilename());
        if (is_resource($rResource))
        {
          $oMessage->Attachments()->Add(
            \MailSo\Mime\Attachment::NewInstance($rResource, $info->getFilename(), $info->getSize())
          );
        }
        fclose($rResource);
      }
      
      return $oApiMailManager->sendMessage($oAccount, $oMessage, null, 'INBOX.Sent');
    }
    return false;
    
}


If I comment attachment foreach it works like a charm, but I cannot create the resource to send the attachment. Is there any example of how to achieve this?

Regards
Back to Top View rsantellan's Profile Search for other posts by rsantellan
 
Alex
AfterLogic Support
AfterLogic Support
Avatar

Joined: 19 November 2003
Online Status: Offline
Posts: 2206
Posted: 17 November 2015 at 3:43am | IP Logged Quote Alex

Hi,

You should remove 'fclose($rResource)' because you never let subsequent $oApiMailManager->sendMessage a chance to read from the resource.

Regards,
Alex

Back to Top View Alex's Profile Search for other posts by Alex
 

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