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

php表单发送到邮箱V1.0

html表单代码:index.

html表单代码:

<form action="index.php" name="form" method="POST">
    <ul >    
    <input name="url" type="hidden" id="url">     
    <input name="ip" type="hidden" id="ip">
    <li><p>名称:p><input type="text" name="name" id="name" />li>
    <li><p>电话:p><input type="text" name="tel" id="tel" />li>
    <li><p>留言:p><textarea class="teare"  name="des">textarea>li>
    ul>
form>

<script src="http://pv.sohu.com/cityjson?ie=utf-8">script>
<script language="Javascript">    
document.getElementById(url).value=window.location.href;
document.getElementById(ip).value=returnCitySN["cip"]+,+returnCitySN["cname"];
script>

index.php代码为:

php 
     
//接收表单提交的数据    
$name = $_POST[‘name‘];
$tel = $_POST[‘tel‘];
$des= $_POST[‘des‘];
$url = $_POST[‘url‘];
$ip = $_POST[‘ip‘];

if($name!=""&&$tel!=""){//判断name、tel是否为空

    
    require_once (‘send.php‘); 
    
    //########################################## 
    
    $smtpserver = "";//SMTP服务器 
    $smtpserverport =25;//SMTP服务器端口 
    $smtpusermail = "";//SMTP服务器的用户邮箱 
    $smtpemailto = "";//发送给谁 
    $smtpuser = "";//SMTP服务器的用户帐号 
    $smtppass = "";//SMTP服务器的用户密码 
    
    $mailsubject = "预约提醒";//邮件主题 
    
    $mailbody = "在线预约提醒
姓名:".$name."
联系方式:".$tel."
留言:".$des."
提交URL:".$url."
提交时间:".date(‘Y-m-d H:i:s‘,time())."
IP地址:".$ip." (仅供参考)";//邮件内容 $mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件 //########################################## $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass); $smtp->debug = false;//是否显示发送的调试信息 $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype); echo ""; } echo ""; ?>
send.php代码为:

    class smtp 
    { 
        /* Public Variables */ 
        public $smtp_port; 
        public $time_out; 
        public $host_name; 
        public $log_file; 
        public $relay_host; 
        public $debug; 
        public $auth; 
        public $user; 
        public $pass; 
         
        /* Private Variables */ 
        private $sock; 
     
        /* Constractor */ 
        function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass) 
        { 
            $this->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 = preg_replace("/(^|(\r\n))(\\.)/", "\\1.\\3", $body); 
            $header .= "MIME-Version:1.0\r\n"; 
            if($mailtype=="HTML"){ 
                $header .= "Content-Type:text/html;charset=gb2312\r\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"); 
            } 
            //echo "
"; //echo $header;
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 LOGIN", 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 = preg_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, "\r\n.\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 (!preg_match("/^[23]/", $response)) { fputs($this->sock, "QUIT\r\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 (preg_match($comment, $address)) { $address = preg_replace($comment, "", $address); } return $address; } function get_address($address) { $address = preg_replace("/([ \t\r\n])+/", "", $address); $address = preg_replace("/^.*<(.+)>.*$/", "\\1", $address); return $address; } function smtp_debug($message) { if ($this->debug) { echo $message."
"; } } function get_attach_type($image_tag) { // $filedata = array(); $img_file_con=fopen($image_tag,"r"); unset($image_data); while ($tem_buffer=AddSlashes(fread($img_file_con,filesize($image_tag)))) $image_data.=$tem_buffer; fclose($img_file_con); $filedata[‘context‘] = $image_data; $filedata[‘filename‘]= basename($image_tag); $extension=substr($image_tag,strrpos($image_tag,"."),strlen($image_tag)-strrpos($image_tag,".")); switch($extension){ case ".gif": $filedata[‘type‘] = "image/gif"; break; case ".gz": $filedata[‘type‘] = "application/x-gzip"; break; case ".htm": $filedata[‘type‘] = "text/html"; break; case ".html": $filedata[‘type‘] = "text/html"; break; case ".jpg": $filedata[‘type‘] = "image/jpeg"; break; case ".tar": $filedata[‘type‘] = "application/x-tar"; break; case ".txt": $filedata[‘type‘] = "text/plain"; break; case ".zip": $filedata[‘type‘] = "application/zip"; break; default: $filedata[‘type‘] = "application/octet-stream"; break; } return $filedata; } } ?>

php表单发送到邮箱V1.0


推荐阅读
  • 本题要求在一组数中反复取出两个数相加,并将结果放回数组中,最终求出最小的总加法代价。这是一个经典的哈夫曼编码问题,利用贪心算法可以有效地解决。 ... [详细]
  • 通常情况下,修改my.cnf配置文件后需要重启MySQL服务才能使新参数生效。然而,通过特定命令可以在不重启服务的情况下实现配置的即时更新。本文将详细介绍如何在线调整MySQL配置,并验证其有效性。 ... [详细]
  • Python 内存管理机制详解
    本文深入探讨了Python的内存管理机制,涵盖了垃圾回收、引用计数和内存池机制。通过具体示例和专业解释,帮助读者理解Python如何高效地管理和释放内存资源。 ... [详细]
  • Appium + Java 自动化测试中处理页面空白区域点击问题
    在进行移动应用自动化测试时,有时会遇到某些页面没有返回按钮,只能通过点击空白区域返回的情况。本文将探讨如何在Appium + Java环境中有效解决此类问题,并提供详细的解决方案。 ... [详细]
  • 利用Selenium与ChromeDriver实现豆瓣网页全屏截图
    本文介绍了一种使用Selenium和ChromeDriver结合Python代码,轻松实现对豆瓣网站进行完整页面截图的方法。该方法不仅简单易行,而且解决了新版Selenium不再支持PhantomJS的问题。 ... [详细]
  • 探讨 HDU 1536 题目,即 S-Nim 游戏的博弈策略。通过 SG 函数分析游戏胜负的关键,并介绍如何编程实现解决方案。 ... [详细]
  • 深入解析动态代理模式:23种设计模式之三
    在设计模式中,动态代理模式是应用最为广泛的一种代理模式。它允许我们在运行时动态创建代理对象,并在调用方法时进行增强处理。本文将详细介绍动态代理的实现机制及其应用场景。 ... [详细]
  • Python自动化测试入门:Selenium环境搭建
    本文详细介绍如何在Python环境中安装和配置Selenium,包括开发工具PyCharm的安装、Python环境的设置以及Selenium包的安装方法。此外,还提供了编写和运行第一个自动化测试脚本的步骤。 ... [详细]
  • 本文探讨了C++编程中理解代码执行期间复杂度的挑战,特别是编译器在程序运行时生成额外指令以确保对象构造、内存管理、类型转换及临时对象创建的安全性。 ... [详细]
  • C#设计模式学习笔记:观察者模式解析
    本文将探讨观察者模式的基本概念、应用场景及其在C#中的实现方法。通过借鉴《Head First Design Patterns》和维基百科等资源,详细介绍该模式的工作原理,并提供具体代码示例。 ... [详细]
  • 如何清除Chrome浏览器地址栏的特定历史记录
    在使用Chrome浏览器时,你可能会发现地址栏保存了大量浏览记录。有时你可能希望删除某些特定的历史记录而不影响其他数据。本文将详细介绍如何单独删除地址栏中的特定记录以及批量清除所有历史记录的方法。 ... [详细]
  • 本文详细介绍如何在 iOS 7 环境下申请苹果开发者账号,涵盖从访问开发者网站到最终激活账号的完整流程。包括选择个人或企业账号类型、付款方式及注意事项等。 ... [详细]
  • 本篇文章介绍如何将两个分别表示整数的链表进行相加,并生成一个新的链表。每个链表节点包含0到9的数值,如9-3-7和6-3相加得到1-0-0-0。通过反向处理链表、逐位相加并处理进位,最终再将结果链表反向,即可完成计算。 ... [详细]
  • CentOS 系统管理基础
    本文介绍了如何在 CentOS 中查询系统版本、内核版本、位数以及磁盘分区的相关知识。通过这些命令,用户可以快速了解系统的配置和磁盘结构。 ... [详细]
  • 本文详细探讨了 PHP 中 method_exists() 和 is_callable() 函数的区别,帮助开发者更好地理解和使用这两个函数。文章不仅解释了它们的功能差异,还提供了代码示例和应用场景的分析。 ... [详细]
author-avatar
zjjj-jz政
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有