Exemplo n.º 1
0
 /**
  * Send new password or link to user
  *
  * @param string $templateId Id of email template
  * @param array $additionalData additional params: link, url, password
  * @return array status: true|false, message: error message, if status = false and message = '' it means that send method has returned false
  */
 public function sendEmailForPassword($templateId, array $additionalData = array())
 {
     global $sugar_config, $current_user;
     $mod_strings = return_module_language('', 'Users');
     $result = array('status' => false, 'message' => '');
     $emailTemp = new EmailTemplate();
     $emailTemp->disable_row_level_security = true;
     if ($emailTemp->retrieve($templateId) == '') {
         $result['message'] = $mod_strings['LBL_EMAIL_TEMPLATE_MISSING'];
         return $result;
     }
     //replace instance variables in email templates
     $htmlBody = $emailTemp->body_html;
     $body = $emailTemp->body;
     if (isset($additionalData['link']) && $additionalData['link'] == true) {
         $htmlBody = str_replace('$contact_user_link_guid', $additionalData['url'], $htmlBody);
         $body = str_replace('$contact_user_link_guid', $additionalData['url'], $body);
     } else {
         $htmlBody = str_replace('$contact_user_user_hash', $additionalData['password'], $htmlBody);
         $body = str_replace('$contact_user_user_hash', $additionalData['password'], $body);
     }
     // Bug 36833 - Add replacing of special value $instance_url
     $htmlBody = str_replace('$config_site_url', $sugar_config['site_url'], $htmlBody);
     $body = str_replace('$config_site_url', $sugar_config['site_url'], $body);
     $htmlBody = str_replace('$contact_user_user_name', $this->user_name, $htmlBody);
     $htmlBody = str_replace('$contact_user_pwd_last_changed', TimeDate::getInstance()->nowDb(), $htmlBody);
     $body = str_replace('$contact_user_user_name', $this->user_name, $body);
     $body = str_replace('$contact_user_pwd_last_changed', TimeDate::getInstance()->nowDb(), $body);
     $emailTemp->body_html = $htmlBody;
     $emailTemp->body = $body;
     $itemail = $this->emailAddress->getPrimaryAddress($this);
     //retrieve IT Admin Email
     //_ppd( $emailTemp->body_html);
     //retrieve email defaults
     $emailObj = new Email();
     $defaults = $emailObj->getSystemDefaultEmail();
     require_once 'include/SugarPHPMailer.php';
     $mail = new SugarPHPMailer();
     $mail->setMailerForSystem();
     //$mail->IsHTML(true);
     $mail->From = $defaults['email'];
     $mail->FromName = $defaults['name'];
     $mail->ClearAllRecipients();
     $mail->ClearReplyTos();
     $mail->Subject = from_html($emailTemp->subject);
     if ($emailTemp->text_only != 1) {
         $mail->IsHTML(true);
         $mail->Body = from_html($emailTemp->body_html);
         $mail->AltBody = from_html($emailTemp->body);
     } else {
         $mail->Body_html = from_html($emailTemp->body_html);
         $mail->Body = from_html($emailTemp->body);
     }
     if ($mail->Body == '' && $current_user->is_admin) {
         global $app_strings;
         $result['message'] = $app_strings['LBL_EMAIL_TEMPLATE_EDIT_PLAIN_TEXT'];
         return $result;
     }
     if ($mail->Mailer == 'smtp' && $mail->Host == '' && $current_user->is_admin) {
         $result['message'] = $mod_strings['ERR_SERVER_SMTP_EMPTY'];
         return $result;
     }
     $mail->prepForOutbound();
     $hasRecipients = false;
     if (!empty($itemail)) {
         if ($hasRecipients) {
             $mail->AddBCC($itemail);
         } else {
             $mail->AddAddress($itemail);
         }
         $hasRecipients = true;
     }
     if ($hasRecipients) {
         $result['status'] = @$mail->Send();
     }
     if ($result['status'] == true) {
         $emailObj->team_id = 1;
         $emailObj->to_addrs = '';
         $emailObj->type = 'archived';
         $emailObj->deleted = '0';
         $emailObj->name = $mail->Subject;
         $emailObj->description = $mail->Body;
         $emailObj->description_html = null;
         $emailObj->from_addr = $mail->From;
         $emailObj->parent_type = 'User';
         $emailObj->date_sent = TimeDate::getInstance()->nowDb();
         $emailObj->modified_user_id = '1';
         $emailObj->created_by = '1';
         $emailObj->status = 'sent';
         $emailObj->save();
         if (!isset($additionalData['link']) || $additionalData['link'] == false) {
             $user_hash = strtolower(md5($additionalData['password']));
             $this->setPreference('loginexpiration', '0');
             $this->setPreference('lockout', '');
             $this->setPreference('loginfailed', '0');
             $this->savePreferencesToDB();
             //set new password
             $now = TimeDate::getInstance()->nowDb();
             $query = "UPDATE {$this->table_name} SET user_hash='{$user_hash}', system_generated_password='******', pwd_last_changed='{$now}' where id='{$this->id}'";
             $this->db->query($query, true, "Error setting new password for {$this->user_name}: ");
         }
     }
     return $result;
 }
