php邮箱发送类代码

 更新时间:2016年11月25日 17:01  点击:2051

下面是一款婚恋网站的邮箱发送实例代码,有需要的朋友可以下载

<?php教程
//if ($err) {echo "发送邮件失败,原因:<br>";foreach($err as $a){echo $a."<br>";}}

class wrzc_netmail {
/*  var $localhost;
 var $smtp_accname;
 var $smtp_password;
 var $smtp_host;
 var $from;
 var $fromname;
 */ function send($to, $subject = 'no subject', $body) {
 $localhost = $this->localhost;
 $smtp_accname = $this->smtp_accname;
 $smtp_password = $this->smtp_password;
 $smtp_host = $this->smtp_host;
 $from = $this->from;
 $fromname = $this->fromname;
 $lb = "rn";
 $headers = "content-type: text/html;charset="gbk"";
 $headers.= $lb;
 $headers.= "content-transfer-encoding: base64";
 $hdr = explode($lb, $headers);
 if ($body) {
 $bdy = preg_replace("/^./", "..", explode($lb, $body));}
 $smtp[] = array("ehlo ".$localhost.$lb, "220,250", "ehlo error: ");
 $smtp[] = array("auth login".$lb, "334", "auth error: ");
 $smtp[] = array(base64_encode($smtp_accname).$lb, "334", "authentification error: ");
 $smtp[] = array(base64_encode($smtp_password).$lb, "235", "authentification error: ");
 $smtp[] = array("mail from: <".$from.">".$lb, "250", "mail from error: ");
 $smtp[] = array("rcpt to: <".$to.">".$lb, "250", "rcpt to error: ");
 $smtp[] = array("data".$lb, "354", "data error: ");
 $smtp[] = array("from: ".$fromname." <".$from.">".$lb, "", "");
 $smtp[] = array("subject: ".$subject.$lb, "", "");
 $smtp[] = array("to: ".$to.$lb, "", "");
 foreach ($hdr as $h) {
 $smtp[] = array($h.$lb, "", "");}
 $smtp[] = array($lb, "", "");
 if ($bdy) {
 foreach ($bdy as $b) {
 $smtp[] = array(base64_encode($b.$lb).$lb, "", "");}}
 $smtp[] = array(".".$lb, "250", "data(end)error: ");
 $smtp[] = array("quit".$lb, "221", "quit error: ");
 $fp = @fsockopen($smtp_host, 25);
 if (!$fp)
 return "error: cannot conect to '".$smtp_host."' by port 25";
 while ($result = @fgets($fp, 1024)) {
 if (substr($result, 3, 1) == " ") {
 break;}}
 $result_str;
 foreach ($smtp as $req) {
 @fputs($fp, $req[0]);
 if ($req[1]) {
 while ($result = @fgets($fp, 1024)) {
 if (substr($result, 3, 1) == " ") {
 break;}};
 if (!strstr($req[1], substr($result, 0, 3))) {
 $result_str[] = $req[2].$result;}}}
 @fclose($fp);
 return $result_str;}
 function setlocalhost($localhost) {$this->localhost = $localhost;}
 function setsmtp_accname($smtp_accname) {$this->smtp_accname = $smtp_accname;}
 function setsmtp_password($smtp_password) {$this->smtp_password = $smtp_password;}
 function setsmtp_host($smtp_host) {$this->smtp_host = $smtp_host;}
 function setfrom($from) {$this->from = $from;}
 function setfromname($fromname) {$this->fromname = $fromname;}
}
//unset
?>

利用php教程 mail()进行邮件发送实现方法

mail() 函数允许您从脚本中直接发送电子邮件。

如果邮件的投递被成功地接收,则返回 true,否则返回 false。

语法
mail(to,subject,message,headers,parameters)

参数 描述
to 必需。规定邮件的接收者。
subject 必需。规定邮件的主题。该参数不能包含任何换行字符。
message 必需。规定要发送的消息。
headers 必需。规定额外的报头,比如 From, Cc 以及 Bcc。
parameters 必需。规定 sendmail 程序的额外参数。

