php socket通过smtp发送邮件(可带附件)
//define("SOL", "\n"); define("EOL", "\r\n"); define("SMTP_HOST", "smtp.163.com");//SMTP服务器 define("SMTP_PORT", "25");//SMTP服务器端口 define("SMTP_USER", "");//SMTP服务器的用户帐号 define("SMTP_PASS", "");//SMTP服务器的用户密码 $from = "";//SMTP服务器的用户邮箱 $to = "";//发送给谁 可用逗号隔开多个邮箱 $cc = ""; $bcc = ""; $subject="这是一个由PHP发送的带附件的邮件";//邮件主题 很多客户端会有乱码,所以转一下码 $body = "这个是一个带附件的邮件发送程序<hr>看到没,这里显示了HTM标签哦;<a href="'http://www.google.com/'">请点开链接</a><input type="'button'" id="'aaa'" name='\"aaa\"'> ".date('Y-m-d H:i:s');//邮件内容 $smtp = new smtp(SMTP_HOST,SMTP_PORT,SMTP_USER,SMTP_PASS, true);//这里面的一个true是表示使用身份验证,否则不使用身份验证. $smtp->addAttachment("mail.zip"); $smtp->sendmail($to, $from, $subject, $body, $cc, $bcc); class smtp { /* Public Variables */ public $attachments = array(); /* Private Variables */ private $smtp_host; private $smtp_port; private $time_out; private $host_name; private $auth; private $user; private $pass; private $sock; /* Constractor */ public function smtp($smtp_host = null, $smtp_port = null, $user = null, $pass = null, $auth = true) { $this->smtp_host = (!empty($smtp_host)) ? $smtp_host : SMTP_HOST; $this->smtp_port = (!empty($smtp_port)) ? $smtp_port : SMTP_PORT; $this->user = (!empty($user)) ? $user : SMTP_PORT; $this->pass = (!empty($pass)) ? $pass : SMTP_PORT; $this->auth = $auth; $this->time_out = 15; # $this->host_name = "localhost"; $this->sock = FALSE; } /* Main Function */ public function sendmail($to, $from, $subject = "", $body = "", $cc = "", $bcc = "") { $bndp = md5(uniqid("")) . rand(1000, 9999); $bnd = md5(uniqid("")) . rand(1000, 9999); list ($msec, $sec) = explode(" ", microtime()); $mail_from = $this->strip_line_breaks($from); $mail_to = explode(",", $to); $body = preg_replace("/(^|(\r\n))(\\.)/", "", $body); if ($cc != "") $mail_to = array_merge($mail_to, explode(",", $cc)); if ($bcc != "") $mail_to = array_merge($mail_to, explode(",", $bcc)); $headers = "MIME-Version:1.0" . EOL; $headers .= "To: " . $to . EOL; if ($cc != "") { $headers .= "Cc: " . $cc . EOL; } $headers .= "From: $from" . EOL; $headers .= "Subject: " . $subject . EOL; $headers .= "Date: " . date("r") . EOL; $headers .= "X-Mailer: Webmail ver 1.0 (PHP Version/" . phpversion() . ")" . EOL; $headers .= "Message-ID: " . EOL; if (count($this->attachments) > 0) { $headers .= "Content-Type: multipart/mixed;" . EOL . chr(9) . " boundary=\"" . $bndp . "\"" . EOL . EOL; $headers .= '--'.$bndp . EOL; $headers .= 'Content-Type : multipart/alternative; boundary="' . $bnd . '"' . EOL . EOL; $headers .= '--' . $bnd . EOL; $headers .= 'Content-Type: text/plain; charset=utf-8' . EOL; $headers .= "Content-Transfer-Encoding: 8bit" . EOL . EOL; $headers .= $body . EOL; $headers .= '--' . $bnd . EOL; $headers .= 'Content-type: text/html; charset=utf-8' . EOL; $headers .= "Content-Transfer-Encoding: 8bit" . EOL . EOL; $headers .= $body . EOL; $headers .= '--' . $bnd . '--' . EOL; foreach ($this->attachments as $att) { $headers .= "--" . $bndp . EOL . $att; } $headers .= '--' . $bndp . '--' . EOL; $this->clear_attachments(); } else { $headers .= 'Content-Type : multipart/alternative;boundary="'.$bnd.'"' . EOL . EOL; $headers .= '--'.$bnd . EOL; $headers .= 'Content-Type: text/plain; charset=utf-8' . EOL; $headers .= "Content-Transfer-Encoding: 8bit" . EOL . EOL; $headers .= $body . EOL; $headers .= '--'.$bnd . EOL; $headers .= 'Content-type: text/html; charset=utf-8' . EOL; $headers .= "Content-Transfer-Encoding: 8bit" . EOL . EOL; $headers .= $body . EOL; $headers .= '--'.$bnd.'--' . EOL; } $sent = TRUE; foreach ($mail_to as $rcpt_to) { $rcpt_to = $this->strip_line_breaks($rcpt_to); if (!$this->smtp_sockopen($rcpt_to)) { $this->log_write("Error: Cannot send email to " . $rcpt_to); $sent = FALSE; continue; } if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $headers, $body)) { $this->log_write("E-mail has been sent to "); } else { $this->log_write("Error: Cannot send email to "); $sent = FALSE; } fclose($this->sock); } $this->log_write("{$mail_to} send over;"); return $sent; } public function addAttachment($file, $dispo = "attachment") { $file_data = (file_exists($file)) ? file_get_contents($file) : ""; if ($file_data != "") { $filename = basename($file); $ext = pathinfo($filename, PATHINFO_EXTENSION); $chunks = chunk_split(base64_encode($file_data)); $parts = "Content-Type: application/$ext; name=\"" . $filename . "\"" . EOL; $parts .= "Content-Transfer-Encoding: base64" . EOL; $parts .= "Content-Disposition: " . $dispo . "; filename=\"" . $filename . "\"" . EOL . EOL; $parts .= $chunks . EOL . EOL; $this->attachments[] = $parts; } } private function clear_attachments() { unset($this->attachments); $this->attachments = array(); } /* Private Functions */ private function smtp_send($helo, $from, $to, $header, $body = "") { if (!$this->smtp_putcmd("HELO", $helo)) { //$this->log_write("Error: Error occurred while sending HELO command."); return FALSE; } #auth if ($this->auth) { if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) { //$this->log_write("Error: Error occurred while sending HELO command."); return FALSE; } if (!$this->smtp_putcmd("", base64_encode($this->pass))) { //$this->log_write("Error: Error occurred while sending HELO command."); return FALSE; } } if (!$this->smtp_putcmd("MAIL", "FROM:")) { //$this->log_write("Error: Error occurred while sending MAIL FROM command."); return FALSE; } if (!$this->smtp_putcmd("RCPT", "TO:")) { //$this->log_write("Error: Error occurred while sending RCPT TO command."); return FALSE; } if (!$this->smtp_putcmd("DATA")) { //$this->log_write("Error: Error occurred while sending DATA command."); return FALSE; } if (!$this->smtp_message($header, $body)) { //$this->log_write("Error: Error occurred while sending message."); return FALSE; } if (!$this->smtp_eom()) { //$this->log_write("Error: Error occurred while sending <cr><lf>.<cr><lf> [EOM]."); return FALSE; } if (!$this->smtp_putcmd("QUIT")) { //$this->log_write("Error: Error occurred while sending QUIT command."); return FALSE; } return TRUE; } private function smtp_sockopen($address) { if ($this->smtp_host == "") { return $this->smtp_sockopen_mx($address); } else { return $this->smtp_sockopen_relay(); } } private function smtp_sockopen_relay() { $this->log_write("Trying to Connect " . $this->smtp_host . ":" . $this->smtp_port . "..."); $this->sock = @fsockopen($this->smtp_host, $this->smtp_port, $errno, $errstr, $this->time_out); if (!($this->sock && $this->smtp_ok())) { $this->log_write("Error: connenct error" . $errstr . " (" . $errno . ")"); return FALSE; } $this->log_write("Connected Ok"); return TRUE; } private function smtp_sockopen_mx($address) { $domain = preg_replace("/^.+@([^@]+)$/", "\1", $address); if (!@getmxrr($domain, $MXHOSTS)) { $this->log_write("Error: Cannot resolve MX \"" . $domain . "\""); return FALSE; } foreach ($MXHOSTS as $host) { $this->log_write("Trying to " . $host . ":" . $this->smtp_port); $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out); if (!($this->sock && $this->smtp_ok())) { $this->log_write("Connect Error ," . $errstr . " (" . $errno . ")"); continue; } $this->log_write("Connected to mx host " . $host); return TRUE; } $this->log_write("Error: Cannot connect to any mx hosts (" . implode(", ", $MXHOSTS) . ")"); return FALSE; } private function smtp_message($header, $body) { fputs($this->sock, $header . "\r\n" . $body); return TRUE; } private function smtp_eom() { fputs($this->sock, "\r\n.\r\n"); return $this->smtp_ok(); } private function smtp_ok() { $response = str_replace("\r\n", "", fgets($this->sock, 512)); if (!preg_match("/^[23]/", $response)) { fputs($this->sock, "QUIT\r\n"); fgets($this->sock, 512); $this->log_write("Error: Remote host returned \"" . $response . "\""); return FALSE; } return TRUE; } private function smtp_putcmd($cmd, $arg = "") { if ($arg != "") $cmd = ($cmd == "") ? $arg : ($cmd . " " . $arg); fputs($this->sock, $cmd . "\r\n"); return $this->smtp_ok(); } private function strip_line_breaks($address) { $address = preg_replace("/([\t\r\n])+/", "", $address); $address = preg_replace("/^.*.*$/", "", $address); return $address; } public function log_write($message) { $message = date("M d H:i:s ") . get_current_user() . "[" . getmypid() . "]: " . $message; file_put_contents(dirname(__FILE__) . '/mail.log', $message . PHP_EOL, FILE_APPEND | LOCK_EX); } }</lf></cr></lf></cr>

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

The steps to build an efficient shopping cart system using sessions include: 1) Understand the definition and function of the session. The session is a server-side storage mechanism used to maintain user status across requests; 2) Implement basic session management, such as adding products to the shopping cart; 3) Expand to advanced usage, supporting product quantity management and deletion; 4) Optimize performance and security, by persisting session data and using secure session identifiers.

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version
Recommended: Win version, supports code prompts!

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
