1. 程式人生 > >PHP多種形式傳送郵件

PHP多種形式傳送郵件

1. 使用 mail() 函式 

沒什麼好講的,就是使用系統自帶的smtp系統來發送,一般是使用sendmail來發。這個按照各個系統不同而定。使用參考手冊。 

2. 使用管道的形式 

昨天剛測試成功,使用本地的qmail來發送郵件。 

 1 /* 使用qmail傳送郵件函式 */  
 2 function send_check_mail($email, $subject,$uid,$buffer)  
 3 {  
 4  $command =  "/var/qmail/bin/qmail-inject ".$email; //qmail程式地址,$email是要傳送的地址  
 5  $handle
= popen($command, "w"); //開啟管道 http://www.cnblogs.com/roucheng/ 6 if (!$handle) { 7 return false; 8 } 9 10 $from = "[email protected]"; //發件人 11 fwrite($handle, "From: ".$from."\n"); //往管道寫資料 12 fwrite($handle, "Return-Path: ".$from."\n"); 13 fwrite($handle, "To: ".$uid
."\n"); 14 fwrite($handle, "Subject: ".$subject."\n"); 15 fwrite($handle, "Mime-Version: 1.0\n"); 16 fwrite($handle, "Content-Type: text/html; charset=\"gb2312\"\n\n"); 17 fwrite($handle, $buffer."\n"); 18 pclose($handle); //關閉管道 19 20 return true; 21 } 22 23 ------------------測試傳送郵件:
24 25 //傳送郵件 26 27 $subject = "測試郵件"; 28 29 $uid = $_POST[’uid’]; //from資訊 30 $content = "<html><body>".$u_email 31 32 ." 你好!<br><br>謝謝,本郵件測試!<br</body></html>"; //內容資訊 33 34 $u_email = "[email protected]"; //傳送到的郵箱 35 if (send_check_mail($u_email, $subject, $uid, $content)) { 36 37 echo "恭喜!傳送投票郵件到你的郵箱!<br><br>請檢查你的郵箱:<font color=#CC0033>".$u_email." </font><br><br>". $close; 38 } else { 39 40 echo "非常不幸,傳送投票郵件到你的郵箱失敗,請重試或聯絡研發人員。<br><br>". $close; 41 42 }

當然,也能使用相同的方法來處理sendmail的程序來發送郵件。 

下面程式碼示例: 

 1 <?php  
 2 $pp = popen("/usr/sbin/sendmail -t", "w") or die("Cannot fork sendmail");  
 3 fputs($pp, "To: [email protected]\r\n");  
 4 fputs($pp, "Reply-to: $senders_email\r\n");  
 5 fputs($pp, "From: $senders_email\r\n");  
 6 fputs($pp, "Subject The Results of your form\r\n\r\n");  
 7 fputs($pp, "$senders_email sent the fllowing comments:\r\n");  
 8 fputs($pp, $comments);  
 9 pclose($pp) or die("Cannot close pipe to sendmail");  
10 ?>  

其實這種管道的方法比較底層,取決於你所呼叫程式的穩定性。所以是一種可選的傳送郵件的方式。 


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.

 1 require("class.phpmailer.php");  
 2   
 3 $mail = new phpmailer();  
 4   
 5 $mail->From     = "[email protected]";  
 6 $mail->FromName = "List manager";  
 7 $mail->Host     = "smtp1.example.com;smtp2.example.com";  
 8 $mail->Mailer   = "smtp";  
 9   
