📄 email.php
字号:
<?php
/*
[SOOBIC!] admin/send_mail.php
Version: 1.5
Author: Soolan (soolan@qq.com)
Copyright: soolan (www.soobic.com)
This is Modified by Soolan: 2005/4/1 10:00
*/
class email {
var $html;
var $text;
var $output;
var $html_text;
var $html_images;
var $image_types;
var $build_params;
var $attachments;
var $headers;
function email($headers = '') {
if ($headers == '') $headers = array();
$this->html_images = array();
$this->headers = array();
if (EMAIL_LINEFEED == 'CRLF') {
$this->lf = "\r\n";
} else {
$this->lf = "\n";
}
$this->image_types = array('gif' => 'image/gif',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpe' => 'image/jpeg',
'bmp' => 'image/bmp',
'png' => 'image/png',
'tif' => 'image/tiff',
'tiff' => 'image/tiff',
'swf' => 'application/x-shockwave-flash');
$this->build_params['html_encoding'] = 'quoted-printable';
$this->build_params['text_encoding'] = '7bit';
$this->build_params['html_charset'] = constant('CHARSET');
$this->build_params['text_charset'] = constant('CHARSET');
$this->build_params['text_wrap'] = 998;
$this->headers[] = 'MIME-Version: 1.0';
reset($headers);
while (list(,$value) = each($headers)) {
if (tep_not_null($value)) {
$this->headers[] = $value;
}
}
}
function get_file($filename) {
$return = '';
if ($fp = fopen($filename, 'rb')) {
while (!feof($fp)) {
$return .= fread($fp, 1024);
}
fclose($fp);
return $return;
} else {
return false;
}
}
function find_html_images($images_dir) {
while (list($key, ) = each($this->image_types)) {
$extensions[] = $key;
}
preg_match_all('/"([^"]+\.(' . implode('|', $extensions).'))"/Ui', $this->html, $images);
for ($i=0; $i<count($images[1]); $i++) {
if (file_exists($images_dir . $images[1][$i])) {
$html_images[] = $images[1][$i];
$this->html = str_replace($images[1][$i], basename($images[1][$i]), $this->html);
}
}
if (tep_not_null($html_images)) {
$html_images = array_unique($html_images);
sort($html_images);
for ($i=0; $i<count($html_images); $i++) {
if ($image = $this->get_file($images_dir . $html_images[$i])) {
$content_type = $this->image_types[substr($html_images[$i], strrpos($html_images[$i], '.') + 1)];
$this->add_html_image($image, basename($html_images[$i]), $content_type);
}
}
}
}
function add_text($text = '') {
$this->text = tep_convert_linefeeds(array("\r\n", "\n", "\r"), $this->lf, $text);
}
function add_html($html, $text = NULL, $images_dir = NULL) {
$this->html = tep_convert_linefeeds(array("\r\n", "\n", "\r"), '<br>', $html);
$this->html_text = tep_convert_linefeeds(array("\r\n", "\n", "\r"), $this->lf, $text);
if (isset($images_dir)) $this->find_html_images($images_dir);
}
function add_html_image($file, $name = '', $c_type='application/octet-stream') {
$this->html_images[] = array('body' => $file,
'name' => $name,
'c_type' => $c_type,
'cid' => md5(uniqid(time())));
}
function add_attachment($file, $name = '', $c_type='application/octet-stream', $encoding = 'base64') {
$this->attachments[] = array('body' => $file,
'name' => $name,
'c_type' => $c_type,
'encoding' => $encoding);
}
function add_text_part(&$obj, $text) {
$params['content_type'] = 'text/plain';
$params['encoding'] = $this->build_params['text_encoding'];
$params['charset'] = $this->build_params['text_charset'];
if (is_object($obj)) {
return $obj->addSubpart($text, $params);
} else {
return new mime($text, $params);
}
}
function add_html_part(&$obj) {
$params['content_type'] = 'text/html';
$params['encoding'] = $this->build_params['html_encoding'];
$params['charset'] = $this->build_params['html_charset'];
if (is_object($obj)) {
return $obj->addSubpart($this->html, $params);
} else {
return new mime($this->html, $params);
}
}
function add_mixed_part() {
$params['content_type'] = 'multipart/mixed';
return new mime('', $params);
}
function add_alternative_part(&$obj) {
$params['content_type'] = 'multipart/alternative';
if (is_object($obj)) {
return $obj->addSubpart('', $params);
} else {
return new mime('', $params);
}
}
function add_related_part(&$obj) {
$params['content_type'] = 'multipart/related';
if (is_object($obj)) {
return $obj->addSubpart('', $params);
} else {
return new mime('', $params);
}
}
function add_html_image_part(&$obj, $value) {
$params['content_type'] = $value['c_type'];
$params['encoding'] = 'base64';
$params['disposition'] = 'inline';
$params['dfilename'] = $value['name'];
$params['cid'] = $value['cid'];
$obj->addSubpart($value['body'], $params);
}
function add_attachment_part(&$obj, $value) {
$params['content_type'] = $value['c_type'];
$params['encoding'] = $value['encoding'];
$params['disposition'] = 'attachment';
$params['dfilename'] = $value['name'];
$obj->addSubpart($value['body'], $params);
}
function build_message($params = '') {
if ($params == '') $params = array();
if (count($params) > 0) {
reset($params);
while(list($key, $value) = each($params)) {
$this->build_params[$key] = $value;
}
}
if (tep_not_null($this->html_images)) {
reset($this->html_images);
while (list(,$value) = each($this->html_images)) {
$this->html = str_replace($value['name'], 'cid:' . $value['cid'], $this->html);
}
}
$null = NULL;
$attachments = ((tep_not_null($this->attachments)) ? true : false);
$html_images = ((tep_not_null($this->html_images)) ? true : false);
$html = ((tep_not_null($this->html)) ? true : false);
$text = ((tep_not_null($this->text)) ? true : false);
switch (true) {
case (($text == true) && ($attachments == false)):
$message = $this->add_text_part($null, $this->text);
break;
case (($text == false) && ($attachments == true) && ($html == false)):
$message = $this->add_mixed_part();
for ($i=0; $i<count($this->attachments); $i++) {
$this->add_attachment_part($message, $this->attachments[$i]);
}
break;
case (($text == true) && ($attachments == true)):
$message = $this->add_mixed_part();
$this->add_text_part($message, $this->text);
for ($i=0; $i<count($this->attachments); $i++) {
$this->add_attachment_part($message, $this->attachments[$i]);
}
break;
case (($html == true) && ($attachments == false) && ($html_images == false)):
if (tep_not_null($this->html_text)) {
$message = $this->add_alternative_part($null);
$this->add_text_part($message, $this->html_text);
$this->add_html_part($message);
} else {
$message = $this->add_html_part($null);
}
break;
case (($html == true) && ($attachments == false) && ($html_images == true)):
if (tep_not_null($this->html_text)) {
$message = $this->add_alternative_part($null);
$this->add_text_part($message, $this->html_text);
$related = $this->add_related_part($message);
} else {
$message = $this->add_related_part($null);
$related = $message;
}
$this->add_html_part($related);
for ($i=0; $i<count($this->html_images); $i++) {
$this->add_html_image_part($related, $this->html_images[$i]);
}
break;
case (($html == true) && ($attachments == true) && ($html_images == false)):
$message = $this->add_mixed_part();
if (tep_not_null($this->html_text)) {
$alt = $this->add_alternative_part($message);
$this->add_text_part($alt, $this->html_text);
$this->add_html_part($alt);
} else {
$this->add_html_part($message);
}
for ($i=0; $i<count($this->attachments); $i++) {
$this->add_attachment_part($message, $this->attachments[$i]);
}
break;
case (($html == true) && ($attachments == true) && ($html_images == true)):
$message = $this->add_mixed_part();
if (tep_not_null($this->html_text)) {
$alt = $this->add_alternative_part($message);
$this->add_text_part($alt, $this->html_text);
$rel = $this->add_related_part($alt);
} else {
$rel = $this->add_related_part($message);
}
$this->add_html_part($rel);
for ($i=0; $i<count($this->html_images); $i++) {
$this->add_html_image_part($rel, $this->html_images[$i]);
}
for ($i=0; $i<count($this->attachments); $i++) {
$this->add_attachment_part($message, $this->attachments[$i]);
}
break;
}
if ( (isset($message)) && (is_object($message)) ) {
$output = $message->encode();
$this->output = $output['body'];
reset($output['headers']);
while (list($key, $value) = each($output['headers'])) {
$headers[] = $key . ': ' . $value;
}
$this->headers = array_merge($this->headers, $headers);
return true;
} else {
return false;
}
}
function send($to_name, $to_addr, $from_name, $from_addr, $subject = '', $headers = '') {
$to = (($to_name != '') ? '"' . $to_name . '" <' . $to_addr . '>' : $to_addr);
$from = (($from_name != '') ? '"' . $from_name . '" <' . $from_addr . '>' : $from_addr);
if (is_string($headers)) {
$headers = explode($this->lf, trim($headers));
}
for ($i=0; $i<count($headers); $i++) {
if (is_array($headers[$i])) {
for ($j=0; $j<count($headers[$i]); $j++) {
if ($headers[$i][$j] != '') {
$xtra_headers[] = $headers[$i][$j];
}
}
}
if ($headers[$i] != '') {
$xtra_headers[] = $headers[$i];
}
}
if (!isset($xtra_headers)) {
$xtra_headers = array();
}
include ('./includes/config_sendmail.php');
if (EMAIL_TRANSPORT == 'smtp') {
ini_set('SMTP', $mailconfig['server']);
ini_set('smtp_port', $mailconfig['port']);
ini_set('sendmail_from', $from);
return mail($to_addr, $subject, $this->output, 'From: ' . $from . $this->lf . 'To: ' . $to . $this->lf . implode($this->lf, $this->headers) . $this->lf . implode($this->lf, $xtra_headers));
} elseif(EMAIL_TRANSPORT == 'sendmail') {
return mail($to, $subject, $this->output, 'From: '.$from.$this->lf.implode($this->lf, $this->headers).$this->lf.implode($this->lf, $xtra_headers));
}elseif(EMAIL_TRANSPORT == 'other'){
$fp = fsockopen($mailconfig['server'], $mailconfig['port'], &$errno, &$errstr, 30);
if($mailconfig['auth']) {
$from = $mailconfig['from'];
fputs($fp, "EHLO SOOBIC! \r\n");
fputs($fp, "AUTH LOGIN \r\n");
fputs($fp, base64_encode($mailconfig['auth_username'])." \r\n");
fputs($fp, base64_encode($mailconfig['auth_password'])." \r\n");
} else {
fputs($fp, "HELO SOOBIC! \r\n");
}
fputs($fp, "MAIL FROM: $from\r\n");
foreach(explode(',', $to) as $touser) {
$touser = trim($touser);
if($touser) {
fputs($fp, "RCPT TO: $touser\r\n");
}
}
fputs($fp, "DATA\r\n");
$tosend = "From: $mailconfig[from]\r\n";
$tosend .= "To: FreeSOOBIC! Users <me@localhost>\r\n"; //fix:还是服务器本地身份稳妥
$tosend .= 'Subject: '.str_replace("\n", ' ', $subject)."\r\n\r\n".$this->output."\r\n.\r\n"; fputs($fp, $tosend);
fputs($fp, "QUIT\r\n");
fclose($fp);
}
}
function get_rfc822($to_name, $to_addr, $from_name, $from_addr, $subject = '', $headers = '') {
$date = 'Date: ' . date('D, d M y H:i:s');
$to = (($to_name != '') ? 'To: "' . $to_name . '" <' . $to_addr . '>' : 'To: ' . $to_addr);
$from = (($from_name != '') ? 'From: "' . $from_name . '" <' . $from_addr . '>' : 'From: ' . $from_addr);
if (is_string($subject)) {
$subject = 'Subject: ' . $subject;
}
if (is_string($headers)) {
$headers = explode($this->lf, trim($headers));
}
for ($i=0; $i<count($headers); $i++) {
if (is_array($headers[$i])) {
for ($j=0; $j<count($headers[$i]); $j++) {
if ($headers[$i][$j] != '') {
$xtra_headers[] = $headers[$i][$j];
}
}
}
if ($headers[$i] != '') {
$xtra_headers[] = $headers[$i];
}
}
if (!isset($xtra_headers)) {
$xtra_headers = array();
}
$headers = array_merge($this->headers, $xtra_headers);
return $date . $this->lf . $from . $this->lf . $to . $this->lf . $subject . $this->lf . implode($this->lf, $headers) . $this->lf . $this->lf . $this->output;
}
}
?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -