Files
crm.e5.pl/include/ECM/EcmSendMail/EcmSendMail.inc

181 lines
5.6 KiB
PHP

<?php
/*
* add mz 2014-12-29
* Class can send email from any module using system mail service
* getStatus() returns 1 if message was sended, or error message if sth goes wrong
*/
class EcmSendMail {
const TEMP_DIR = "upload/EcmSendMailTemp/";
private $to_addresses = NULL; // array with recipients addresses
private $cc_addresses = NULL; // array with CC addresses
private $bcc_addresses = NULL; // array with BCC addresses
private $attachments = NULL; // array with attachments filenames
private $from = NULL;
private $fromName = NULL;
private $replyTo = NULL;
private $replyToName = NULL;
private $subject = NULL;
private $message = NULL;
private $status;
private $OBCharset;
private $clearTemp = false;
private $s; // class strings
function __construct() {
global $locale, $current_language;
$this->OBCharset = $locale->getPrecedentPreference ( 'default_email_charset' );
// get labels if exists
if (file_exists ( 'include/ECM/EcmSendMail/' . $current_language . '.php' ))
include_once ('include/ECM/EcmSendMail/' . $current_language . '.php');
else
// othervise load en_us
include_once ('include/ECM/EcmSendMail/en_us.php');
$this->s = $strings;
//create temp dir if not exists
if (!is_dir(self::TEMP_DIR))
mkdir(self::TEMP_DIR);
}
public function sendMail() {
include_once ('include/SugarPHPMailer.php');
include_once ('include/utils/db_utils.php'); // for from_html function
$mail = new SugarPHPMailer ();
$mail->prepForOutbound ();
$mail->setMailerForSystem ();
// clear addresses
$mail->ClearAllRecipients ();
$mail->ClearReplyTos ();
$mail->ClearCCs ();
$mail->ClearBCCs ();
// Add recipients
if (! is_array ( $this->to_addresses ) || sizeof ( $this->to_addresses ) == 0) {
$this->status = $this->s ['LBL_NO_TO_ADDRESS'];
return;
}
foreach ( $this->to_addresses as $k => $v )
$mail->AddAddress ( $k, $this->t ( $v ) ); // function t() is explained below
// Add CC
if (is_array ( $this->cc_addresses ) || sizeof ( $this->cc_addresses ) > 0) {
foreach ( $this->cc_addresses as $k => $v )
$mail->AddCC ( $k, $this->t ( $v ) );
}
// Add BCC
if (is_array ( $this->bcc_addresses ) || sizeof ( $this->bcc_addresses ) > 0) {
foreach ( $this->bcc_addresses as $k => $v )
$mail->AddBCC ( $k, $this->t ( $v ) );
}
// if sender is not defined by setSender(), get values from user settings
if (! $this->from || ! $this->fromName) {
global $current_user;
$u = new User ();
$u->retrieve ( $current_user->id );
}
if (! $this->from)
$this->from = $u->emailAddress->getPrimaryAddress ( $u );
if (! $this->fromName)
$this->fromName = (! empty ( $replyToName )) ? $current_user->getPreference ( 'mail_fromname' ) : $current_user->full_name;
if ($u)
unset ( $u );
$mail->From = $this->from;
$mail->FromName = $this->fromName;
// Add reply to informations if they're setted
if ($this->replyToName && $this->replyTo)
$mail->AddReplyTo ( $this->replyTo, $this->replyToName );
$mail->Sender = $mail->From; /* set Return-Path field in header to reduce spam score in emails sent via Sugar's Email module */
// Add subject
$mail->Subject = $this->subject;
// Prepare and add message
// Using code from handleBodyInHTMLformat(), /modules/Email/Email.php
$mail->IsHTML ( true );
$mail->Body = from_html ( wordwrap ( $this->message, 996 ) );
$plainText = from_html ( $this->description_html );
$plainText = strip_tags ( br2nl ( $plainText ) );
$mail->AltBody = $plainText;
// add attachments
if (is_array ( $this->attachments ) && sizeof ( $this->attachments ) > 0) {
foreach ( $this->attachments as $f ) {
$filename = $f;
$location = self::TEMP_DIR.$filename;
if (! file_exists ( $location ) || is_dir ( $location )) {
$this->status = $this->s ['LBL_NO_ATTACHMENT'] . $filename;
return;
}
// get file mime type
$finfo = finfo_open ( FILEINFO_MIME_TYPE );
$mime_type = finfo_file ( $finfo, $location );
finfo_close ( $finfo );
// add attachment
$mail->AddAttachment ( $location, $this->t ( $filename ), 'base64', $mime_type );
}
}
// Send mail, log if there is error
echo "?";
if (! $mail->Send ()) {
echo ' tu?';
var_dump( $mail->ErrorInfo);
$this->status = $this->s ['LBL_SEND_ERROR'];
return;
}
if ($this->clearTemp)
$this->deleteTempFiles ();
$this->status = 1;
}
private function deleteTempFiles() {
foreach ( $this->attachments as $f )
unlink ( self::TEMP_DIR.$f );
}
// helper function to translate charset
private function t($s) {
global $locale;
return $locale->translateCharsetMIME ( trim ( $s ), 'UTF-8', $this->OBCharset );
}
// swetery ;)
public function setSubject($subject) {
$this->subject = $this->t ( $subject );
}
public function setMessage($message) {
$this->message = $message;
}
public function setAddresses($to_addresses) {
$this->to_addresses = $to_addresses;
}
public function setCCAddresses($cc_addresses) {
$this->cc_addresses = $cc_addresses;
}
public function setBCCAddresses($bcc_addresses) {
$this->bcc_addresses = $bcc_addresses;
}
public function setAttachments($attachments) {
$this->attachments = $attachments;
}
public function setSender($from, $fromName) {
$this->from = $from;
$this->fromName = $this->t ( $fromName );
}
public function setReplyTo($replyTo, $replyToName) {
$this->replyTo = $replyTo;
$this->replyToName = $this->t ( $replyToName );
}
public function setClearTemp($clearTemp) {
$this->clearTemp = $clearTemp;
}
// getery ;)
public function getStatus() {
return $this->status;
}
}
?>