简单的发送html内容

<html>
  <head>
  <title>Simple Send Mail Form</title>
  </head>
  <body>
  <h1>Mail Form</h1>
  <form name="form1" method="post" action="SimpleEmail.php">
  <table>
      <tr><td><b>To</b></td><td><input type="text" name="mailto" size="35"></td></tr>
      <tr><td><b>Subject</b></td>
          <td><input type="text" name="mailsubject" size="35"></td></tr>
      <tr><td><b>Message</b></td>
          <td><textarea name="mailbody" cols="50" rows="7"></textarea></td>
      </tr>
      <tr><td colspan="2">
            <input type="submit" name="Submit" value="Send">
          </td>
      </tr>
   </table>
 </form>
 </body>
 </html>
 
 
<!-- SimpleEmail.php
  <?php
    if (empty ($mailto) ) {
       die ( "Recipient is blank! ") ;
    }

    if (empty ($mailsubject) ){
       $mailsubject=" " ;
    }

    if (empty ($mailbody) ) {
       $mailbody=" " ;
    }

    $result = mail ($mailto, $mailsubject, $mailbody) ;

    if ($result) {
       echo "Email sent successfully!" ;
    }else{
       echo "Email could not be sent." ;
    }
?>

带上抄送与密送功能

HTML>
  <HEAD>
  <TITLE>Send email with CC and BCC</TITLE>
  </HEAD>
  <BODY>
  <FORM action="sendemailWithCC_BCC.php" method=post name=form1>
  <TABLE>
    <TBODY>
    <TR>
      <TD>
       <DIV align=right><b>To</b></DIV></TD>
      <TD>
        <p>Name <INPUT name=mailtoname size=35><BR>E-mail
                <INPUT name=mailtomail size=35></p></TD></TR>
    <TR>
      <TD>
        <DIV align=right><b>CC</b></DIV></TD>
      <TD><INPUT name=mailcc size=35> </TD></TR>
    <TR>
      <TD>
        <DIV align=right><b>BCC</b></DIV></TD>
      <TD><INPUT name=mailbcc size=35> </TD></TR>
    <TR>
      <TD>
        <DIV align=right><b>Priority</b></DIV></TD>
      <TD><SELECT name=mailpriority>
            <OPTION value=1>Highest</OPTION>
            <OPTION value=2>High</OPTION>
            <OPTION selected value=3>Normal</OPTION>
            <OPTION value=4>Low</OPTION>
            <OPTION value=5>Lowest</OPTION>
          </SELECT>
      </TD></TR>
    <TR>
      <TD><DIV align=right><b>Subject</b></DIV></TD>
      <TD><INPUT name=mailsubject size=35></TD></TR>
    <TR>
      <TD>
        <DIV align=right><b>Message</b> </DIV></TD>
      <TD><TEXTAREA cols=50 name=mailbody rows=7></TEXTAREA> </TD></TR>
    <TR>
      <TD colSpan=2>
        <DIV align=center><INPUT name=Submit type=submit value=Submit></DIV>
    </TD>
    </TR>
   </TBODY>
   </TABLE>
  </FORM>
  </BODY>
  </HTML>
 
 
 
 
