-
-
include_once("class.phpmailer.php");
- /**
- * 定義郵件模組配製資訊
- */
- define("SMTP_HOST","smtp.mail.yahoo.com"); // SMTP 主機
- define("SMTP_MAIL"," XXXX@yahoo.cn"); // SMTP 使用者email
- define ("SMTP_PASS"," XXXX"); // SMTP 用的密碼
define("SERVICE_MAIL"," XXXX@yahoo.cn"); // SMTP 使用者email
- define ("SERVICE_NAME","PHPBOOK郵件測試"); // SMTP 用的名字
/**
- * 使用phpmailer發送郵件模組
- *
- * @param string $email
- * @param string $user
- * @param string $subject
- * @param string $body
- * @return bool
- */
- function sendMail($email,$user,$subject ,$body)
- {
- $mail = new PHPMailer();
- //$this;
- $mail->IsSMTP(); // 設定使用SMTP
- $mail-> Host = SMTP_HOST; // 設定SMTP伺服器位址
- $mail->SMTPAuth = true; // 設定SMTP權限驗證
- $mail->Username = SMTP_MAIL; // SMTP 使用者名稱
- $mail-> Password = SMTP_PASS; // SMTP 伺服器密碼
$mail->From = SERVICE_MAIL; // 設定發送者位址
- $mail->FromName = SERVICE_NAME; // 設定發送者名稱
- $mail->AddAddress($email, $user); // 新增接收者位址
- $mail->AddReplyTo(SERVICE_MAIL, SERVICE_NAME); // 設定回覆位址
$mail->WordWrap = 50; // 設定顯示格式
- $mail->IsHTML(true); // 設定郵件支援html
- $mail->Subject = $subject;
- $mail- >Body = $body;
- $mail->AltBody = ""; // 文字類型的郵件
if(!$mail->Send())
- {
- return $mail->ErrorInfo;
- }
- return true;
- }
//開始傳送測試郵件ng: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /var/www/xiehui/admin/mail/class.smtp.php on line 89
- $tomail = " XXXX@126.com";
- user = = =d " XXXXlinux";
- $_mailSubject = "郵件測試範例!"; // 發給使用者的郵件標題小組
- $_mailBody = "新浪網" ; // 郵件內容小組
- sendMail($tomail,$user,$_mailSubject,$_mailBody);
- ?>
-
複製代碼
複製代碼複製代碼複製代碼複製代碼複製代碼複製代碼複製代碼複製代碼 實驗證明yahoo的smtp很好用,號稱sina的其實並不好用,我卡在著好長時間。
方法四,給予socket所寫的程序
使用socket發送郵件的封裝類別:
-
-
class sendmail{
- var $lastmessage; //記錄最後回傳的回應訊息
- var $lastact; //最後的動作,字串形式
- var $welcome; //用在HELO後面,歡迎使用者
- var $debug; //是否顯示偵錯資訊
- var $smtp; //smtp伺服器
- var $ port; //smtp連接埠號碼
- var $fp; //socket句柄
- //傳送郵件函數
- function send_mail($smtp, $welcome="", $debug=false) {
- if(empty($smtp)) die("SMTP不能為空!");
- $this->smtp=$smtp;
- if(empty($welcome)) {
- $this->welcome=gethostbyaddr("localhost");
- }else
- $this->welcome=$welcome;
- $this->debug=$debug;
- $this->lastmessage="";
- $this->lastact=" ";
- $ this->port="25";
- }
- // 顯示偵錯訊息
- function show_debug($message, $inout) {
- if ($this-> ;debug) {
- if($inout=="in"){ //回應訊息
- $m=' }else
- $m='>> ';
- if(!ereg ( "n$", $message))
- $message .= "
";
- $message=nl2br($message);
- echo "${m } ${message}";
- }
- }
- // 執行提交的命令
- function do_command($command, $code) {
- $this->lastact= $ command;
- $this->show_debug($this->lastact, "out");
- fputs ( $this->fp , $this->lastact );
- $this->lastmessage = fgets ( $this->fp, 512 );
- $this->show_debug($this->lastmessage, "in ");
- if(!ereg("^$code", $this->lastmessage ) )
- return false;
- else
- return true;
- }
- //郵件發送處理
- function send( $to,$from,$subject,$message) {
- // 連線伺服器
- $this->lastact="connect";
- $this-> ;show_debug("連線到SMTP 伺服器: ".$this->smtp, "out");
- $this->fp = fsockopen ( $this->smtp, $this->port );
- if ( $this->fp ) {
- $this->set_socket_blocking( $this->fp, true );
- $this->lastmessage=fgets($this->; fp,512) ;
- $this->show_debug($this->lastmessage, "in");
- if (!ereg ( "^220", $this->lastmessage ) ) {
- return false;
- }else{
- $this->lastact="HELO " . "n";
- if(!$this->do_command($this->lastact, "250")){
- fclose($this->fp);
- return false;
- }
- $this->lastact="郵件寄件者:$from" 。 "n";
- if(!$this->do_command($this->lastact, "250")){
- fclose($this->fp);
- return false;
- }
- $this->lastact="RCPT TO: $to" 。 "n";
- if(!$this->do_command($this->lastact, "250")){
- fclose($this->fp);
- return false;
- }
- //開始傳送郵件正文
- $this->lastact="DATAn";
- if(!$this->do_command($this->lastact, "354") ){
- fclose ($this->fp);
- return false;
- }
- //開始處理郵件主題頭
- $head="Subject: $subjectn";
- if(!empty($ subject) && !ereg($head, $message)){
- $message = $head.$message;
- }
- //開始處理郵件Fromhead
- $head="From: $fromn ";
- if(!empty($from) && !ereg($head, $message)) {
- $message = $head.$message;
- }
- //開始處理郵件Tohead
- $head="To: $ton";
- if(!empty($to) && !ereg($head, $message)) {
- $message = $head.$message;
- }
- //處理結束字串
- if(!ereg("n.n", $message))
- $message .= "n.n";
- $ this->show_debug($message, " out");
- fputs($this->fp, $message);
- $this->lastact="QUITn";
- if(! $this->do_command($this->lastact, "250")){
- fclose($this->fp);
- 回傳false;
- }
- }
- 回傳true;
- }else{
- $this- >show_debug("連接失敗!", "in");
- return false;
- }
- }
- }
- ?>
複製代碼
使用socket發送郵件範例:
-
-
include ("./sendmail.class.php");
- $mail = new sendmail();
- $email = "您好,這是一個測試郵件!";
- $sendmail = new send_mail("smtp.mail.126.com","PHPBOOK",true); //顯示調示訊息
- if($mail->send("XXXX@126.com", "XXXX@126.com", "測試SOCKET郵件", $email)) {
- echo "發送成功!
";
- }else{
- echo "發送失敗!
";
- }
- ?>
複製程式碼
|
複製程式碼