首頁 PHP 函式庫 其它類別庫 發送電子郵件的php類
發送電子郵件的php類
<?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

相關文章

PHP發送電子郵件類 PHP發送電子郵件類

25 Jul 2016

PHP發送電子郵件類

發送帶有PHP的電子郵件 發送帶有PHP的電子郵件

02 Mar 2025

核心要點 PHP 提供了一種簡單有效的方法來發送電子郵件,包括基本的純文本郵件、HTML 郵件和帶有附件的郵件。 PHP 的 mail() 函數用於發送電子郵件。對於簡單的郵件,它只需要三個參數:收件人的地址、主題和郵件正文。 發送 HTML 郵件或帶有附件的郵件時,需要使用 MIME 標準將郵件分解成多個部分,並用選定的邊界分隔。每個部分都應定義內容是什麼、如何編碼、可能的內容處置方式,以及內容本身。 使用 PHPMailer 庫可以增強 PHP 發送郵件的功能,該庫允許連接 SMTP 服務

如何解決 PHP Mail() 和 PHPMailer 的電子郵件傳送問題? 如何解決 PHP Mail() 和 PHPMailer 的電子郵件傳送問題?

22 Oct 2024

本文提供了使用 PHP mail() 或 PHPMailer 發送電子郵件時遇到的問題的偵錯解決方案。它解決了與 mail() 函數和缺少 PHPMailer 類別導入相關的錯誤。文章建議啟用 SMTP debuggin

發送郵件的PHP類 發送郵件的PHP類

25 Jul 2016

發送郵件的PHP類

PHP發送電子郵件程式碼 PHP發送電子郵件程式碼

25 Jul 2016

PHP發送電子郵件程式碼

自動發送電子郵件 自動發送電子郵件

07 Nov 2024

?很高興分享我的專案—自動電子郵件發送系統? 我開發了一種工具,透過處理從客製化訊息到大規模分發的所有內容,可以更快、更有效率地發送電子郵件。 ?無論是用於行銷、提醒或

See all articles