Exemplo n.º 2
0
 /**
  * Sends Email
  * @return bool True on success
  */
 function send()
 {
     global $mod_strings, $app_strings;
     global $current_user;
     global $sugar_config;
     global $locale;
     $OBCharset = $locale->getPrecedentPreference('default_email_charset');
     $mail = new SugarPHPMailer();
     foreach ($this->to_addrs_arr as $addr_arr) {
         if (empty($addr_arr['display'])) {
             $mail->AddAddress($addr_arr['email'], "");
         } else {
             $mail->AddAddress($addr_arr['email'], $locale->translateCharsetMIME(trim($addr_arr['display']), 'UTF-8', $OBCharset));
         }
     }
     foreach ($this->cc_addrs_arr as $addr_arr) {
         if (empty($addr_arr['display'])) {
             $mail->AddCC($addr_arr['email'], "");
         } else {
             $mail->AddCC($addr_arr['email'], $locale->translateCharsetMIME(trim($addr_arr['display']), 'UTF-8', $OBCharset));
         }
     }
     foreach ($this->bcc_addrs_arr as $addr_arr) {
         if (empty($addr_arr['display'])) {
             $mail->AddBCC($addr_arr['email'], "");
         } else {
             $mail->AddBCC($addr_arr['email'], $locale->translateCharsetMIME(trim($addr_arr['display']), 'UTF-8', $OBCharset));
         }
     }
     $mail = $this->setMailer($mail);
     // FROM ADDRESS
     if (!empty($this->from_addr)) {
         $mail->From = $this->from_addr;
     } else {
         $mail->From = $current_user->getPreference('mail_fromaddress');
         $this->from_addr = $mail->From;
     }
     // FROM NAME
     if (!empty($this->from_name)) {
         $mail->FromName = $this->from_name;
     } else {
         $mail->FromName = $current_user->getPreference('mail_fromname');
         $this->from_name = $mail->FromName;
     }
     //Reply to information for case create and autoreply.
     if (!empty($this->reply_to_name)) {
         $ReplyToName = $this->reply_to_name;
     } else {
         $ReplyToName = $mail->FromName;
     }
     if (!empty($this->reply_to_addr)) {
         $ReplyToAddr = $this->reply_to_addr;
     } else {
         $ReplyToAddr = $mail->From;
     }
     $mail->Sender = $mail->From;
     /* set Return-Path field in header to reduce spam score in emails sent via Sugar's Email module */
     $mail->AddReplyTo($ReplyToAddr, $locale->translateCharsetMIME(trim($ReplyToName), 'UTF-8', $OBCharset));
     //$mail->Subject = html_entity_decode($this->name, ENT_QUOTES, 'UTF-8');
     $mail->Subject = $this->name;
     ///////////////////////////////////////////////////////////////////////
     ////	ATTACHMENTS
     foreach ($this->saved_attachments as $note) {
         $mime_type = 'text/plain';
         if ($note->object_name == 'Note') {
             if (!empty($note->file->temp_file_location) && is_file($note->file->temp_file_location)) {
                 // brandy-new file upload/attachment
                 $file_location = $sugar_config['upload_dir'] . $note->id;
                 $filename = $note->file->original_file_name;
                 $mime_type = $note->file->mime_type;
             } else {
                 // attachment coming from template/forward
                 $file_location = rawurldecode(UploadFile::get_file_path($note->filename, $note->id));
                 // cn: bug 9723 - documents from EmailTemplates sent with Doc Name, not file name.
                 $filename = !empty($note->filename) ? $note->filename : $note->name;
                 $mime_type = $note->file_mime_type;
             }
         } elseif ($note->object_name == 'DocumentRevision') {
             // from Documents
             $filePathName = $note->id;
             // cn: bug 9723 - Emails with documents send GUID instead of Doc name
             $filename = $note->getDocumentRevisionNameForDisplay();
             $file_location = getcwd() . '/' . $GLOBALS['sugar_config']['upload_dir'] . $filePathName;
             $mime_type = $note->file_mime_type;
         }
         // strip out the "Email attachment label if exists
         $filename = str_replace($mod_strings['LBL_EMAIL_ATTACHMENT'] . ': ', '', $filename);
         //is attachment in our list of bad files extensions?  If so, append .txt to file location
         //get position of last "." in file name
         $file_ext_beg = strrpos($file_location, ".");
         $file_ext = "";
         //get file extension
         if ($file_ext_beg > 0) {
             $file_ext = substr($file_location, $file_ext_beg + 1);
         }
         //check to see if this is a file with extension located in "badext"
         foreach ($sugar_config['upload_badext'] as $badExt) {
             if (strtolower($file_ext) == strtolower($badExt)) {
                 //if found, then append with .txt to filename and break out of lookup
                 //this will make sure that the file goes out with right extension, but is stored
                 //as a text in db.
                 $file_location = $file_location . ".txt";
                 break;
                 // no need to look for more
             }
         }
         $mail->AddAttachment($file_location, $locale->translateCharsetMIME(trim($filename), 'UTF-8', $OBCharset), 'base64', $mime_type);
         // embedded Images
         if ($note->embed_flag == true) {
             $cid = $filename;
             $mail->AddEmbeddedImage($file_location, $cid, $filename, 'base64', $mime_type);
         }
     }
     ////	END ATTACHMENTS
     ///////////////////////////////////////////////////////////////////////
     $mail = $this->handleBody($mail);
     $GLOBALS['log']->debug('Email sending --------------------- ');
     ///////////////////////////////////////////////////////////////////////
     ////	I18N TRANSLATION
     $mail->prepForOutbound();
     ////	END I18N TRANSLATION
     ///////////////////////////////////////////////////////////////////////
     if ($mail->Send()) {
         ///////////////////////////////////////////////////////////////////
         ////	INBOUND EMAIL HANDLING
         // mark replied
         if (!empty($_REQUEST['inbound_email_id'])) {
             $ieMail = new Email();
             $ieMail->retrieve($_REQUEST['inbound_email_id']);
             $ieMail->status = 'replied';
             $ieMail->save();
         }
         $GLOBALS['log']->debug(' --------------------- buh bye -- sent successful');
         ////	END INBOUND EMAIL HANDLING
         ///////////////////////////////////////////////////////////////////
         return true;
     }
     $GLOBALS['log']->debug($app_strings['LBL_EMAIL_ERROR_PREPEND'] . $mail->ErrorInfo);
     return false;
 }
