X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=WEB-INF%2Flib%2Fmail%2FMailer.class.php;h=30f0ffb64b20c8b249d8a1b437bb1b83de7445ee;hb=75a1eedb8977b8f2db459128bab9aaf367e3b58b;hp=eb4e7f9a6b840e1a501101ee2b313cc9c7e355a1;hpb=466c7825f8313f5e3a11c63383537c8bb302a932;p=timetracker.git diff --git a/WEB-INF/lib/mail/Mailer.class.php b/WEB-INF/lib/mail/Mailer.class.php index eb4e7f9a..30f0ffb6 100644 --- a/WEB-INF/lib/mail/Mailer.class.php +++ b/WEB-INF/lib/mail/Mailer.class.php @@ -1,122 +1,136 @@ mSendType = $type; + var $mMailMode; + var $mCharSet = 'iso-8859-1'; + var $mContentType = 'text/plain'; + var $mSender; + var $mReceiver; + var $mReceiverCC; + var $mReceiverBCC; + + function __construct($type='mail') { + $this->mMailMode = $type; + } + + function setMailMode($value) { + $this->mMailMode = $value; + } + + function setCharSet($value) { + $this->mCharSet = $value; + } + + function setContentType($value) { + $this->mContentType = $value; + } + + function setReceiver($value) { + $this->mReceiver = $value; + } + + function setReceiverCC($value) { + $this->mReceiverCC = $value; + } + + function setReceiverBCC($value) { + $this->mReceiverBCC = $value; + } + + function setSender($value) { + $this->mSender = $value; + } + + function send($subject, $data) { + $data = chunk_split(base64_encode($data)); + $subject = Mailer::mimeEncode($subject, $this->mCharSet); + + $headers = array('From' => $this->mSender, 'To' => $this->mReceiver); + if (isset($this->mReceiverCC)) $headers = array_merge($headers, array('CC' => $this->mReceiverCC)); + if (isset($this->mReceiverBCC)) $headers = array_merge($headers, array('BCC' => $this->mReceiverBCC)); + $headers = array_merge($headers, array( + 'Subject' => $subject, + 'MIME-Version' => '1.0', + 'Content-Type' => $this->mContentType.'; charset='.$this->mCharSet, + 'Content-Transfer-Encoding' => 'BASE64')); + + // PEAR::Mail + require_once('Mail.php'); + + $recipients = $this->mReceiver; + switch ($this->mMailMode) { + case 'mail': + $mail = Mail::factory('mail'); + break; + + case 'smtp': + // Mail_smtp does not do CC or BCC -> recipients conversion. + if (!empty($this->mReceiverCC)) { + // make exactly one space after a comma + $recipients .= ', ' . preg_replace('/,[[:space:]]+/', ', ', $this->mReceiverCC); + } + if (!empty($this->mReceiverBCC)) { + // make exactly one space after a comma + $recipients .= ', ' . preg_replace('/,[[:space:]]+/', ', ', $this->mReceiverBCC); + } + + $host = defined('MAIL_SMTP_HOST') ? MAIL_SMTP_HOST : 'localhost'; + $port = defined('MAIL_SMTP_PORT') ? MAIL_SMTP_PORT : '25'; + $username = defined('MAIL_SMTP_USER') ? MAIL_SMTP_USER : null; + $password = defined('MAIL_SMTP_PASSWORD') ? MAIL_SMTP_PASSWORD : null; + $auth = isTrue('MAIL_SMTP_AUTH'); + $debug = isTrue('MAIL_SMTP_DEBUG'); + + $mail = Mail::factory('smtp', array ('host' => $host, + 'port' => $port, + 'username' => $username, + 'password' => $password, + 'auth' => $auth, + 'debug' => $debug)); + break; } - function setSendType($value) { - $this->mSendType = $value; - } - - function setCharSet($value) { - $this->mCharSet = $value; - } + if (isTrue('MAIL_SMTP_DEBUG')) + PEAR::setErrorHandling(PEAR_ERROR_PRINT); - function setContentType($value) { - $this->mContentType = $value; - } - - function setReceiver($value) { - $this->mReceiver = $value; - } + $res = $mail->send($recipients, $headers, $data); + return (!is_a($res, 'PEAR_Error')); + } - function setReceiverCC($value) { - $this->mReceiverCC = $value; + function mimeEncode($in_str, $charset) { + $out_str = $in_str; + if ($out_str && $charset) { + $start = '=?'.strtoupper($charset).'?B?'; + $end = '?='; + $out_str = base64_encode($out_str); + $out_str = $start . $out_str . $end; } - - function setSender($value) { - $this->mSender = $value; - } - - function send($subject, $data) { - $data = chunk_split(base64_encode($data)); - $subject = Mailer::mimeEncode($subject, $this->mCharSet); - - $headers = array( - 'From' => $this->mSender, - 'To' => $this->mReceiver); - if (isset($this->mReceiverCC)) $headers = array_merge($headers, array( - 'CC' => $this->mReceiverCC)); - $headers = array_merge($headers, array( - 'Subject' => $subject, - 'MIME-Version' => '1.0', - 'Content-Type' => $this->mContentType.'; charset='.$this->mCharSet, - 'Content-Transfer-Encoding' => 'BASE64', - )); - - // PEAR::Mail - require_once('Mail.php'); - - $recipients = $this->mReceiver; - switch ($this->mSendType) { - case 'mail': - $mail = Mail::factory('mail'); - break; - - case "smtp": - // Mail_smtp does not do CC -> recipients conversion - if (!empty($this->mReceiverCC)) { - // make exactly one space after a comma - $recipients .= ', ' . preg_replace('/,[[:space:]]+/', ', ', $this->mReceiverCC);; - } - - $host = defined('MAIL_SMTP_HOST') ? MAIL_SMTP_HOST : 'localhost'; - $port = defined('MAIL_SMTP_PORT') ? MAIL_SMTP_PORT : '25'; - $username = defined('MAIL_SMTP_USER') ? MAIL_SMTP_USER : null; - $password = defined('MAIL_SMTP_PASSWORD') ? MAIL_SMTP_PASSWORD : null; - $auth = (defined('MAIL_SMTP_AUTH') && isTrue(MAIL_SMTP_AUTH)) ? true : false; - $debug = (defined('MAIL_SMTP_DEBUG') && isTrue(MAIL_SMTP_DEBUG)) ? true : false; - - $mail = Mail::factory('smtp', array ('host' => $host, - 'port' => $port, - 'username' => $username, - 'password' => $password, - 'auth' => $auth, - 'debug' => $debug)); - break; - } - - if (defined('MAIL_SMTP_DEBUG') && isTrue(MAIL_SMTP_DEBUG)) - PEAR::setErrorHandling(PEAR_ERROR_PRINT); - $res = $mail->send($recipients, $headers, $data); - return (!is_a($res, 'PEAR_Error')); - } - - /** - * convert to base64-string - * - * @param string $in_str - * @param string $charset - * @return string - */ - function mimeEncode($in_str, $charset) { - $out_str = $in_str; - if ($out_str && $charset) { - - $end = "?="; - $start = "=?" . strtoupper($charset) . "?B?"; - $spacer = $end . "\r\n " . $start; - - $length = 75 - strlen($start) - strlen($end); - $length = floor($length/2) * 2; - - $out_str = base64_encode($out_str); - //$out_str = Mail::encodemime($out_str,"base64"); - //$out_str = chunk_split($out_str, $length, $spacer); - - //$spacer = preg_quote($spacer); - //$out_str = preg_replace("/" . $spacer . "$/", "", $out_str); - $out_str = $start . $out_str . $end; - } - return $out_str; - } -} \ No newline at end of file + return $out_str; + } +}