<?php
$arrPost = \Components\AF\Courses\Posts::get(40);
$arrContacts = \Components\AF\Courses::getClassList($arrPost['course_id']);
sendNewPostEmail($arrPost, $arrContacts);
function sendNewPostEmail($arrPost, $arrContacts = [])
{
if (!$arrContacts)
return;
$iCourseId = $arrPost['course_id'];
if (!$arrCourse = \Components\AF\Courses::get($iCourseId))
return;
// Set course title for either collective or private course
if ($arrCourse['course_type'] === 'collective')
{
$arrProduct = \Components\Commerce\Products::getIncludingDeleted($arrCourse['product_id']);
$strCourseTitle = $arrProduct['product_title'] ?? null;
} else
$strCourseTitle = $arrCourse['course_code'];
$strSubject = 'There\'s a new post for your ' . $strCourseTitle . ' class';
$strTemplatePageId = '/admin/af/courses/posts/email-templates/post-new/';
$iPostId = $arrPost['post_id'];
// Attachments
$arrAttachmentsData = [];
if ($arrAttachments = \Components\AF\Courses\Posts::attachmentsGet($iPostId))
{
$arrAttachmentsData['shared_folder_name'] = 'courses_posts_' . $iPostId; // Correspondence shared attachments folder
foreach ($arrAttachments as $strAttachment)
$arrAttachmentsData['attachments'][] = \Components\AF\Courses\Posts::getPrivateMediaDir() . $iPostId . '/' . $strAttachment;
}
// Build vars
$arrVariables = [
'course' => $arrCourse,
'post' => $arrPost,
'post_contact' => \Components\Customers\Contacts::get($arrPost['contact_id']),
'post_contact_title' => \Components\AF\Courses\Posts::renderTitleForContact($arrPost['contact_id'], $arrCourse['course_id']),
'course_title' => $strCourseTitle,
// 'media_attachments' => $arrMediaAttachments
];
// Send email notification
foreach ($arrContacts as $arrContact)
{
sendMail($arrContact, $strSubject, $strTemplatePageId, $arrVariables, $arrAttachmentsData);
}
}
function sendMail($arrContact, $strSubject, $strTemplatePageId, $arrVariables, $arrAttachmentsData = [])
{
$pMessage = new \Framework\Mail\Message();
$pMessage->setSubject($strSubject);
$strFromEmail = \Components\AF\Courses::configGetValue('student_portal_from_email') ?: \Components\Settings\ConfigurationItems::getValue('\Components\Settings\MyAccount', 'account_contact_email');
$strFromName = \Components\AF\Courses::configGetValue('student_portal_from_name') ?: \Components\Settings\ConfigurationItems::getValue('\Components\Settings\MyAccount', 'account_title');
// Set addresses
$pMessage->setFrom($strFromEmail);
$pMessage->setReplyTo($strFromEmail);
$pMessage->setFromName($strFromName);
$pMessage->setReplyToName($strFromName);
$pMessage->setTo($arrContact['contact_email']);
\Components\Customers\Contacts::impersonateBegin($arrContact['contact_id']);
foreach($arrVariables as $strKey => &$strValue)
\Framework\DOM\CurrentContext::setVariable($strKey, $strValue);
$arrPage = \Components\Website\Pages::get($strTemplatePageId);
$arrPage['design_id'] = \Components\AF\Courses::configGetValue('student_portal_email_design_id');
$pMessage->setContentHTMLBodyFromPage($arrPage);
$strContent = $pMessage->generateBodyHTML();
// Add attachments to email
if (isset($arrAttachmentsData['attachments']) && $arrAttachmentsData['attachments'])
{
foreach ($arrAttachmentsData['attachments'] as $strAttachment)
$pMessage->addAttachmentFromFile($strAttachment);
}
$pMessage->send();
\Components\Customers\Contacts::impersonateEnd();
// Record Correspondence
$arrRecord = [];
$arrRecord['contact_correspondence_is_outbound'] = 1;
$arrRecord['contact_correspondence_type'] = 'email';
$arrRecord['contact_correspondence_subject'] = $pMessage->getSubject();
$arrRecord['contact_correspondence_content'] = $strContent;
$arrRecord['contact_correspondence_source'] = 'form';
$arrRecord['contact_id'] = $arrContact['contact_id'];
$arrRecord['contact_correspondence_shared_attachments_folder'] = null;
$bHasAttachments = false;
if ($arrAttachmentsData && isset($arrAttachmentsData['attachments']) && count($arrAttachmentsData['attachments']))
$bHasAttachments = true;
if ($bHasAttachments && isset($arrAttachmentsData['shared_folder_name']))
$arrRecord['contact_correspondence_shared_attachments_folder'] = $arrAttachmentsData['shared_folder_name'];
$iCorrespondenceId = \Components\Customers\Contacts\Correspondence::save($arrRecord);
// Copy entity attachments to a correspondence folder
$strAttachmentsDir = '';
if ($arrRecord['contact_correspondence_shared_attachments_folder'])
{
$strAttachmentsDir = '../Private/media/customers_contacts_correspondence/' . $arrAttachmentsData['shared_folder_name'] . '/';
} else
{
if ($bHasAttachments)
$strAttachmentsDir = '../Private/media/customers_contacts_correspondence/' . $iCorrespondenceId . '/';
}
if ($strAttachmentsDir && !file_exists($strAttachmentsDir))
{
\Framework\System\FileSystem::mkdir($strAttachmentsDir);
foreach ($arrAttachmentsData['attachments'] as $strAttachment)
\Framework\System\FileSystem::copy($strAttachment, $strAttachmentsDir . basename($strAttachment));
}
}
?>
<?php
$arrPost = \Components\AF\Courses\Posts::get(40);
$arrContacts = \Components\AF\Courses::getClassList($arrPost['course_id']);
sendNewPostEmail($arrPost, $arrContacts);
function sendNewPostEmail($arrPost, $arrContacts = [])
{
if (!$arrContacts)
return;
$iCourseId = $arrPost['course_id'];
if (!$arrCourse = \Components\AF\Courses::get($iCourseId))
return;
// Set course title for either collective or private course
if ($arrCourse['course_type'] === 'collective')
{
$arrProduct = \Components\Commerce\Products::getIncludingDeleted($arrCourse['product_id']);
$strCourseTitle = $arrProduct['product_title'] ?? null;
} else
$strCourseTitle = $arrCourse['course_code'];
$strSubject = 'There\'s a new post for your ' . $strCourseTitle . ' class';
$strTemplatePageId = '/admin/af/courses/posts/email-templates/post-new/';
$iPostId = $arrPost['post_id'];
// Attachments
$arrAttachmentsData = [];
if ($arrAttachments = \Components\AF\Courses\Posts::attachmentsGet($iPostId))
{
$arrAttachmentsData['shared_folder_name'] = 'courses_posts_' . $iPostId; // Correspondence shared attachments folder
foreach ($arrAttachments as $strAttachment)
$arrAttachmentsData['attachments'][] = \Components\AF\Courses\Posts::getPrivateMediaDir() . $iPostId . '/' . $strAttachment;
}
// Build vars
$arrVariables = [
'course' => $arrCourse,
'post' => $arrPost,
'post_contact' => \Components\Customers\Contacts::get($arrPost['contact_id']),
'post_contact_title' => \Components\AF\Courses\Posts::renderTitleForContact($arrPost['contact_id'], $arrCourse['course_id']),
'course_title' => $strCourseTitle,
// 'media_attachments' => $arrMediaAttachments
];
// Send email notification
foreach ($arrContacts as $arrContact)
{
sendMail($arrContact, $strSubject, $strTemplatePageId, $arrVariables, $arrAttachmentsData);
}
}
function sendMail($arrContact, $strSubject, $strTemplatePageId, $arrVariables, $arrAttachmentsData = [])
{
$pMessage = new \Framework\Mail\Message();
$pMessage->setSubject($strSubject);
$strFromEmail = \Components\AF\Courses::configGetValue('student_portal_from_email') ?: \Components\Settings\ConfigurationItems::getValue('\Components\Settings\MyAccount', 'account_contact_email');
$strFromName = \Components\AF\Courses::configGetValue('student_portal_from_name') ?: \Components\Settings\ConfigurationItems::getValue('\Components\Settings\MyAccount', 'account_title');
// Set addresses
$pMessage->setFrom($strFromEmail);
$pMessage->setReplyTo($strFromEmail);
$pMessage->setFromName($strFromName);
$pMessage->setReplyToName($strFromName);
$pMessage->setTo($arrContact['contact_email']);
\Components\Customers\Contacts::impersonateBegin($arrContact['contact_id']);
foreach($arrVariables as $strKey => &$strValue)
\Framework\DOM\CurrentContext::setVariable($strKey, $strValue);
$arrPage = \Components\Website\Pages::get($strTemplatePageId);
$arrPage['design_id'] = \Components\AF\Courses::configGetValue('student_portal_email_design_id');
$pMessage->setContentHTMLBodyFromPage($arrPage);
$strContent = $pMessage->generateBodyHTML();
// Add attachments to email
if (isset($arrAttachmentsData['attachments']) && $arrAttachmentsData['attachments'])
{
foreach ($arrAttachmentsData['attachments'] as $strAttachment)
$pMessage->addAttachmentFromFile($strAttachment);
}
$pMessage->send();
\Components\Customers\Contacts::impersonateEnd();
// Record Correspondence
$arrRecord = [];
$arrRecord['contact_correspondence_is_outbound'] = 1;
$arrRecord['contact_correspondence_type'] = 'email';
$arrRecord['contact_correspondence_subject'] = $pMessage->getSubject();
$arrRecord['contact_correspondence_content'] = $strContent;
$arrRecord['contact_correspondence_source'] = 'form';
$arrRecord['contact_id'] = $arrContact['contact_id'];
$arrRecord['contact_correspondence_shared_attachments_folder'] = null;
$bHasAttachments = false;
if ($arrAttachmentsData && isset($arrAttachmentsData['attachments']) && count($arrAttachmentsData['attachments']))
$bHasAttachments = true;
if ($bHasAttachments && isset($arrAttachmentsData['shared_folder_name']))
$arrRecord['contact_correspondence_shared_attachments_folder'] = $arrAttachmentsData['shared_folder_name'];
$iCorrespondenceId = \Components\Customers\Contacts\Correspondence::save($arrRecord);
// Copy entity attachments to a correspondence folder
$strAttachmentsDir = '';
if ($arrRecord['contact_correspondence_shared_attachments_folder'])
{
$strAttachmentsDir = '../Private/media/customers_contacts_correspondence/' . $arrAttachmentsData['shared_folder_name'] . '/';
} else
{
if ($bHasAttachments)
$strAttachmentsDir = '../Private/media/customers_contacts_correspondence/' . $iCorrespondenceId . '/';
}
if ($strAttachmentsDir && !file_exists($strAttachmentsDir))
{
\Framework\System\FileSystem::mkdir($strAttachmentsDir);
foreach ($arrAttachmentsData['attachments'] as $strAttachment)
\Framework\System\FileSystem::copy($strAttachment, $strAttachmentsDir . basename($strAttachment));
}
}
?>