热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

phpfsockopen邮箱发送实例代码

?phpok的邮箱发送。includequot;smtp.class.phpquot;;$smtpserverquot;smtp...
debug = true; //是否开启调试,只在测试程序时使用,正式使用时请将此行注释
$to = "你要发给的那个人的邮箱地址"; //收件人
$subject = "你好";
$body = "你发送的内容 ";
$send = $smtp->sendmail($to, $sender, $subject, $body, $mailtype);
if ($send == 1) {
    echo "邮件发送成功";
} else {
    echo "邮件发送失败
"; //echo "原因:".$this->smtp->logs; } ?>

邮箱发送类smtp.class.php

debug = false;
        $this->smtp_port = $smtp_port;
        $this->relay_host = $relay_host;
        $this->time_out = 30; //is used in fsockopen()
        //
        $this->auth = $auth; //auth
        $this->user = $user;
        $this->pass = $pass;
        //
        $this->host_name = "localhost"; //is used in helo command
        $this->log_file = "";
        $this->sock = false;
    }
    /* main function */
    function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "") {
        $mail_from = $this->get_address($this->strip_comment($from));
        $body = ereg_replace("(^|(rn))(.)", "1.3", $body);
        $header.= "mime-version:1.0r\n";
        if ($mailtype == "html") {
            $header.= "content-type:text/htmlr\n";
        }
        $header.= "to: " . $to . "r\n";
        if ($cc != "") {
            $header.= "cc: " . $cc . "r\n";
        }
        $header.= "from: $from<" . $from . ">;r\n";
        $header.= "subject: " . $subject . "r\n";
        $header.= $additional_headers;
        $header.= "date: " . date("r") . "r\n";
        $header.= "x-mailer:by redhat (php/" . phpversion() . ")r\n";
        list($msec, $sec) = explode(" ", microtime());
        $header.= "message-id: <" . date("ymdhis", $sec) . "." . ($msec * 1000000) . "." . $mail_from . ">;r\n";
        $to = explode(",", $this->strip_comment($to));
        if ($cc != "") {
            $to = array_merge($to, explode(",", $this->strip_comment($cc)));
        }
        if ($bcc != "") {
            $to = array_merge($to, explode(",", $this->strip_comment($bcc)));
        }
        $sent = true;
        foreach ($to as $rcpt_to) {
            $rcpt_to = $this->get_address($rcpt_to);
            if (!$this->smtp_sockopen($rcpt_to)) {
                $this->log_write("error: cannot send email to " . $rcpt_to . "\n");
                $sent = false;
                continue;
            }
            if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
                $this->log_write("e-mail has been sent to <" . $rcpt_to . ">;\n");
            } else {
                $this->log_write("error: cannot send email to <" . $rcpt_to . ">;\n");
                $sent = false;
            }
            fclose($this->sock);
            $this->log_write("disconnected from remote host\n");
        }
        return $sent;
    }
    /* private functions */
    function smtp_send($helo, $from, $to, $header, $body = "") {
        if (!$this->smtp_putcmd("helo", $helo)) {
            return $this->smtp_error("sending helo command");
        }
        //auth
        if ($this->auth) {
            if (!$this->smtp_putcmd("auth logi\n", base64_encode($this->user))) {
                return $this->smtp_error("sending helo command");
            }
            if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
                return $this->smtp_error("sending helo command");
            }
        }
        //
        if (!$this->smtp_putcmd("mail", "from:<" . $from . ">;")) {
            return $this->smtp_error("sending mail from command");
        }
        if (!$this->smtp_putcmd("rcpt", "to:<" . $to . ">;")) {
            return $this->smtp_error("sending rcpt to command");
        }
        if (!$this->smtp_putcmd("data")) {
            return $this->smtp_error("sending data command");
        }
        if (!$this->smtp_message($header, $body)) {
            return $this->smtp_error("sending message");
        }
        if (!$this->smtp_eom()) {
            return $this->smtp_error("sending ;;.;; [eom]");
        }
        if (!$this->smtp_putcmd("quit")) {
            return $this->smtp_error("sending quit command");
        }
        return true;
    }
    function smtp_sockopen($address) {
        if ($this->relay_host == "") {
            return $this->smtp_sockopen_mx($address);
        } else {
            return $this->smtp_sockopen_relay();
        }
    }
    function smtp_sockopen_relay() {
        $this->log_write("trying to " . $this->relay_host . ":" . $this->smtp_port . "\n");
        $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
        if (!($this->sock && $this->smtp_ok())) {
            $this->log_write("error: cannot connenct to relay host " . $this->relay_host . "\n");
            $this->log_write("error: " . $errstr . " (" . $errno . ")\n");
            return false;
        }
        $this->log_write("connected to relay host " . $this->relay_host . "\n");
        return true;
    }
    function smtp_sockopen_mx($address) {
        $domain = ereg_replace("^.+@([^@]+)$", "1", $address);
        if (!@getmxrr($domain, $mxhosts)) {
            $this->log_write("error: cannot resolve mx "".$domain.""\n");
            return false;
        }
        foreach ($mxhosts as $host) {
            $this->log_write("trying to " . $host . ":" . $this->smtp_port . "\n");
            $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
            if (!($this->sock && $this->smtp_ok())) {
                $this->log_write("warning: cannot connect to mx host " . $host . "\n");
                $this->log_write("error: " . $errstr . " (" . $errno . ")\n");
                continue;
            }
            $this->log_write("connected to mx host " . $host . "\n");
            return true;
        }
        $this->log_write("error: cannot connect to any mx hosts (" . implode(", ", $mxhosts) . ")\n");
        return false;
    }
    function smtp_message($header, $body) {
        fputs($this->sock, $header . "r\n" . $body);
        $this->smtp_debug(">; " . str_replace("r\n", "\n" . ">; ", $header . "n>; " . $body . "n>; "));
        return true;
    }
    function smtp_eom() {
        fputs($this->sock, "rn.r\n");
        $this->smtp_debug(". [eom]\n");
        return $this->smtp_ok();
    }
    function smtp_ok() {
        $respOnse= str_replace("r\n", "", fgets($this->sock, 512));
        $this->smtp_debug($response . "\n");
        if (!ereg("^[23]", $response)) {
            fputs($this->sock, "quitr\n");
            fgets($this->sock, 512);
            $this->log_write("error: remote host returned "".$response.""\n");
            return false;
        }
        return true;
    }
    function smtp_putcmd($cmd, $arg = "") {
        if ($arg != "") {
            if ($cmd == "") $cmd = $arg;
            else $cmd = $cmd . " " . $arg;
        }
        fputs($this->sock, $cmd . "r\n");
        $this->smtp_debug(">; " . $cmd . "\n");
        return $this->smtp_ok();
    }
    function smtp_error($string) {
        $this->log_write("error: error occurred while " . $string . ".\n");
        return false;
    }
    function log_write($message) {
        $this->smtp_debug($message);
        if ($this->log_file == "") {
            return true;
        }
        $message = date("m d h:i:s ") . get_current_user() . "[" . getmypid() . "]: " . $message;
        if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
            $this->smtp_debug("warning: cannot open log file \"".$this->log_file."\"\n");
            return false;
        }
        flock($fp, lock_ex);
        fputs($fp, $message);
        fclose($fp);
        return true;
    }
    function strip_comment($address) {
        $comment = "([^()]*)";
        while (ereg($comment, $address)) {
            $address = ereg_replace($comment, "", $address);
        }
        return $address;
    }
    function get_address($address) {
        $address = ereg_replace("([ trn])+", "", $address);
        $address = ereg_replace("^.*<(.+)>;.*$", "1", $address);
        return $address;
    }
    function smtp_debug($message) {
        if ($this->debug) {
            echo $message;
        }
    }
}
function sendmail($smtpserver, $smtpuser, $smtppass, $smtpemailto, $smtpusermail, $mailsubject, $mailbody) {
    $smtp = new smtp($smtpserver, 25, true, $smtpuser, $smtppass);
    //$smtp->debug = true;
    $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, "html");
}
//such as
//sendmail("smtp.126.com","test@126.com","password","1034555083@qq.com","test@126.com","title","body");
?>

