pquery('SELECT from_email_field FROM vtiger_systems WHERE from_email_field != ? AND server_type = ?', array('', 'email'));
return $adb->query_result($result, 0, 'from_email_field');
}
/**
* @param int $userId
* @param string $type
* @return array
* @throws Exception
*/
public static function getUserDataByType($userId, $type = 'email1')
{
$adb = PearDatabase::getInstance();
$userResult = $adb->pquery(sprintf('SELECT first_name, last_name, %s AS email FROM vtiger_users WHERE id=?', $type), array($userId));
return $adb->query_result_rowdata($userResult);
}
/**
* @param string $sourceModule
* @return string
*/
public static function getSelectTemplateUrl($sourceRecord, $sourceModule)
{
if (vtlib_isModuleActive('EMAILMaker')) {
return 'module=EMAILMaker&view=Popup&src_record=' . $sourceRecord . '&src_module=' . $sourceModule;
}
return 'module=EmailTemplates&view=Popup&src_record=' . $sourceRecord . '&src_module=' . $sourceModule;
}
/**
* @param int $userId
* @return string
* @throws Exception
*/
public static function getSignature($userId)
{
$adb = PearDatabase::getInstance();
$result = $adb->pquery('SELECT signature FROM vtiger_users WHERE id=?', array($userId));
return nl2br($adb->query_result($result, 0, 'signature'));
}
/**
* @throws \ITS4You\PHPMailer\Exception
*/
public function sendEmail()
{
global $ITS4YouEmails_SendingClass;
$mailer = $this->getMailer();
if (!empty($ITS4YouEmails_SendingClass) && class_exists($ITS4YouEmails_SendingClass)) {
$success = $ITS4YouEmails_SendingClass::send($mailer);
} else {
$success = $mailer->send();
}
return $success;
}
public function send()
{
$success = false;
$mailer = $this->getMailer();
try {
if (!$this->isEmpty('smtp')) {
$mailer->retrieveSMTPById($this->get('smtp'));
} else {
$mailer->retrieveSMTPVtiger();
}
$mailer->isHTML(true);
$this->retrieveContent();
$this->retrieveReplyTo();
$this->retrieveFromEmail();
$this->retrieveToEmails();
$this->retrieveCCEmails();
$this->retrieveBCCEmails();
$this->retrieveAttachments();
$this->retrieveLogo();
$this->retrieveImages();
$this->retrieveInReplyTo();
$this->convertImagesToEmbed();
$hasBannedWords = $this->hasBannedWords();
if (!$hasBannedWords) {
$success = $this->sendEmail();
$this->set('subject', $mailer->Subject);
$this->setBody($mailer->Body);
}
if ($hasBannedWords) {
$result = $mailer->ErrorInfo;
$flag = self::$FLAG_BANNED_WORD;
} elseif ($success) {
$result = vtranslate('LBL_SUCCESS_EMAIL', $this->getModuleName());
$flag = self::$FLAG_SENT;
$mailer->saveMessageId();
$this->saveEmailToSentFolder();
} else {
$result = vtranslate('LBL_ERROR_EMAIL', $this->getModuleName()) . $mailer->ErrorInfo;
$flag = self::$FLAG_ERROR;
}
} catch (Exception $e) {
$result = vtranslate('LBL_ERROR_EMAIL', $this->getModuleName()) . $mailer->ErrorInfo . ' | Exception: ' . $e->getMessage();
$flag = self::$FLAG_ERROR;
}
$this->set('result', $result);
$this->set('email_flag', $flag);
$this->set('mode', 'edit');
$this->save();
return $success;
}
/**
* Retrieve banned words, return boolean value and set ErrorInfo to mailer
* @return bool
*/
public function hasBannedWords()
{
$this->retrieveBannedWords();
$subject = strtolower($this->getSubject());
$body = strtolower($this->getBody());
foreach ($this->getBannedWords() as $bannedWord) {
$bannedWordLower = strtolower($bannedWord);
if (-1 < stripos($subject, $bannedWordLower) || -1 < stripos($body, $bannedWordLower)) {
$mailer = $this->getMailer();
$mailer->ErrorInfo = $bannedWord;
return true;
}
}
return false;
}
/**
* @return ITS4YouEmails_Mailer_Model|\ITS4You\PHPMailer\PHPMailer
*/
public function getMailer()
{
if (empty($this->mailer)) {
$this->retrieveMailer();
}
return $this->mailer;
}
public function setMailer($value)
{
$this->mailer = $value;
}
public function retrieveMailer()
{
$this->setMailer(ITS4YouEmails_Mailer_Model::getCleanInstance());
}
/**
* @return void
* @throws Exception
*/
public function retrieveContent()
{
$mailer = $this->getMailer();
$subject = $this->getSubject();
$body = $this->getBody();
if (vtlib_isModuleActive('EMAILMaker')) {
$record = $this->get('related_to');
$module = getSalesEntityType($record);
$toEmailIds = $this->getJsonArray('to_email_ids');
foreach ($toEmailIds as $toEmailId) {
list($recipientId, $recipientEmail, $recipientModule) = explode('|', $toEmailId);
$EMAILContentModel = EMAILMaker_EMAILContent_Model::getInstance($module, $record, $this->get('email_template_language'), $recipientId, $recipientModule);
$EMAILContentModel->setSubject($subject);
$EMAILContentModel->setBody($body);
if (!$this->isEmpty('email_template_data')) {
foreach ((array)$this->get('email_template_data') as $email_key => $email_value) {
$EMAILContentModel->set($email_key, $email_value);
}
}
$EMAILContentModel->getContent(true, !empty($module));
$subject = $EMAILContentModel->getSubject();
$body = $EMAILContentModel->getBody();
$images = $EMAILContentModel->getEmailImages();
if (count($images)) {
foreach ($images as $imageId => $imageData) {
$this->setImage($imageId, $imageData['path'], $imageData['name']);
}
}
}
}
$mailer->Subject = $this->getProcessedSubject($subject);
$mailer->Body = $this->getProcessedBody($body);
$mailer->AltBody = $this->getProcessedAltBody($body);
}
public function getSubject()
{
return strip_tags(decode_html($this->get('subject')));
}
public function getBody()
{
return decode_html($this->get('body'));
}
public function getJsonArray($name)
{
$value = $this->get($name);
if (!is_array($value) && !$this->isEmpty($name)) {
$value = json_decode(htmlspecialchars_decode($value));
}
return (array)$value;
}
public function setBody($content)
{
if (!$this->isEmpty('images')) {
$images = (array)$this->get('images');
foreach ($images as $id => $data) {
$content = str_replace('cid:' . $id, $data['path'], $content);
}
}
$this->set('body', $content);
}
public function setImage($id, $path, $name = null)
{
$images = (array)$this->get('images');
$images[$id] = [
'cid' => $id,
'path' => $path,
'name' => !empty($name) ? $name : basename($path),
];
$this->set('images', $images);
}
public function getProcessedSubject($content)
{
$content = decode_html($content);
return $this->convertVariables($content);
}
public function convertVariables($content)
{
$relatedTo = $this->get('related_to');
$relatedToModule = getSalesEntityType($relatedTo);
$content = getMergedDescription($content, Users_Record_Model::getCurrentUserModel()->getId(), 'Users');
$toEmailIds = $this->getJsonArray('to_email_ids');
foreach ($toEmailIds as $toEmailId) {
list($record, $email, $module) = explode('|', $toEmailId);
if (!empty($module) && !empty($record) && isRecordExists($record)) {
$content = getMergedDescription($content, $record, $module);
}
}
if (!empty($relatedToModule) && !empty($relatedTo) && isRecordExists($relatedTo)) {
$content = getMergedDescription($content, $relatedTo, $relatedToModule);
}
return $content;
}
public function getProcessedBody($content)
{
$content = decode_html($content);
$content = $this->convertVariables($content);
$content = ITS4YouEmails_Utils_Helper::purifyHtmlEventAttributes($content);
//$content = decode_emptyspace_html($content);
$content = preg_replace('##is', '', $content);
if (strpos($content, '$logo$')) {
$content = str_replace('$logo$', '
', $content);
$this->logo = true;
}
$content = $this->convertUrlsToTrackUrls($content);
$content = $this->convertCssToInline($content);
$content .= $this->getTrackImageDetails();
return $content;
}
public function convertUrlsToTrackUrls($content, $type = 'html')
{
if ($this->isEmailTrackEnabled()) {
$extractedUrls = Vtiger_Functions::getUrlsFromHtml($content);
foreach ($extractedUrls as $sourceUrl => $value) {
$trackingUrl = $this->getTrackUrlForClicks($sourceUrl);
$content = $this->replaceLinkWithShortUrl($content, $trackingUrl, $sourceUrl, $type);
}
}
return $content;
}
/**
* @return bool
*/
public function isEmailTrackEnabled()
{
$emailTracking = vglobal('email_tracking');
if (empty($emailTracking) || 'Yes' === $emailTracking) {
return true;
}
return false;
}
/**
* @param int $parentId
* @param string $redirectUrl
* @param string $linkName
* @return string
*/
public function getTrackUrlForClicks($redirectUrl = false, $linkName = false)
{
$params = [
'record' => $this->getId(),
'parentId' => $this->getRelatedTo(),
'method' => 'click',
];
if ($redirectUrl) {
$params['redirectUrl'] = $redirectUrl;
}
if ($linkName) {
$params['linkName'] = $linkName;
}
return Vtiger_ShortURL_Helper::generateURL([
'handler_path' => 'modules/ITS4YouEmails/handlers/Tracker.php',
'handler_class' => 'ITS4YouEmails_Tracker_Handler',
'handler_function' => 'process',
'handler_data' => $params,
]);
}
public function getRelatedTo()
{
return intval($this->get('related_to'));
}
/**
* @param string $content
* @param string $toReplace
* @param string $search
* @param string $type
* @return string
*/
public function replaceLinkWithShortUrl($content, $toReplace, $search, $type)
{
if ('html' === $type) {
$search = '"' . $search . '"';
$toReplace = '"' . $toReplace . '"';
}
$position = strpos($content, $search);
if (false !== $position) {
return substr_replace($content, $toReplace, $position) . substr($content, $position + strlen($search));
}
return $content;
}
public function convertCssToInline($content)
{
if (preg_match('/