<!-- sendemailWithCC_BCC.php

  <html>
  <head>
  <title>Mail Sent</title>
  </head>
  <body>
  <?php
 
    $message= " " ;
    if (empty ( $mailtoname) || empty ( $mailtomail) ) {
       die ( "Recipient is blank! ") ;
    }else{
       $to = $mailtoname . " <" . $mailtomail . ">" ;
    }
   
    if ( empty ( $mailsubject) ) {
      $mailsubject=" ";
    }

    if (($mailpriority>0) && ($mailpriority<6)) {
       $mailheader = "X-Priority: ". $mailpriority ."n";
    }

    $mailheader.= "From: " . "Sales Team <sales@yourdomain.com>n";
    $mailheader.= "X-Sender: " . "support@yourdomain.comn";
    $mailheader.= "Return-Path: " . "support@yourdomain.comn";

    if (!empty($mailcc)) {
      $mailheader.= "Cc: " . $mailcc ."n";
    }

    if (!empty($mailbcc)) {
      $mailheader.= "Bcc: " . $mailbcc ."n";
    }
   
    if (empty($mailbody)) {
      $mailbody=" ";
    }
 
    $result = mail ($to, $mailsubject, $mailbody, $mailheader);
    echo "<center><b>Mail sent to ". "$to". "<br>";
    echo $mailsubject. "<br>";
    echo $mailbody. "<br>";
    echo $mailheader. "<br>";
    if ($result) {
       echo "<p><b>Email sent successfully!</b></p>";
    }else{
       echo "<p><b>Email could not be sent. </b></p>";
    }
  ?>
  <div align="center">
  <table><tr><td width="66"><div align="right"><b>To</b></div></td>
             <td width="308"><b><?php echo $mailtoname . " [". $mailtomail . " ]";?></b></td></tr>
        
         <tr><td width="66"><div align="right"><b>CC</b></div></td>
             <td width="308"><b><?php echo $mailcc;?></b></td></tr>
         <tr><td width="66"><div align="right"><b>BCC</b></div></td>
             <td width="308"><b><?php echo $mailbcc; ?></b></td></tr>
         <tr><td width="66"><div align="right"><b>Priority</b></div></td>
             <td width="308"><b><?php echo $mailpriority;?></b></td></tr>
         <tr><td width="66"><div align="right"><b>Subject </b></div></td>
             <td width="308"><b><?php echo $mailsubject;?></b></td></tr>
         <tr><td width="66"><div align="right"><b>Message</b></div></td>
             <td width="308"><b><?php echo $mailbody;?></b></td></tr>
  </table>
  </div>
  </body>
  </html>


在 message 参数规定的消息中,行之间必须以一个 LF(n)分隔。每行不能超过 70 个字符。

(Windows 下)当 PHP 直接连接到 SMTP 服务器时,如果在一行开头发现一个句号,则会被删掉。要避免此问题,将单个句号替换成两个句号。

 

php教程 fsockopen邮箱发送实例代码

<?
//ok的邮箱发送。
include "smtp.class.php";
//$smtps教程erver = "smtp.163.com"; //您的smtp服务器的地址
$smtpserver="smtp.163.com";
$port =25; //smtp服务器的端口,一般是 25
$smtpuser = "你的邮箱@163.com"; //您登录smtp服务器的用户名
$smtppwd = "你邮箱的密码"; //您登录smtp服务器的密码
$mailtype = "txt"; //邮件的类型,可选值是 txt 或 html ,txt 表示是纯文本的邮件,html 表示是 html格式的邮件
$sender = "你的邮箱@163.com";
//发件人,一般要与您登录smtp服务器的用户名($smtpuser)相同,否则可能会因为smtp服务器的设置导致发送失败
$smtp = new smtp($smtpserver,$port,true,$smtpuser,$smtppwd,$sender);
$smtp->debug = true; //是否开启调试,只在测试程序时使用,正式使用时请将此行注释
$to = "你要发给的那个人的邮箱地址"; //收件人
$subject = "你好";
$body = "你发送的内容 ";
$send=$smtp->sendmail($to,$sender,$subject,$body,$mailtype);

if($send==1){
echo "邮件发送成功";
}else{
echo "邮件发送失败<br/>";
//echo "原因:".$this->smtp->logs;
}
?>

邮箱发送类smtp.class.php

