<?php class SendM{ private $Mailhost,$Mailuser,$Mailpwd,$Mailport,$Mailtimeout,$ms,$ending = "\r\n",$endingc="\n"; function __construct($Mailhost,$Mailuser,$Mailpwd,$Mailport,$Mailtimeout){ $this->Mailhost=$Mailhost; $this->Mailuser=$Mailuser; $this->Mailpwd=$Mailpwd; $this->Mailport=$Mailport; $this->Mailtimeout=$Mailtimeout; $this->ConnectSmtpServer(); } private function ConnectSmtpServer(){ if(!is_string($this->Mailhost)){ settype(trim($this->Mailhost),"string"); } if(!is_integer($this->Mailport)){ settype(trim($this->Mailport),"integer"); } if(!is_integer($this->Mailtimeout)){ settype(trim($this->Mailtimeout),"integer"); } $this->ms=@fsockopen($this->Mailhost,$this->Mailport,$this->errorno,$this->errorstr,$this->Mailtimeout); if(substr(PHP_OS,0,3) != "WIN"){ stream_set_timeout($this->ms, $this->Mailtimeout, 0);} $rcp = $this->get_echo(); fputs($this->ms,"ehlo bobo".$this->ending); $rcp = $this->get_echo(); if(substr($rcp,0,3)!='250'){ return false; } fputs($this->ms,'auth login'.$this->ending); $rcp = $this->get_echo(); if(substr($rcp,0,3)=='334'){ $this->Auth($this->Mailuser,$this->Mailpwd); }else{ return false; } } private function Auth($Mailuser,$Mailpwd){ $this->Mailuseren=base64_encode($Mailuser); $this->Mailpwden=base64_encode($Mailpwd); fputs($this->ms,$this->Mailuseren.$this->ending); $rcp = $this->get_echo(); fputs($this->ms,$this->Mailpwden.$this->ending); $rcp = $this->get_echo(); } private function get_echo(){ $edata=""; while($estr=@fgets($this->ms,600)){ $edata .= $estr; if(substr($estr,3,1) == " ") { break; } } return $edata; } public function Send($to,$subject,$connect){ $host=explode('.',$this->Mailhost); $fromaddress=$this->Mailuser.'@'.$host[1].'.'.$host[2]; fputs($this->ms,'mail from:<'.$fromaddress.'>'.$this->ending); $rcp = $this->get_echo(); fputs($this->ms,'rcpt to:<'.$to.'>'.$this->ending); $rcp = $this->get_echo(); fputs($this->ms,'data'.$this->ending); $rcp = $this->get_echo(); fputs($this->ms,"to:$to".$this->endingc); fputs($this->ms,"from:$fromaddress".$this->endingc); fputs($this->ms,"subject:$subject".$this->endingc.$this->endingc); fputs($this->ms,"$connect".$this->endingc); fputs($this->ms,'.'.$this->ending); $rcp = $this->get_echo(); if(substr($rcp,0,3)=='250'){header("Location:main_pro.php?act=msg&errors=on&msg=邮件发送成功!已成功提交至对方服务器!"); }else{ header("Location:main_pro.php?act=msg&errors=on&msg=很遗憾,邮件发送失败了!请检查邮件账户配置是否正确!"); } } } ?>
這是一個發送電子郵件的php類,需要的朋友可以下載使用。
使用說明:
$m= new SendM('smtp伺服器位址','帳號','密碼',連接埠(int),逾時重試時間(int));
$m->Send('收件者信箱','主題','郵件正文內容');
使用範例:
$m= new SendM ('smtp.yeah.net','testuser','testuserpwd',25,30);
$m->Send('a@coolmr.com ','測試郵件','這是一封郵件發送類別的測試郵件,謝謝您的支援');
本站所有資源皆由網友貢獻或各大下載網站轉載。請自行檢查軟體的完整性!本站所有資源僅供學習參考。請不要將它們用於商業目的。否則,一切後果都由您負責!如有侵權,請聯絡我們刪除。聯絡方式:admin@php.cn
相關文章
17Jul2017
本文以下面這個類別庫做為講解《強大的php發送郵件類別》點擊標題可以去下該類別庫。如果你需要查看更多的php類別庫可以點下面的連結:http://www.php.cn/xiazai/leiku
24Nov2017
我相信很多同學都用過thinkphp,而thinkphp這個框架本身也有類別庫,這篇文章我們來講講thinkphp怎麼使用外部PHPMailer類別庫。
15Mar2019
JavaMail API支援用於傳送電子郵件的JavaMail(javax.mail)介面。 javax.mail具有發送電子郵件和SMTP,POP3和IMAP等其他郵件協定所需的所有類別。
18Jun2023
PHP是一種廣泛使用的伺服器端腳本語言,在開發Web應用程式時經常使用。它可以輕易地發送和接收電子郵件,這讓開發者可以快速建立自己的郵件系統。在本文中,我們將探討如何使用PHP實作郵件發送和接收的方法。一、發送電子郵件PHP提供了許多發送電子郵件的函數,最常用的是使用SMTP伺服器發送電子郵件的PHPMailer類別。這個類別是使用PHP編寫的開源程式庫,具有廣泛的