php socket通过smtp发送邮件(可带附件)
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>

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

本篇文章给大家带来了关于php+socket的相关知识,其中主要介绍了IO多路复用,以及php+socket如何实现web服务器?感兴趣的朋友下面一起来看一下,希望对大家有帮助。

SpringBoot端第一步,引入依赖首先我们需要引入WebSocket所需的依赖,以及处理输出格式的依赖com.alibabafastjson1.2.73org.springframework.bootspring-boot-starter-websocket第二步,创建WebSocket配置类importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Config

一、基于TCP协议的socket套接字编程1、套接字工作流程先从服务器端说起。服务器端先初始化Socket,然后与端口绑定(bind),对端口进行监听(listen),调用accept阻塞,等待客户端连接。在这时如果有个客户端初始化一个Socket,然后连接服务器(connect),如果连接成功,这时客户端与服务器端的连接就建立了。客户端发送数据请求,服务器端接收请求并处理请求,然后把回应数据发送给客户端,客户端读取数据,最后关闭连接,一次交互结束,使用以下Python代码实现:importso

php socket无法连接的解决办法:1、检查php是否开启socket扩展;2、打开php.ini文件,检查“php_sockets.dll”是否被加载;3、取消“php_sockets.dll”的注释状态即可。

PHP是一种常用的开发语言,可以用于开发各种Web应用程序。除了常见的HTTP请求和响应以外,PHP也支持通过Socket进行网络通信,实现更为灵活和高效的数据交互。本文将介绍PHP如何实现Socket通信的方法与技巧,并附上具体的代码示例。什么是Socket通信Socket是一种在网络中进行通信的方法,可以在不同的计算机之间传输数据。通过S

C#中常见的网络通信和安全性问题及解决方法在当今互联网时代,网络通信已经成为了软件开发中必不可少的一部分。在C#中,我们通常会遇到一些网络通信的问题,例如数据传输的安全性、网络连接的稳定性等。本文将针对C#中常见的网络通信和安全性问题进行详细讨论,并提供相应的解决方法和代码示例。一、网络通信问题网络连接中断:网络通信过程中,可能会出现网络连接的中断,这会导致

本篇文章给大家带来了关于php+socket的相关知识,其中主要介绍了什么是socket?php+socket如何实现客户端与服务端数据传输?感兴趣的朋友下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于php+socket的相关知识,其中主要介绍了怎么使用php原生socket实现一个简易的web聊天室?感兴趣的朋友下面一起来看一下,希望对大家有帮助。