10 @MYSQL_CONNECT("localhost","root","password");  
11 @mysql_select_db("my_company");  
12 $query?=?SELECT full_name, email,?hoto?ROM employee?HERE?d=$id";  
13 $result??MYSQL_QUERY($query);  
14   
15 while ($row = mysql_fetch_array ($result))  
16 {  
17     // HTML body  
18     $body  = "Hello <font size=\"4\">" . $row["full_name"] . "</font>, <p>";  
19     $body .= "<i>Your</i> personal photograph to this message.<p>";  
20     $body .= "Sincerely, <br>";  
21     $body .= "phpmailer List manager";  
22   
23     // Plain text body (for mail clients that cannot read HTML)  
24     $text_body  = "Hello " . $row["full_name"] . ", \n\n";  
25     $text_body .= "Your personal photograph to this message.\n\n";  
26     $text_body .= "Sincerely, \n";  
27     $text_body .= "phpmailer List manager";  
28   
29     $mail->Body    = $body;  
30     $mail->AltBody = $text_body;  
31     $mail->AddAddress($row["email"], $row["full_name"]);  
32     $mail->AddStringAttachment($row["photo"], "YourPhoto.jpg");  
33   
34     if(!$mail->Send())  
35         echo "There has been a mail error sending to " . $row["email"] . "<br>";  
36   
37     // Clear all addresses and attachments for next loop  
38     $mail->ClearAddresses();  
39     $mail->ClearAttachments();  
40 }  

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"); 

 1 class my_phpmailer extends phpmailer {  
 2     // Set default variables for all new objects  
 3     var $From     = "[email protected]";  
 4     var $FromName = "Mailer";  
 5     var $Host     = "smtp1.example.com;smtp2.example.com";  
 6     var $Mailer   = "smtp";                         // Alternative to IsSMTP()  
 7     var $WordWrap = 75;  
 8   
 9     // Replace the default error_handler  
10     function error_handler($msg) {  
11         print("My Site Error");  
12         print("Description:");  
13         printf("%s", $msg);  
14         exit;  
15     }  
16   
17     // Create an additional function  
18     function do_something($something) {  
19         // Place your new code here  
20     }  
21 }  

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

 1 require("mail.inc.php");  
 2   
 3 // Instantiate your new class  
 4 $mail = new my_phpmailer;  
 5   
 6 // Now you only need to add the necessary stuff  
 7 $mail->AddAddress("[email protected]", "Josh Adams");  
 8 $mail->Subject = "Here is the subject";  
 9 $mail->Body    = "This is the message body";  
10 $mail->AddAttachment("c:/temp/11-10-00.zip", "new_name.zip");  // optional name  
11   
12 if(!$mail->Send())  
13 {  
14    echo "There was an error sending the message";  
15    exit;  
16 }  
17   
18 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協議。 
http://www.cnblogs.com/roucheng/
我的使用程式碼:

 1 //------------------------------------------  
 2   
 3 require_once ’Net/SMTP.php’; //載入類庫  
 4   
 5   
 6 $subject = "測試郵件";  
 7   
 8 $uid = $_POST[’uid’]; //from資訊  
 9 $content = "<html><body>".$u_email   
10   
11    ." 你好!<br><br>謝謝,本郵件測試!<br</body></html>"; //內容資訊  
12   
13 $u_email = "[email protected]"; //傳送到的郵箱  
14   
15 $smtp = new Net_SMTP(’192.168.0.1’); //smtp伺服器  
16 $smtp->connect(); //連線伺服器  
17 $smtp->helo(’unixsky.net’); //傳送HELO資訊給伺服器  
18 $smtp->mailFrom(’[email protected]’); //發件人地址  
19 $smtp->rcptTo($u_email); //收件人地址  
20 $date = date(’r’); //獲取發信日期  
21 $smtp->data("Date: $date\r\nFrom: [email protected]\r\nTo: $u_email\r\nSubject: $subject\r\nContent-Type: text/html; charset=\"gb2312\"\r\n\r\n$content\r\n"); //添加發送資料並且傳送  
22 $smtp->disconnect(); //關閉連線  

相關推薦

PHP多種形式傳送郵件

1. 使用 mail() 函式 沒什麼好講的,就是使用系統自帶的smtp系統來發送,一般是使用sendmail來發。這個按照各個系統不同而定。使用參考手冊。 2. 使用管道的形式 昨天剛測試成功,使用本地的qmail來發送郵件。  1 /* 使用qmail傳送郵件函式 */ 2 fun

oracle pl/sql 將資料寫入Csv檔案 且以附件的形式傳送郵件

內容介紹 這篇文章將介紹,oracle 中如何將資料庫中查找出來的資料寫入csv 檔案,且將這個csv 檔案 ,用郵件以附件的形式傳送出去。如果你也想實現這個功能,請參考以下程式碼。如果想用sqlplus 方式實現,請參考我的另外一篇文章:sqlplus spool 生成csv檔案,且用