推荐阅读
  • 如何实现织梦DedeCms全站伪静态
    本文介绍了如何通过修改织梦DedeCms源代码来实现全站伪静态,以提高管理和SEO效果。全站伪静态可以避免重复URL的问题,同时通过使用mod_rewrite伪静态模块和.htaccess正则表达式,可以更好地适应搜索引擎的需求。文章还提到了一些相关的技术和工具,如Ubuntu、qt编程、tomcat端口、爬虫、php request根目录等。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 本文内容为asp.net微信公众平台开发的目录汇总,包括数据库设计、多层架构框架搭建和入口实现、微信消息封装及反射赋值、关注事件、用户记录、回复文本消息、图文消息、服务搭建(接入)、自定义菜单等。同时提供了示例代码和相关的后台管理功能。内容涵盖了多个方面,适合综合运用。 ... [详细]
  • Monkey《大话移动——Android与iOS应用测试指南》的预购信息发布啦!
    Monkey《大话移动——Android与iOS应用测试指南》的预购信息已经发布,可以在京东和当当网进行预购。感谢几位大牛给出的书评,并呼吁大家的支持。明天京东的链接也将发布。 ... [详细]
  • 本文介绍了Python高级网络编程及TCP/IP协议簇的OSI七层模型。首先简单介绍了七层模型的各层及其封装解封装过程。然后讨论了程序开发中涉及到的网络通信内容,主要包括TCP协议、UDP协议和IPV4协议。最后还介绍了socket编程、聊天socket实现、远程执行命令、上传文件、socketserver及其源码分析等相关内容。 ... [详细]
  • 基于layUI的图片上传前预览功能的2种实现方式
    本文介绍了基于layUI的图片上传前预览功能的两种实现方式:一种是使用blob+FileReader,另一种是使用layUI自带的参数。通过选择文件后点击文件名,在页面中间弹窗内预览图片。其中,layUI自带的参数实现了图片预览功能。该功能依赖于layUI的上传模块,并使用了blob和FileReader来读取本地文件并获取图像的base64编码。点击文件名时会执行See()函数。摘要长度为169字。 ... [详细]
  • 搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的详细步骤
    本文详细介绍了搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的步骤,包括环境说明、相关软件下载的地址以及所需的插件下载地址。 ... [详细]
  • 这是原文链接:sendingformdata许多情况下,我们使用表单发送数据到服务器。服务器处理数据并返回响应给用户。这看起来很简单,但是 ... [详细]
  • 本文介绍了使用AJAX的POST请求实现数据修改功能的方法。通过ajax-post技术,可以实现在输入某个id后,通过ajax技术调用post.jsp修改具有该id记录的姓名的值。文章还提到了AJAX的概念和作用,以及使用async参数和open()方法的注意事项。同时强调了不推荐使用async=false的情况,并解释了JavaScript等待服务器响应的机制。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • PHP设置MySQL字符集的方法及使用mysqli_set_charset函数
    本文介绍了PHP设置MySQL字符集的方法,详细介绍了使用mysqli_set_charset函数来规定与数据库服务器进行数据传送时要使用的字符集。通过示例代码演示了如何设置默认客户端字符集。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
author-avatar
游山玩水人生
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有