<?php
class smtp
{
/* public variables */
var $smtp_port;
var $time_out;
var $host_name;
var $log_file;
var $relay_host;
var $debug;
var $auth;
var $user;
var $pass;
/* private variables */
var $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 = ereg_replace("(^|(rn))(.)", "1.3", $body);
$header .= "mime-version:1.0rn";
if($mailtype=="html"){
$header .= "content-type:text/htmlrn";
}
$header .= "to: ".$to."rn";
if ($cc != "") {
$header .= "cc: ".$cc."rn";
}
$header .= "from: $from<".$from.">;rn";
$header .= "subject: ".$subject."rn";
$header .= $additional_headers;
$header .= "date: ".date("r")."rn";
$header .= "x-mailer:by redhat (php/".phpversion().")rn";
list($msec, $sec) = explode(" ", microtime());
$header .= "message-id: <".date("ymdhis", $sec).".".($msec*1000000).".".$mail_from.">;rn";
$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 hostn");
}
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 <cr>;<lf>;.<cr>;<lf>; [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."rn".$body);
$this->smtp_debug(">; ".str_replace("rn", "n".">; ", $header."n>; ".$body."n>; "));
return true;
}
function smtp_eom()
{
fputs($this->sock, "rn.rn");
$this->smtp_debug(". [eom]n");
return $this->smtp_ok();
}
function smtp_ok()
{
$response = str_replace("rn", "", fgets($this->sock, 512));
$this->smtp_debug($response."n");
if (!ereg("^[23]", $response)) {
fputs($this->sock, "quitrn");
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."rn");
$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");
?>

更多关于fsockopen函数请查看

http://www.111cn.net/phper/php-function/33796.htm

/*邮件发送类
*功能:php教程 socket 使用smtp服务器发送邮件
*作者:longlong
*时间:2007-11-26
*转帖请加本贴引用地址:showpost.asp教程?threadid=1345 
*/
class smtp
{
/* 全局变量 */
var $smtp_port;
var $time_out;
var $host_name;
var $log_file;
var $relay_host;
var $debug;
var $auth;
var $user;
var $pass;

/* private variables */ 
var $sock;

/* 构造函数 */

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;
}

/* 主函数,发送邮件 */

function sendmail($flag, $boundary, $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.0rn";
   
  //*
  if($mailtype=="html"){
  //echo $boundary;exit;
if($flag==2)
{
  $header .= "content-type:multipart/mixed; boundary= $boundaryrn";
  //$header .= "content-type:text/htmlrn";
}
else

  $header .= "content-type:text/htmlrn";
}
  }
  //*/

  $header .= "to: ".$to."rn";

  if ($cc != "") {
  $header .= "cc: ".$cc."rn";
  }

  $header .= "from: $from<".$from.">rn";
  $header .= "subject: ".$subject."rn";
  $header .= $additional_headers;
  $header .= "date: ".date("r")."rn";
  $header .= "x-mailer:by redhat (php/".phpversion().")rn";

  //$header.=$body;//edit by shaolong
  
  list($msec, $sec) = explode(" ", microtime());

  $header .= "message-id: <".date("ymdhis", $sec).".".($msec*1000000).".".$mail_from.">rn";

  $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 hostn");
  }
  return $sent;
}

/* 私有函数 */

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 <cr><lf>.<cr><lf> [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."rn".$body);
  $this->smtp_debug("> ".str_replace("rn", "n"."> ", $header."n> ".$body."n> "));
  return true;
}


function smtp_eom()
{
  fputs($this->sock, "rn.rn");

  $this->smtp_debug(". [eom]n");

  return $this->smtp_ok();
}


function smtp_ok()
{
  $response = str_replace("rn", "", fgets($this->sock, 512));
  $this->smtp_debug($response."n");

  if (!ereg("^[23]", $response)) {
  fputs($this->sock, "quitrn");
  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."rn");
  $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;
  }
}
}

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>php发送邮件</title>
</head>

<body>
<table width="611" height="200" border="1">
  <tr>
    <td width="601"><form id="form1" name="form1" method="post" action="send.php">
      <table width="600" height="240" border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td width="102" height="28">收件人地址</td>
          <td width="213"><input name="to" type="text" id="to" /></td>
          <td width="84">发送人地址</td>
          <td width="201"><input name="from" type="text" id="from" value="cert@163.com" /></td>
        </tr>
        <tr>
          <td height="33">发送人用户名</td>
          <td><input name="name" type="text" id="username" value="cert" /></td>
          <td>邮箱密码</td>
          <td><input name="password" type="password" id="password" value="***" /></td>
        </tr>
        <tr>
          <td height="27">smtp服务器</td>
          <td><input name="smtp" type="text" id="smtp" value="smtp.163.com" /></td>
          <td colspan="2">注:163邮箱smtp为: smtp.163.com</td>
          </tr>
        <tr>
          <td height="26">标题</td>
          <td colspan="3"><input name="subject" type="text" id="subject" value="cert测试php发送邮件" size="50" /></td>
          </tr>
        <tr>
          <td height="69">内容</td>
          <td colspan="3"><textarea name="content" cols="50" rows="6" id="content">计算机紧急响应组欢迎你!
