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;
 }
 function generate_email()
 {
     global $mod_strings;
     global $current_user;
     global $sugar_config;
     global $locale;
     require_once 'include/utils.php';
     $query = 'SELECT name';
     $query .= ' FROM emails';
     $query .= " WHERE deleted=0";
     $query .= " AND name='{$this->pnum}-Estimate'";
     $result = $this->db->query($query, true, " Error filling in additional detail fields: ");
     $n = $this->db->getRowCount($result);
     if ($n == 0) {
         $queryname = 'SELECT user_name';
         $queryname .= ' FROM users';
         $queryname .= " WHERE deleted=0";
         $queryname .= " AND id='{$this->assigned_user_id}'";
         $result_name = $this->db->query($queryname, true, " Error filling in additional detail fields: ");
         $username = $this->db->fetchByAssoc($result_name);
         $to_addrs_names = $username['user_name'];
         $queryemail = 'SELECT email1';
         $queryemail .= ' FROM users';
         $queryemail .= " WHERE deleted=0";
         $queryemail .= " AND id='{$this->assigned_user_id}'";
         $result_email = $this->db->query($queryemail, true, " Error filling in additional detail fields: ");
         $useremail = $this->db->fetchByAssoc($result_email);
         $to_addrs_emails = $useremail['email1'];
         $id = create_guid();
         $from_addr = $current_user->name;
         $from_name = $current_user->email1;
         $name = $this->pnum . '-Estimate';
         $description = $this->pnum . ' is waiting for estimate';
         $query2 = "INSERT into emails (id,  assigned_user_id, created_by, name, status,  to_addrs_names, to_addrs_emails, from_addr, from_name, description, deleted, type, intent) ";
         $query2 .= " VALUES ('{$id}', '{$this->assigned_user_id}', '{$current_user->id}', '{$name}', 'sent', '{$to_addrs_names}', '{$to_addrs_emails}', '{$from_addr}', '{$from_name}', '{$description}', '0', 'out', 'pick') ";
         $this->db->query($query2, true, " Error filling in additional detail fields: ");
         $mail = new SugarPHPMailer();
         $mail->AddAddress($to_addrs_emails, $to_addrs_names);
         $mail->Mailer = "sendmail";
         // FROM ADDRESS
         $mail->From = $from_addr;
         // FROM NAME
         $mail->FromName = $from_name;
         $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($name, ENT_QUOTES, $encoding);
         ///////////////////////////////////////////////////////////////////////
         ////	HANDLE EMAIL FORMAT PREFERENCE
         // the if() below is HIGHLY dependent on the Javascript unchecking the Send HTML Email box
         // HTML email
         // plain text only
         $description_html = '';
         $mail->IsHTML(false);
         $mail->Body = wordwrap(from_html($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();
         $raw_source = urlencode($raw);
         ////    END SAVE RAW MESSAGE
         ///////////////////////////////////////////////////////////////////////
         $GLOBALS['log']->debug('Email sending --------------------- ');
         ///////////////////////////////////////////////////////////////////////
         ////	I18N TRANSLATION
         $mail->prepForOutbound();
         ////	END I18N TRANSLATION
         ///////////////////////////////////////////////////////////////////////
         $mail->Send();
     }
 }