欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php多种形式发送邮件(mail qmail邮件系统 phpmailer类)

程序员文章站 2022-06-20 23:46:51
1. 使用 mail() 函数 没什么好讲的,就是使用系统自带的smtp系统来发送,一般是使用sendmail来发。这个按照各个系统不同而定。使用参考手册。 2....

1. 使用 mail() 函数

没什么好讲的,就是使用系统自带的smtp系统来发送,一般是使用sendmail来发。这个按照各个系统不同而定。使用参考手册。

2. 使用管道的形式

昨天刚测试成功,使用本地的qmail来发送邮件。

复制代码 代码如下:

/* 使用qmail发送邮件函数 */ 
function send_check_mail($email, $subject,$uid,$buffer) 

 $command =  "/var/qmail/bin/qmail-inject ".$email; //qmail程式地址,$email是要发送的地址 
 $handle = popen($command, "w"); //打开管道  http://www.cnblogs.com/roucheng/
 if (!$handle) { 
  return false; 
 }  

 $from = "webmaster@unixsky.net"; //发件人 
 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 = "hren@yahoo.com.cn"; //发送到的邮箱 
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; 

}

当然,也能使用相同的方法来处理sendmail的进程来发送邮件。

下面代码示例:

复制代码 代码如下:

<?php 
$pp = popen("/usr/sbin/sendmail -t", "w") or die("cannot fork sendmail"); 
fputs($pp, "to: sterling@designmultimedia.com\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类

是个开源的发送邮件类,主站:http://phpmailer.sourceforge.net

里面是两个文件,一个是class.smtp.php,更有以个是class.phpmailer.php
另外加上官方网站的使用方法:
examples using phpmailer
1. advanced examplethis 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     = "list@example.com"; 
$mail->fromname = "list manager"; 
$mail->host     = "smtp1.example.com;smtp2.example.com"; 
$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 phpmailerextending 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: mail.inc.php

复制代码 代码如下:

require("class.phpmailer.php");

复制代码 代码如下:

class my_phpmailer extends phpmailer { 
    // set default variables for all new objects 
    var $from     = "from@example.com"; 
    var $fromname = "mailer"; 
    var $host     = "smtp1.example.com;smtp2.example.com"; 
    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("mail.inc.php"); 

// instantiate your new class 
$mail = new my_phpmailer; 

// now you only need to add the necessary stuff 
$mail->addaddress("josh@example.com", "josh adams"); 
$mail->subject = "here is the subject"; 
$mail->body    = "this is the message body"; 
$mail->addattachment("c:/temp/11-10-00.zip", "new_name.zip");  // optional name 

if(!$mail->send()) 

   echo "there was an error sending the message"; 
   exit; 


echo "message was sent successfully";

4. 使用pear::net_smtp组件

pear真是个好东西,可能非常多人都不怎么用,至少我目前使用他的db类,发送邮件类都不错。

需要net_smtp类,能去 http://pear.php.net 下载,net_smtp类的使用手册:

http://pear.php.net/manual/en/package.networking.net-smtp.php

我使用上面几个类,这个是最佳的,不管是速度还是别的,不过操作涉及到一些简单的smtp协议。

我的使用代码:

复制代码 代码如下:

//------------------------------------------ 

require_once 'net/smtp.php'; //加载类库 

 
$subject = "测试邮件"; 

$uid = $_post['uid']; //from信息 
$content = "<html><body>".$u_email  

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

$u_email = "hren@yahoo.com.cn"; //发送到的邮箱 

$smtp = new net_smtp('192.168.0.1'); //smtp服务器 
$smtp->connect(); //连接服务器 
$smtp->helo('unixsky.net'); //发送helo信息给服务器 
$smtp->mailfrom('hren@unixsky.net'); //发件人地址 
$smtp->rcptto($u_email); //收件人地址 
$date = date('r'); //获取发信日期 
$smtp->data("date: $date\r\nfrom: vdddote@eyou.net\r\nto: $u_email\r\nsubject: $subject\r\ncontent-type: text/html; charset=\"gb2312\"\r\n\r\n$content\r\n"); //添加发送数据并且发送 
$smtp->disconnect(); //关闭连接