http://www.111cn.net
组织网站即将进行改版</textarea></td>
          </tr>
        <tr>
       
          <td>&nbsp;</td>
          <td align="right"><input type="submit" name="submit" value="发送" /></td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
      </table>
        <p>说明:我用163的邮箱发给 163 或 126 的邮箱立刻就能收到。大家试试。</p>
    </form>
    </td>
  </tr>
</table>
</body>
</html>

<?
require_once 'mail.php';

$conf['mail'] = array(
        'host'     => 'smtp.126.com',                  //smtp服务器地址
        'auth'     => true,                           //true表示smtp服务器需要验证,false不需要
        'username' => 'liangbowen',                   //用户名
        'password' => '******'                        //密码
);


//发送邮件
$headers['from']    = 'liangbowen@126.com';               //发信地址
$headers['to']      = 'liangbowen@hotmail.com';               //收信地址
$headers['subject'] = 'test mail send by php bowen.mvbb.com';   //邮件标题
$mail_object = &mail::factory('smtp', $conf['mail']);   
//邮件正文
$body = "这是一封自己发给自己的邮件。";

$mail_res = $mail_object->send($headers['to'], $headers, $body); //发送

                           
if(pear::iserror($mail_res)){      //检测错误
    die($mail_res->getmessage());
}
else{
 echo "send successful!";
}

源码下载

http://down.111cn.net/down/code/php/qitayuanma/2010/1220/22333.html

[!--infotagslink--]

