1. 使用 mail() 函数


2. 使用管道的形式


/* 使用qmail发送邮件函数 */
function send_check_mail($email, $subject,$uid,$buffer)
 $command =  “/var/qmail/bin/qmail-inject “.$email; //qmail程序地址,$email是要发送的地址
 $handle = popen($command, “w”); //打开管道
 if (!$handle) {
  return false;

 $from = ““; //发件人
 fwrite($handle, “From: “.$from.”\n”); //往管道写数据
 fwrite($handle, “Return-Path: “.$from.”\n”);
 fwrite($handle, “To: “.$uid.”\n”);
 fwrite($handle, “Subject: “.$subject.”\n”);
 fwrite($handle, “Mime-Version: 1.0\n”);
 fwrite($handle, “Content-Type: text/html; charset=\”gb2312\”\n\n”);
 fwrite($handle, $buffer.”\n”);
 pclose($handle); //关闭管道

 return true;



$subject = “测试邮件”;

$uid = $_POST[‘uid’]; //from信息
$content = “<html><body>”.$u_email

   .” 您好!<br><br>谢谢,本邮件测试!<br</body></html>”; //内容信息

$u_email = ““; //发送到的邮箱
if (send_check_mail($u_email, $subject, $uid, $content)) {

 echo “恭喜!发送投票邮件到您的邮箱!<br><br>请检查您的邮箱:<font color=#CC0033>”.$u_email.” </font><br><br>”. $close;
 } else {

 echo “很不幸,发送投票邮件到您的邮箱失败,请重试或者联系开发人员。<br><br>”. $close;




$pp = popen(“/usr/sbin/sendmail -t”, “w”) or die(“Cannot fork sendmail”);
fputs($pp, “To:\r\n“);
fputs($pp, “Reply-to: $senders_email\r\n”);
fputs($pp, “From: $senders_email\r\n”);
fputs($pp, “Subject The Results of your form\r\n\r\n”);
fputs($pp, “$senders_email sent the fllowing comments:\r\n”);
fputs($pp, $comments);
pclose($pp) or die(“Cannot close pipe to sendmail”);


3. 使用phpmailer类




Examples using phpmailer

1. Advanced Example

This demonstrates sending out multiple email messages with binary attachments from a MySQL database with multipart/alternative support.

require(“class.phpmailer.php”);$mail = new phpmailer();$mail->From     = “”;$mail->FromName = “List manager”;$mail->Host     = “;”;$mail->Mailer   = “smtp”;@MYSQL_CONNECT(“localhost”,”root”,”password”);@mysql_select_db(“my_company”);$query?=?SELECT full_name, email,爌hoto燜ROM employee燱HERE爄d=$id”;$result?燖MYSQL_QUERY($query);while ($row = mysql_fetch_array ($result)){    // HTML body    $body  = “Hello <font size=\”4\”>” . $row[“full_name”] . “</font>, <p>”;    $body .= “<i>Your</i> personal photograph to this message.<p>”;    $body .= “Sincerely, <br>”;    $body .= “phpmailer List manager”;    // Plain text body (for mail clients that cannot read HTML)    $text_body  = “Hello ” . $row[“full_name”] . “, \n\n”;    $text_body .= “Your personal photograph to this message.\n\n”;    $text_body .= “Sincerely, \n”;    $text_body .= “phpmailer List manager”;    $mail->Body    = $body;    $mail->AltBody = $text_body;    $mail->AddAddress($row[“email”], $row[“full_name”]);    $mail->AddStringAttachment($row[“photo”], “YourPhoto.jpg”);    if(!$mail->Send())        echo “There has been a mail error sending to ” . $row[“email”] . “<br>”;    // Clear all addresses and attachments for next loop    $mail->ClearAddresses();    $mail->ClearAttachments();}

2. Extending phpmailer

Extending classes with inheritance is one of the most powerful features of object-oriented programming. It allows you to make changes to the original class for your own personal use without hacking the original classes. Plus, it is very easy to do. I’ve provided an example:

Here’s a class that extends the phpmailer class and sets the defaults for the particular site:
PHP include file:

require(“class.phpmailer.php”);class my_phpmailer extends phpmailer {    // Set default variables for all new objects    var $From     = “”;    var $FromName = “Mailer”;    var $Host     = “;”;    var $Mailer   = “smtp”;                         // Alternative to IsSMTP()    var $WordWrap = 75;    // Replace the default error_handler    function error_handler($msg) {        print(“My Site Error”);        print(“Description:”);        printf(“%s”, $msg);        exit;    }    // Create an additional function    function do_something($something) {        // Place your new code here    }}

Now here’s a normal PHP page in the site, which will have all the defaults set above:
Normal PHP file: mail_test.php

require(“”);// Instantiate your new class$mail = new my_phpmailer;// Now you only need to add the necessary stuff$mail->AddAddress(“”, “Josh Adams”);$mail->Subject = “Here is the subject”;$mail->Body    = “This is the message body”;$mail->AddAttachment(“c:/temp/”, “”);  // optional nameif(!$mail->Send()){   echo “There was an error sending the message”;   exit;}echo “Message was sent successfully”;

4. 使用PEAR::Net_SMTP组件


需要Net_SMTP类,可以去 下载,Net_SMTP类的使用手册:




require_once ‘Net/SMTP.php’; //加载类库

$subject = “测试邮件”;

$uid = $_POST[‘uid’]; //from信息
$content = “<html><body>”.$u_email

   .” 您好!<br><br>谢谢,本邮件测试!<br</body></html>”; //内容信息

$u_email = ““; //发送到的邮箱

$smtp = new Net_SMTP(‘’); //smtp服务器
$smtp->connect(); //连接服务器
$smtp->helo(‘’); //发送HELO信息给服务器
$smtp->mailFrom(‘’); //发件人地址
$smtp->rcptTo($u_email); //收件人地址
$date = date(‘r’); //获取发信日期
$smtp->data(“Date: $date\r\nFrom:\r\nTo: $u_email\r\nSubject: $subject\r\nContent-Type: text/html; charset=\”gb2312\”\r\n\r\n$content\r\n”); //添加发送数据并且发送
$smtp->disconnect(); //关闭连接

5. 其他方法