PHP 使用 fsockopen 函数 SMTP 发信
< 返回列表时间: 2020-06-10来源:OSCHINA
<?php /** * PHP 使用 fsockopen 函数 SMTP 实现发信 * * @version 1.0 * @date 2017/01/29 22:31 */ function smtp_mailer($mailto, $subject = 'No Subject', $body = 'No Body') { $smtp_host = 'smtp.lanseyujie.com'; //SMTP服务器地址 $smtp_port = 465; //SMTP服务器端口 $smtp_openssl = 1; //是否使用SSL加密通信 $smtp_debug = 1; //是否debug输出运行信息 $smtp_username = 'service@lanseyujie.com'; //用户账号 $smtp_password = '1234567890abcdefghij'; //用户密码 $smtp_from = 'service@lanseyujie.com'; //发件邮箱 $smtp = array( //1.EHLO 向服务器标识用户身份发送者 收到 220 或 250 时OK array("EHLO ". $smtp_host ."\r\n", "220,250", "EHLO: "), //2.AUTH LOGIN 请求登录认证 334 OK array("AUTH LOGIN\r\n", "334", "AUTH LOGIN: "), //3.发送经Base64加密的用户账号 334 OK array(base64_encode($smtp_username) ."\r\n", "334", "Send Base64 Encode username: "), //4.发送经Base64加密的用户密码 235 OK array(base64_encode($smtp_password) ."\r\n", "235", "Send Base64 Encode password: "), //5.Mail From 发送发件人邮箱 250 OK array("MAIL FROM: <". $smtp_from .">\r\n", "250", "MAIL FROM: "), //6.发送收件人邮箱 250 OK array("RCPT TO: <". $mailto .">\r\n", "250", "RCPT TO: "), //7.开始发送邮件数据 354 OK array("DATA\r\n", "354", "DATA Start: "), //8.0 发件人 array("From: ". $smtp_from ."\r\n", "", ""), //8.1 收件人 array("To: ". $mailto ."\r\n", "", ""), //8.2 发件代理客户端 array("X-Mailer: LANSEYUJIE WebMailer 1.0\r\n", "", ""), //8.3 邮件重要级别 1(Highest) 3(Normal) 5(Lowest) array("X-Priority: 1 (Highest)\r\n", "", ""), //8.4 标题 array("Subject: ". $subject ."\r\n", "", ""), //8.5 编码 array("Content-Type: text/html; charset=\"utf-8\"\r\n", "", ""), //8.6 内容加密方式 array("Content-Transfer-Encoding: base64\r\n\r\n", "", ""), //8.7 Base64 Encode Body array(base64_encode($body) ."\r\n", "", ""), //9.结束发送邮件数据 250 array("\r\n.\r\n", "250", "DATA End: "), //10.结束会话 array("QUIT\r\n", "221", "QUIT: ") ); $info = ''; //建立socket通信 if (1 == $smtp_openssl) { //SSL加密通信 $fp = @fsockopen('ssl://'. $smtp_host, $smtp_port); } else { //普通无加密通信 $fp = @fsockopen($smtp_host, $smtp_port); } if (!$fp) { $info .= "FSOCKOPEN Error: Cannot conect to ". $smtp_host ."\r\n<br />"; } //发送smtp数组中的命令/数据 foreach ($smtp as $request) { //发送请求信息 @fputs($fp, $request[0]); //接收响应信息 if (1 == $smtp_debug && '' != $request[1]) { //接收信息 $response = @fgets($fp, 128); $info .= $request[2] . $response ."\r\n<br />"; } } //关闭连接 @fclose($fp); return $info; } // 测试 echo smtp_mailer('test@lanseyujie.com', 'Mailer Test', '<i style="color: red;font-size: 16px;">This is a test mail to verify the Mailer run or not!</i>');
热门排行