相关文章

  • 不打开网页直接查看网站的源代码

      有一种方法,可以不打开网站而直接查看到这个网站的源代码..   这样可以有效地防止误入恶意网站...   在浏览器地址栏输入:   view-source:http://...2016-09-20
  • php 调用goolge地图代码

    <?php require('path.inc.php'); header('content-Type: text/html; charset=utf-8'); $borough_id = intval($_GET['id']); if(!$borough_id){ echo ' ...2016-11-25
  • JS基于Mootools实现的个性菜单效果代码

    本文实例讲述了JS基于Mootools实现的个性菜单效果代码。分享给大家供大家参考,具体如下:这里演示基于Mootools做的带动画的垂直型菜单,是一个初学者写的,用来学习Mootools的使用有帮助,下载时请注意要将外部引用的mootools...2015-10-23
  • JS+CSS实现分类动态选择及移动功能效果代码

    本文实例讲述了JS+CSS实现分类动态选择及移动功能效果代码。分享给大家供大家参考,具体如下:这是一个类似选项卡功能的选择插件,与普通的TAb区别是加入了动画效果,多用于商品类网站,用作商品分类功能,不过其它网站也可以用,...2015-10-21
  • JS实现自定义简单网页软键盘效果代码

    本文实例讲述了JS实现自定义简单网页软键盘效果。分享给大家供大家参考,具体如下:这是一款自定义的简单点的网页软键盘,没有使用任何控件,仅是为了练习JavaScript编写水平,安全性方面没有过多考虑,有顾虑的可以不用,目的是学...2015-11-08
  • php 取除连续空格与换行代码

    php 取除连续空格与换行代码,这些我们都用到str_replace与正则函数 第一种: $content=str_replace("n","",$content); echo $content; 第二种: $content=preg_replac...2016-11-25
  • php简单用户登陆程序代码

    php简单用户登陆程序代码 这些教程很对初学者来讲是很有用的哦,这款就下面这一点点代码了哦。 <center> <p>&nbsp;</p> <p>&nbsp;</p> <form name="form1...2016-11-25
  • PHP实现清除wordpress里恶意代码

    公司一些wordpress网站由于下载的插件存在恶意代码,导致整个服务器所有网站PHP文件都存在恶意代码,就写了个简单的脚本清除。恶意代码示例...2015-10-23
  • JS实现双击屏幕滚动效果代码

    本文实例讲述了JS实现双击屏幕滚动效果代码。分享给大家供大家参考,具体如下:这里演示双击滚屏效果代码的实现方法,不知道有觉得有用处的没,现在网上还有很多还在用这个特效的呢,代码分享给大家吧。运行效果截图如下:在线演...2015-10-30
  • js识别uc浏览器的代码

    其实挺简单的就是if(navigator.userAgent.indexOf('UCBrowser') > -1) {alert("uc浏览器");}else{//不是uc浏览器执行的操作}如果想测试某个浏览器的特征可以通过如下方法获取JS获取浏览器信息 浏览器代码名称:navigator...2015-11-08
  • JS日期加减,日期运算代码

    一、日期减去天数等于第二个日期function cc(dd,dadd){//可以加上错误处理var a = new Date(dd)a = a.valueOf()a = a - dadd * 24 * 60 * 60 * 1000a = new Date(a)alert(a.getFullYear() + "年" + (a.getMonth() +...2015-11-08
  • PHP开发微信支付的代码分享

    微信支付,即便交了保证金,你还是处理测试阶段,不能正式发布。必须到你通过程序测试提交订单、发货通知等数据到微信的系统中,才能申请发布。然后,因为在微信中是通过JS方式调用API,必须在微信后台设置支付授权目录,而且要到...2014-05-31
  • PHP常用的小程序代码段

    本文实例讲述了PHP常用的小程序代码段。分享给大家供大家参考,具体如下:1.计算两个时间的相差几天$startdate=strtotime("2009-12-09");$enddate=strtotime("2009-12-05");上面的php时间日期函数strtotime已经把字符串...2015-11-24
  • NodeJS实现阿里大鱼短信通知发送

    本文给大家介绍的是nodejs实现使用阿里大鱼短信API发送消息的方法和代码,有需要的小伙伴可以参考下。...2016-01-20
  • php怎么用拼音 简单的php中文转拼音的实现代码

    小编分享了一段简单的php中文转拼音的实现代码,代码简单易懂,适合初学php的同学参考学习。 代码如下 复制代码 <?phpfunction Pinyin($_String...2017-07-06
  • PHP测试成功的邮件发送案例

    mail()函数的作用:连接到邮件服务器,利用smtp协议,与该服务器交互并投邮件。注意:1、mail函数不支持esmtp协议,---即,只能直投,不能登陆2、由上条,我们只能直投至最终的收件服务器地址.而该地址,又是在PHP.ini中指定的,所...2015-10-30
  • php导出csv格式数据并将数字转换成文本的思路以及代码分享

    php导出csv格式数据实现:先定义一个字符串 存储内容,例如 $exportdata = '规则111,规则222,审222,规222,服2222,规则1,规则2,规则3,匹配字符,设置时间,有效期'."/n";然后对需要保存csv的数组进行foreach循环,例如复制代...2014-06-07
  • ecshop商品无限级分类代码

    ecshop商品无限级分类代码 function cat_options($spec_cat_id, $arr) { static $cat_options = array(); if (isset($cat_options[$spec_cat_id]))...2016-11-25
  • 几种延迟加载JS代码的方法加快网页的访问速度

    本文介绍了如何延迟javascript代码的加载,加快网页的访问速度。 当一个网站有很多js代码要加载,js代码放置的位置在一定程度上将会影像网页的加载速度,为了让我们的网页加载速度更快,本文总结了一下几个注意点...2013-10-13
  • php邮件发送的两种方式

    这篇文章研究的主要内容就是使用PHP来发送电子邮件,总结为以下两种方法:一、使用PHP内置的mail()函数<&#63;php $to = "test@163.com"; //收件人 $subject = "Test"; //主题 $message = "This is a test mail!"; //正文...2015-10-30