Exemplo n.º 3
0
 function send()
 {
     global $mod_strings;
     global $current_user;
     global $sugar_config;
     global $locale;
     $mail = new SugarPHPMailer();
     foreach ($this->to_addrs_arr as $addr_arr) {
         if (empty($addr_arr['display'])) {
             $mail->AddAddress($addr_arr['email'], "");
         } else {
             $mail->AddAddress($addr_arr['email'], $addr_arr['display']);
         }
     }
     foreach ($this->cc_addrs_arr as $addr_arr) {
         if (empty($addr_arr['display'])) {
             $mail->AddCC($addr_arr['email'], "");
         } else {
             $mail->AddCC($addr_arr['email'], $addr_arr['display']);
         }
     }
     foreach ($this->bcc_addrs_arr as $addr_arr) {
         if (empty($addr_arr['display'])) {
             $mail->AddBCC($addr_arr['email'], "");
         } else {
             $mail->AddBCC($addr_arr['email'], $addr_arr['display']);
         }
     }
     if ($current_user->getPreference('mail_sendtype') == "SMTP") {
         $mail->Mailer = "smtp";
         $mail->Host = $current_user->getPreference('mail_smtpserver');
         $mail->Port = $current_user->getPreference('mail_smtpport');
         if ($current_user->getPreference('mail_smtpauth_req')) {
             $mail->SMTPAuth = TRUE;
             $mail->Username = $current_user->getPreference('mail_smtpuser');
             $mail->Password = $current_user->getPreference('mail_smtppass');
         }
     } else {
         // cn:no need to check since we default to it in any case!
         $mail->Mailer = "sendmail";
     }
     // FROM ADDRESS
     if (!empty($this->from_addr)) {
         $mail->From = $this->from_addr;
     } else {
         $mail->From = $current_user->getPreference('mail_fromaddress');
         $this->from_addr = $mail->From;
     }
     // FROM NAME
     if (!empty($this->from_name)) {
         $mail->FromName = $this->from_name;
     } else {
         $mail->FromName = $current_user->getPreference('mail_fromname');
         $this->from_name = $mail->FromName;
     }
     $mail->Sender = $mail->From;
     /* set Return-Path field in header to reduce spam score in emails sent via Sugar's Email module */
     $mail->AddReplyTo($mail->From, $mail->FromName);
     $encoding = version_compare(phpversion(), '5.0', '>=') ? 'UTF-8' : 'ISO-8859-1';
     $mail->Subject = html_entity_decode($this->name, ENT_QUOTES, $encoding);
     ///////////////////////////////////////////////////////////////////////
     ////	ATTACHMENTS
     foreach ($this->saved_attachments as $note) {
         $mime_type = 'text/plain';
         if ($note->object_name == 'Note') {
             if (!empty($note->file->temp_file_location) && is_file($note->file->temp_file_location)) {
                 // brandy-new file upload/attachment
                 $file_location = $sugar_config['upload_dir'] . $note->id;
                 $filename = $note->file->original_file_name;
                 $mime_type = $note->file->mime_type;
             } else {
                 // attachment coming from template/forward
                 $file_location = rawurldecode(UploadFile::get_file_path($note->filename, $note->id));
                 $filename = $note->name;
                 $mime_type = $note->file_mime_type;
             }
         } elseif ($note->object_name == 'DocumentRevision') {
             // from Documents
             $filename = $note->id;
             $file_location = getcwd() . '/cache/upload/' . $filename;
             $mime_type = $note->file_mime_type;
         }
         // strip out the "Email attachment label if exists
         $filename = str_replace($mod_strings['LBL_EMAIL_ATTACHMENT'] . ': ', '', $filename);
         // cn: bug 9233 attachment filenames need to be translated into the destination charset.
         $filename = $locale->translateCharset($filename, 'UTF-8', $locale->getPrecedentPreference('default_email_charset'));
         //is attachment in our list of bad files extensions?  If so, append .txt to file location
         //get position of last "." in file name
         $file_ext_beg = strrpos($file_location, ".");
         $file_ext = "";
         //get file extension
         if ($file_ext_beg > 0) {
             $file_ext = substr($file_location, $file_ext_beg + 1);
         }
         //check to see if this is a file with extension located in "badext"
         foreach ($sugar_config['upload_badext'] as $badExt) {
             if (strtolower($file_ext) == strtolower($badExt)) {
                 //if found, then append with .txt to filename and break out of lookup
                 //this will make sure that the file goes out with right extension, but is stored
                 //as a text in db.
                 $file_location = $file_location . ".txt";
                 break;
                 // no need to look for more
             }
         }
         $mail->AddAttachment($file_location, $filename, 'base64', $mime_type);
     }
     ////	END ATTACHMENTS
     ///////////////////////////////////////////////////////////////////////
     ///////////////////////////////////////////////////////////////////////
     ////	HANDLE EMAIL FORMAT PREFERENCE
     // the if() below is HIGHLY dependent on the Javascript unchecking the Send HTML Email box
     // HTML email
     if (isset($_REQUEST['setEditor']) && $_REQUEST['setEditor'] == 1 && trim($_REQUEST['description_html']) != '' || trim($this->description_html) != '') {
         // wp: if body is html, then insert new lines at 996 characters. no effect on client side
         // due to RFC 2822 which limits email lines to 998
         $mail->IsHTML(true);
         $body = from_html(wordwrap($this->description_html, 996));
         $mail->Body = $body;
         // if alternative body is defined, use that, else, striptags the HTML part
         if (trim($this->description) == '') {
             $plainText = from_html($this->description_html);
             $plainText = strip_tags(br2nl($plainText));
             //$plainText = $locale->translateCharset($plainText, 'UTF-8', $locale->getPrecedentPreference('default_email_charset'));
             $mail->AltBody = $plainText;
             $this->description = $plainText;
         } else {
             $mail->AltBody = wordwrap(from_html($this->description), 996);
         }
         // cn: bug 9709 - html email sent accidentally
         // handle signatures fubar'ing the type
         $sigs = $current_user->getDefaultSignature();
         $htmlSig = trim(str_replace(" ", "", strip_tags(from_html($sigs['signature_html']))));
         $htmlBody = trim(str_replace(" ", "", strip_tags(from_html($this->description_html))));
         if ($htmlSig == $htmlBody) {
             // found just a sig. ignore it.
             $this->description_html = '';
             $mail->IsHTML(false);
             $mail->Body = wordwrap(from_html($this->description, 996));
         }
     } else {
         // plain text only
         $this->description_html = '';
         $mail->IsHTML(false);
         $mail->Body = wordwrap(from_html($this->description, 996));
     }
     // wp: if plain text version has lines greater than 998, use base64 encoding
     foreach (explode("\n", $mail->ContentType == "text/html" ? $mail->AltBody : $mail->Body) as $line) {
         if (strlen($line) > 998) {
             $mail->Encoding = 'base64';
             break;
         }
     }
     ////	HANDLE EMAIL FORMAT PREFERENCE
     ///////////////////////////////////////////////////////////////////////
     ///////////////////////////////////////////////////////////////////////
     ////    SAVE RAW MESSAGE
     $mail->SetMessageType();
     $raw = $mail->CreateHeader();
     $raw .= $mail->CreateBody();
     $this->raw_source = urlencode($raw);
     ////    END SAVE RAW MESSAGE
     ///////////////////////////////////////////////////////////////////////
     $GLOBALS['log']->debug('Email sending --------------------- ');
     ///////////////////////////////////////////////////////////////////////
     ////	I18N TRANSLATION
     $mail->prepForOutbound();
     ////	END I18N TRANSLATION
     ///////////////////////////////////////////////////////////////////////
     if ($mail->Send()) {
         ///////////////////////////////////////////////////////////////////
         ////	INBOUND EMAIL HANDLING
         // mark replied
         if (!empty($_REQUEST['inbound_email_id'])) {
             $ieMail = new Email();
             $ieMail->retrieve($_REQUEST['inbound_email_id']);
             $ieMail->status = 'replied';
             $ieMail->save();
         }
         $GLOBALS['log']->debug(' --------------------- buh bye -- sent successful');
         ////	END INBOUND EMAIL HANDLING
         ///////////////////////////////////////////////////////////////////
         return true;
     }
     $GLOBALS['log']->fatal("Error emailing:" . $mail->ErrorInfo);
     return false;
 }
