mailer = $mailer; } protected function instanciate() { if( null == $this->mailer ) { $this->mailer = new PHPMailer( true ); $this->mailer->IsSMTP(); $this->mailer->CharSet = defined('SMTPMAILER_CHARSET_ENCODING')? SMTPMAILER_CHARSET_ENCODING : "utf-8"; $this->mailer->Host = defined('SMTPMAILER_SMTP_SERVER_ADDRESS')? SMTPMAILER_SMTP_SERVER_ADDRESS : "localhost"; $this->mailer->Port = defined('SMTPMAILER_SMTP_SERVER_PORT')? SMTPMAILER_SMTP_SERVER_PORT : 25; $this->mailer->SMTPSecure = defined('SMTPMAILER_USE_SECURE_CONNECTION')? strtolower( SMTPMAILER_USE_SECURE_CONNECTION ) : ''; $this->mailer->SMTPAuth = defined('SMTPMAILER_DO_AUTHENTICATE')? SMTPMAILER_DO_AUTHENTICATE : false; if( $this->mailer->SMTPAuth ) { $this->mailer->Username = defined('SMTPMAILER_USERNAME')? SMTPMAILER_USERNAME : "username"; $this->mailer->Password = defined('SMTPMAILER_PASSWORD')? SMTPMAILER_PASSWORD : "password"; } $this->mailer->SMTPDebug = defined('SMTPMAILER_DEBUG_MESSAGING_LEVEL')? SMTPMAILER_DEBUG_MESSAGING_LEVEL : 0; $this->mailer->SetLanguage( defined('SMTPMAILER_LANGUAGE_OF_MESSAGES')? SMTPMAILER_LANGUAGE_OF_MESSAGES : 'en' ); } } /* Overwriting Mailer's function */ function sendPlain($to, $from, $subject, $plainContent, $attachedFiles = false, $customheaders = false) { $this->instanciate(); $this->mailer->IsHTML( false ); $this->mailer->Body = $plainContent; $this->sendMailViaSmtp( $to, $from, $subject, $attachedFiles, $customheaders, false ); } /* Overwriting Mailer's function */ function sendHTML($to, $from, $subject, $htmlContent, $attachedFiles = false, $customheaders = false, $plainContent = false, $inlineImages = false) { $this->instanciate(); $this->mailer->IsHTML( true ); if( $inlineImages ) { $this->mailer->MsgHTML( $htmlContent, Director::baseFolder() ); } else { $this->mailer->Body = $htmlContent; if( empty( $plainContent ) ) $plainContent = trim( Convert::html2raw( $htmlContent ) ); $this->mailer->AltBody = $plainContent; } $this->sendMailViaSmtp( $to, $from, $subject, $attachedFiles, $customheaders, $inlineImages ); } protected function sendMailViaSmtp( $to, $from, $subject, $attachedFiles = false, $customheaders = false, $inlineImages = false ) { if( $this->mailer->SMTPDebug > 0 ) echo "*** Debug mode is on, printing debug messages and not redirecting to the website:
"; $msgForLog = "\n*** The sender was : $from\n*** The message was :\n{$this->mailer->AltBody}\n"; try { $this->buildBasicMail( $to, $from, $subject ); $this->addCustomHeaders( $customheaders ); $this->attachFiles( $attachedFiles ); $this->mailer->Send(); if( $this->mailer->SMTPDebug > 0 ) { echo "*** E-mail to $to has been sent.
"; echo "*** The debug mode blocked the process to avoid the url redirection. So the CC e-mail is not sent."; die(); } } catch( phpmailerException $e ) { $this->handleError( $e->errorMessage(), $msgForLog ); } catch( Exception $e ) { $this->handleError( $e->getMessage(), $msgForLog ); } } function handleError( $e, $msgForLog ) { $msg = $e . $msgForLog; if( $this->mailer->SMTPDebug > 0 ) { echo( $msg ); } Debug::log( $msg ); die(); } protected function buildBasicMail( $to, $from, $subject ) { if( preg_match('/(\'|")(.*?)\1[ ]+<[ ]*(.*?)[ ]*>/', $from, $from_splitted ) ) { // If $from countain a name, e.g. "My Name" $this->mailer->SetFrom( $from_splitted[3], $from_splitted[2] ); } else { $this->mailer->SetFrom( $from ); } $to = validEmailAddr( $to ); $this->mailer->ClearAddresses(); $addresses = explode(',',$to); foreach($addresses as $address){ $address = trim($address); $this->mailer->AddAddress( $address, ucfirst( substr( $address, 0, strpos( $address, '@' ) ) ) ); // For the recipient's name, the string before the @ from the e-mail address is used } $this->mailer->Subject = $subject; } protected function addCustomHeaders( $headers ) { if( null == $headers or !is_array( $headers ) ) $headers = array(); if( !isset( $headers["X-Mailer"] ) ) $headers["X-Mailer"] = X_MAILER; if( !isset( $headers["X-Priority"] ) ) $headers["X-Priority"] = 3; $this->mailer->ClearCustomHeaders(); foreach( $headers as $header_name => $header_value ) { $this->mailer->AddCustomHeader( $header_name.':'.$header_value ); } } protected function attachFiles( $attachedFiles ) { if( !empty( $attachedFiles ) and is_array( $attachedFiles ) ) { foreach( $attachedFiles as $attachedFile ) { $this->mailer->AddAttachment( Director::baseFolder().DIRECTORY_SEPARATOR.$attachedFile['filename'] ); } } } } ?>