| Author |  | 
      
        | rsantellan Newbie
 
  
 
 Joined: 14 October 2015
 Online Status: Offline
 Posts: 11
 | 
          Hi,
           | Posted: 16 November 2015 at 4:56am | IP Logged |   |  
           | 
 |  
 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 |     | 
       
       
        |  | 
        | Alex AfterLogic Support
 
  
  
 Joined: 19 November 2003
 Online Status: Offline
 Posts: 2207
 | 
          Hi,
           | Posted: 17 November 2015 at 3:43am | IP Logged |   |  
           | 
 |  
 You should remove 'fclose($rResource)' because you never let subsequent $oApiMailManager->sendMessage a chance to read from the resource.
 
 Regards,
 Alex
 
 
 | 
       
        | Back to Top |     | 
       
       
        |  |