Exemplo n.º 4
0
}
if ($mail->Body == '' && $current_user->is_admin) {
    echo $app_strings['LBL_EMAIL_TEMPLATE_EDIT_PLAIN_TEXT'];
    $new_pwd = '4';
    return;
}
if ($mail->Mailer == 'smtp' && $mail->Host == '' && $current_user->is_admin) {
    echo $mod_strings['ERR_SERVER_SMTP_EMPTY'];
    $new_pwd = '4';
    return;
}
$mail->prepForOutbound();
$hasRecipients = false;
if (!empty($itemail)) {
    if ($hasRecipients) {
        $mail->AddBCC($itemail);
    } else {
        $mail->AddAddress($itemail);
    }
    $hasRecipients = true;
}
$success = false;
if ($hasRecipients) {
    $success = @$mail->Send();
}
//now create email
if ($success) {
    $emailObj->team_id = 1;
    $emailObj->to_addrs = '';
    $emailObj->type = 'archived';
    $emailObj->deleted = '0';
Exemplo n.º 5
0
 function sendEmail($emailTo, $emailSubject, $emailBody, $altemailBody, SugarBean $relatedBean = null, $emailCc = array(), $emailBcc = array(), $attachments = array())
 {
     require_once 'modules/Emails/Email.php';
     require_once 'include/SugarPHPMailer.php';
     $emailObj = new Email();
     $defaults = $emailObj->getSystemDefaultEmail();
     $mail = new SugarPHPMailer();
     $mail->setMailerForSystem();
     $mail->From = $defaults['email'];
     $mail->FromName = $defaults['name'];
     $mail->ClearAllRecipients();
     $mail->ClearReplyTos();
     $mail->Subject = from_html($emailSubject);
     $mail->Body = $emailBody;
     $mail->AltBody = $altemailBody;
     $mail->handleAttachments($attachments);
     $mail->prepForOutbound();
     if (empty($emailTo)) {
         return false;
     }
     foreach ($emailTo as $to) {
         $mail->AddAddress($to);
     }
     if (!empty($emailCc)) {
         foreach ($emailCc as $email) {
             $mail->AddCC($email);
         }
     }
     if (!empty($emailBcc)) {
         foreach ($emailBcc as $email) {
             $mail->AddBCC($email);
         }
     }
     //now create email
     if (@$mail->Send()) {
         $emailObj->to_addrs = implode(',', $emailTo);
         $emailObj->cc_addrs = implode(',', $emailCc);
         $emailObj->bcc_addrs = implode(',', $emailBcc);
         $emailObj->type = 'out';
         $emailObj->deleted = '0';
         $emailObj->name = $mail->Subject;
         $emailObj->description = $mail->AltBody;
         $emailObj->description_html = $mail->Body;
         $emailObj->from_addr = $mail->From;
         if ($relatedBean instanceof SugarBean && !empty($relatedBean->id)) {
             $emailObj->parent_type = $relatedBean->module_dir;
             $emailObj->parent_id = $relatedBean->id;
         }
         $emailObj->date_sent = TimeDate::getInstance()->nowDb();
         $emailObj->modified_user_id = '1';
         $emailObj->created_by = '1';
         $emailObj->status = 'sent';
         $emailObj->save();
         return true;
     }
     return false;
 }