php使用smtp傳送郵件

   在這裡我要介紹的是如何使用smtp進行傳送郵件。 一、準備材料 二、程式碼實現     簡單粗暴,上程式碼: <?php header("Content-Type: text/html; charset=utf-8"); require_once(

[php]mail函式傳送郵件(正文+附件+中文)

<?php $from = "[email protected]"; $to = "[email protected], [email protected]"; $subject = "郵件主題"; $subject = "=?UTF-8

php利用smtp傳送郵件

PHP : 5.6.8 email工具類下載地址:      http://files.cnblogs.com/files/rhythmK/email.class.zip 傳送郵件程式碼如下: require_once("email.class.php"); $sm

php mail函式傳送郵件header過長

今天用php的mail函式傳送一個郵件,其中header部分是用html拼出來的一個表格,但發出來之後會發現表格會有部分顯示不正常,比如"<tr><td>a</td><td>b</td><td>c<

最新PHP實現SMTP傳送郵件的方法

<?php require("smtp.php"); $smtpserver = "smtp.qq.com";//SMTP伺服器 $smtpserverport = 25;//SMTP伺服器埠

php使用PHPMailer傳送郵件

本篇記錄的是我的發郵件的程式碼整理。用PHPMailer實現發郵件功能 <?php use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; require './PHPMail

【JavaMail開發總結】配置檔案形式--傳送郵件程式

        在上一篇中簡單的實現了一個傳送郵件功能的程式,今天用配置檔案的方式來實現,大致思路一致,示例程式碼如下: package com.javamail.test.demo; import java.io.IOException; import java.io.I

PHP利用PHPmailer封裝包傳送郵件

1、github上下載PHPmailer檔案https://github.com/PHPMailer/PHPMailer解壓縮,將src下PHPMailer、Exception和SMTP檔案複製到相應位置,更改裡面的名稱空間,然後編寫傳送郵件的程式碼 2、$mail = new PHPMaile

PHP傳送郵件PHPMailer》系列分享專欄

《PHP傳送郵件PHPMailer》已整理成PDF文件,點選可直接下載至本地查閱https://www.webfalse.com/read/201726.html 文章 PHPMailer郵件類利用smtp.163.com傳送郵件方法 PHPMailer安裝方法及簡單例項

PHP傳送郵件

PHP傳送郵件的示例。 首先下載PHPmailer ,下載地址: https://github.com/PHPMailer/PHPMailer/ <?php // ini_set("display_errors", "On"); // error_reporti

郵件附件的形式傳送測試報告

1. 建立 EmailAnnex目錄, 在 EmailAnnex 下建立 bing.py,並編寫 from selenium import webdriver from time import sleep import unittest class Bing(unittest.TestCas

郵件形式傳送測試報告

1.建立一個Email 目錄(資料夾),在 Email 中建立 bing.py測試用例 from selenium import webdriver from time import sleep import unittest # driver.find_element_by_xpath("

使用PHP傳送郵件

來源:慕課網教程 一、用composer安裝nette/mail composer require nette/mail 在當前資料夾下生成vendor資料夾,composer.json檔案,composer.lock檔案。 二、資料庫 程

LDAP批量新增php頁面,包含郵件傳送

頁面index.html <html lang="en"> <head> <!-- <meta charset="UTF-8"> --> <title>ldap新增使用者</title> <

php Thinkphp結合composer實現smtp傳送郵件

傳送郵件也算是網站的常用功能之一,相信很多人已經在網上找到相應的原始碼(我以前也是用的那套原始碼,程式碼很老了),為了避免重複造輪子和節約時間,使用composer上的nette/mail包。 版本要求: php:5.3.1+ nette/mail:2.3(

PHP中呼叫mail()函式傳送郵件所需sendmail的基本配置和html格式的郵件資訊

首先從http://glob.com.au/sendmail上下載sendmail壓縮包;並將其解壓到D:盤中(一般最好不要解壓到C:盤,且目錄不要太長)。 設定一下PHP.ini檔案: [mail function] ; For Win32 only. ; http://

解決zabbix 報警郵件以附件形式傳送

#!/bin/sh #export.UTF-8 FILE=/tmp/mailtmp.txt echo "$3" >$FILE dos2unix -k $FILE /bin/mail -s "$2" $1 < $FILE touch /tmp/mailtmp.txt chown zabbix.za

Linux配置sendmail實現PHP傳送郵件

1.安裝sendmail yum -y install sendmail 2.安裝mail命令 yum -y install mailx 3.開啟sendmail /etc/rc.d/init.d/sendmail start 4.設定開機啟動 vim /etc/rc.local 最後一行新增上: