📄 class.phpmailer.php
字号:
else $buf = $part; } } else { $buf_o = $buf; if ($e == 0) $buf .= $word; else $buf .= " " . $word; if (strlen($buf) > $length and $buf_o != "") { $message .= $buf_o . $soft_break; $buf = $word; } } } $message .= $buf . $this->LE; } return ($message); } /** * Set the body wrapping. * @access private * @return void */ function SetWordWrap() { if($this->WordWrap < 1) return; switch($this->message_type) { case "alt": case "alt_attachment": $this->AltBody = $this->word_wrap($this->AltBody, $this->WordWrap); break; default: $this->Body = $this->word_wrap($this->Body, $this->WordWrap); break; } } /** * Assembles message header. Returns a string if successful * or false if unsuccessful. * @access private * @return string */ function create_header() { $header = array(); // Set the boundaries $uniq_id = md5(uniqid(time())); $this->boundary[1] = "b1_" . $uniq_id; $this->boundary[2] = "b2_" . $uniq_id; // To be created automatically by mail() if($this->Mailer != "mail") { if(count($this->to) > 0) $header[] = $this->addr_append("To", $this->to); else if (count($this->cc) == 0) $header[] = "To: undisclosed-recipients:;".$this->LE; } $from = array(); $from[0][0] = trim($this->From); $from[0][1] = $this->FromName; $header[] = $this->addr_append("From", $from); if(count($this->cc) > 0) $header[] = $this->addr_append("Cc", $this->cc); // sendmail and mail() extract Bcc from the header before sending if((($this->Mailer == "sendmail") || ($this->Mailer == "mail")) && (count($this->bcc) > 0)) $header[] = $this->addr_append("Bcc", $this->bcc); if(count($this->ReplyTo) > 0) $header[] = $this->addr_append("Reply-to", $this->ReplyTo); // mail() sets the subject itself if($this->Mailer != "mail") $header[] = sprintf("Subject: %s%s", $this->encode_header(trim($this->Subject)), $this->LE); $header[] = sprintf("Message-ID: <%s@%s>%s", $uniq_id, $this->get_server_hostname(), $this->LE); $header[] = sprintf("X-Priority: %d%s", $this->Priority, $this->LE); $header[] = sprintf("X-Mailer: phpmailer [version %s]%s", $this->Version, $this->LE); if($this->Sender == "") $header[] = sprintf("Return-Path: %s%s", trim($this->From), $this->LE); else $header[] = sprintf("Return-Path: %s%s", trim($this->Sender), $this->LE); if($this->ConfirmReadingTo != "") $header[] = sprintf("Disposition-Notification-To: <%s>%s", trim($this->ConfirmReadingTo), $this->LE); // Add custom headers for($index = 0; $index < count($this->CustomHeader); $index++) $header[] = sprintf("%s: %s%s", trim($this->CustomHeader[$index][0]), $this->encode_header(trim($this->CustomHeader[$index][1])), $this->LE); if($this->UseMSMailHeaders) $header[] = $this->AddMSMailHeaders(); $header[] = sprintf("MIME-Version: 1.0%s", $this->LE); // Determine what type of message this is if(count($this->attachment) < 1 && strlen($this->AltBody) < 1) $this->message_type = "plain"; else { if(count($this->attachment) > 0) $this->message_type = "attachments"; if(strlen($this->AltBody) > 0 && count($this->attachment) < 1) $this->message_type = "alt"; if(strlen($this->AltBody) > 0 && count($this->attachment) > 0) $this->message_type = "alt_attachments"; } switch($this->message_type) { case "plain": $header[] = sprintf("Content-Transfer-Encoding: %s%s", $this->Encoding, $this->LE); $header[] = sprintf("Content-Type: %s; charset=\"%s\"", $this->ContentType, $this->CharSet); break; case "attachments": case "alt_attachments": if($this->EmbeddedImageCount() > 0) { $header[] = sprintf("Content-Type: %s;%s\ttype=\"text/html\";%s\tboundary=\"%s\"%s", "multipart/related", $this->LE, $this->LE, $this->boundary[1], $this->LE); } else { $header[] = sprintf("Content-Type: %s;%s", "multipart/mixed", $this->LE); $header[] = sprintf("\tboundary=\"%s\"%s", $this->boundary[1], $this->LE); } break; case "alt": $header[] = sprintf("Content-Type: %s;%s", "multipart/alternative", $this->LE); $header[] = sprintf("\tboundary=\"%s\"%s", $this->boundary[1], $this->LE); break; } // No additional lines when using mail() function if($this->Mailer != "mail") $header[] = $this->LE.$this->LE; return(join("", $header)); } /** * Assembles the message body. Returns a string if successful * or false if unsuccessful. * @access private * @return string */ function create_body() { $body = array(); $this->SetWordWrap(); switch($this->message_type) { case "alt": // Return text of body $bndry = new Boundary($this->boundary[1]); $bndry->CharSet = $this->CharSet; $bndry->Encoding = $this->Encoding; $body[] = $bndry->GetSource(); $body[] = $this->encode_string($this->AltBody, $this->Encoding); $body[] = $this->LE.$this->LE; $bndry = new Boundary($this->boundary[1]); $bndry->CharSet = $this->CharSet; $bndry->ContentType = "text/html"; $bndry->Encoding = $this->Encoding; $body[] = $bndry->GetSource(); $body[] = $this->encode_string($this->Body, $this->Encoding); $body[] = $this->LE.$this->LE; // End the boundary $body[] = sprintf("%s--%s--%s", $this->LE, $this->boundary[1], $this->LE.$this->LE); break; case "plain": $body[] = $this->encode_string($this->Body, $this->Encoding); break; case "attachments": $bndry = new Boundary($this->boundary[1]); $bndry->CharSet = $this->CharSet; $bndry->ContentType = $this->ContentType; $bndry->Encoding = $this->Encoding; $body[] = $bndry->GetSource(false) . $this->LE; $body[] = $this->encode_string($this->Body, $this->Encoding); $body[] = $this->LE; if(!$body[] = $this->attach_all()) return false; break; case "alt_attachments": $body[] = sprintf("--%s%s", $this->boundary[1], $this->LE); $body[] = sprintf("Content-Type: %s;%s" . "\tboundary=\"%s\"%s", "multipart/alternative", $this->LE, $this->boundary[2], $this->LE.$this->LE); // Create text body $bndry = new Boundary($this->boundary[2]); $bndry->CharSet = $this->CharSet; $bndry->ContentType = "text/plain"; $bndry->Encoding = $this->Encoding; $body[] = $bndry->GetSource() . $this->LE; $body[] = $this->encode_string($this->AltBody, $this->Encoding); $body[] = $this->LE.$this->LE; // Create the HTML body $bndry = new Boundary($this->boundary[2]); $bndry->CharSet = $this->CharSet; $bndry->ContentType = "text/html"; $bndry->Encoding = $this->Encoding; $body[] = $bndry->GetSource() . $this->LE; $body[] = $this->encode_string($this->Body, $this->Encoding); $body[] = $this->LE.$this->LE; $body[] = sprintf("%s--%s--%s", $this->LE, $this->boundary[2], $this->LE.$this->LE); if(!$body[] = $this->attach_all()) return false; break; } $sBody = join("", $body); return $sBody; } ///////////////////////////////////////////////// // ATTACHMENT METHODS ///////////////////////////////////////////////// /** * Adds an attachment from a path on the filesystem. * Checks if attachment is valid and then adds * the attachment to the list. * Returns false if the file could not be found * or accessed. * @access public * @return bool */ function AddAttachment($path, $name = "", $encoding = "base64", $type = "application/octet-stream") { if(!@is_file($path)) { $this->error_handler(sprintf("Could not access [%s] file", $path)); return false; } $filename = basename($path); if($name == "") $name = $filename; // Append to $attachment array $cur = count($this->attachment); $this->attachment[$cur][0] = $path; $this->attachment[$cur][1] = $filename; $this->attachment[$cur][2] = $name; $this->attachment[$cur][3] = $encoding; $this->attachment[$cur][4] = $type; $this->attachment[$cur][5] = false; // isStringAttachment $this->attachment[$cur][6] = "attachment"; $this->attachment[$cur][7] = 0; return true; } /** * Attaches all fs, string, and binary attachments to the message. * Returns a string if successful or false if unsuccessful. * @access private * @return string */ function attach_all() { // Return text of body $mime = array(); // Add all attachments for($i = 0; $i < count($this->attachment); $i++) { // Check for string attachment $bString = $this->attachment[$i][5]; if ($bString) { $string = $this->attachment[$i][0]; } else { $path = $this->attachment[$i][0]; } $filename = $this->attachment[$i][1]; $name = $this->attachment[$i][2]; $encoding = $this->attachment[$i][3]; $type = $this->attachment[$i][4]; $disposition = $this->attachment[$i][6]; $cid = $this->attachment[$i][7]; $mime[] = sprintf("--%s%s", $this->boundary[1], $this->LE); $mime[] = sprintf("Content-Type: %s; name=\"%s\"%s", $type, $name, $this->LE); $mime[] = sprintf("Content-Transfer-Encoding: %s%s", $encoding, $this->LE); if($disposition == "inline") $mime[] = sprintf("Content-ID: <%s>%s", $cid, $this->LE); $mime[] = sprintf("Content-Disposition: %s; filename=\"%s\"%s", $disposition, $name, $this->LE.$this->LE); // Encode as string attachment if($bString) { if(!$mime[] = $this->encode_string($string, $encoding)) return false; $mime[] = $this->LE.$this->LE; } else { if(!$mime[] = $this->encode_file($path, $encoding)) return false; $mime[] = $this->LE.$this->LE; } } $mime[] = sprintf("--%s--%s", $this->boundary[1], $this->LE); return(join("", $mime)); } /** * Encodes attachment in requested format. Returns a * string if successful or false if unsuccessful. * @access private * @return string */ function encode_file ($path, $encoding = "base64") { if(!@$fd = fopen($path, "rb")) { $this->error_handler(sprintf("File Error: Could not open file %s", $path)); return false; } $file_buffer = fread($fd, filesize($path)); $file_buffer = $this->encode_string($file_buffer, $encoding); fclose($fd); return $file_buffer; } /** * Encodes string to requested format. Returns a * string if successful or false if unsuccessful. * @access private * @return string */ function encode_string ($str, $encoding = "base64") { switch(strtolower($encoding)) { case "base64": // chunk_split is found in PHP >= 3.0.6 $encoded = chunk_split(base64_encode($str)); break; case "7bit": case "8bit": $encoded = $this->fix_eol($str); if (substr($encoded, -2) != $this->LE) $encoded .= $this->LE; break; case "binary": $encoded = $str; break; case "quoted-printable": $encoded = $this->encode_qp($str); break; default: $this->error_handler(sprintf("Unknown encoding: %s", $encoding)); return false; } return($encoded); } /** * Encode a header string to best of Q, B, quoted or none. Returns a string. * @access private * @return string */ function encode_header ($str, $position = 'text') { $x = 0; switch (strtolower($position)) { case 'phrase': if (preg_match_all('/[\200-\377]/', $str, $matches) == 0) { // Can't use addslashes as we don't know what value has magic_quotes_sybase. $encoded = addcslashes($str, '\000-\037\177'); $encoded = preg_replace('/([\"])/', '\\"', $encoded); if (($str == $encoded) && (preg_match_all('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str, $matches) == 0)) return ($encoded); else return ("\"$encoded\""); } $x = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches); break; case 'comment': $x = preg_match_all('/[()"]/', $str, $matches); // Fall-through case 'text': default: $x += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches); break; } if ($x == 0) return ($str); $maxlen = 75 - 7 - strlen($this->CharSet); // Try to select the encoding which should produce the shortest output// if (strlen($str)/3 < $x) { $encoding = 'B'; $encoded = base64_encode($str); $maxlen -= $maxlen % 4; $encoded = trim(chunk_split($encoded, $maxlen, "\n"));// } else {// $encoding = 'Q';// $encoded = $this->encode_q($str, $position);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -