} 當(dāng)然,也能使用相同的方法來(lái)處理sendmail的進(jìn)程來(lái)發(fā)送郵件。 下面代碼示例: 復(fù)制代碼 代碼如下: ?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"); ? 其實(shí)這種管道的方法比較底層,取決于你所調(diào)用程式的穩(wěn)定性。所以是一種可選的發(fā)送郵件的方式。 3. 使用phpmailer類 是個(gè)開(kāi)源的發(fā)送郵件類,主站:http://phpmailer.sourceforge.net 里面是兩個(gè)文件,一個(gè)是class.smtp.php,更有以個(gè)是class.phpmailer.php 另外加上官方網(wǎng)站的使用方法: Examples using phpmailer 1. Advanced ExampleThis demonstrates sending out multiple email messages with binary attachments from a MySQL database with multipart/alternative support.復(fù)制代碼 代碼如下: require("class.phpmailer.php");
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";
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 復(fù)制代碼 代碼如下: require("class.phpmailer.php"); 復(fù)制代碼 代碼如下: 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 復(fù)制代碼 代碼如下: 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真是個(gè)好東西,可能非常多人都不怎么用,至少我目前使用他的DB類,發(fā)送郵件類都不錯(cuò)。 需要Net_SMTP類,能去 http://pear.php.net 下載,Net_SMTP類的使用手冊(cè): http://pear.php.net/manual/en/package.networking.net-smtp.php 我使用上面幾個(gè)類,這個(gè)是最佳的,不管是速度還是別的,不過(guò)操作涉及到一些簡(jiǎn)單的smtp協(xié)議。 我的使用代碼:復(fù)制代碼 代碼如下: //------------------------------------------
require_once 'Net/SMTP.php'; //加載類庫(kù)
$subject = "測(cè)試郵件";
$uid = $_POST['uid']; //from信息 $content = " html body ".$u_email