完整邮件发送类_PHP教程
完整邮件发送类
class smtp
{
/* Public Variables */
var $smtp_port;
var $time_out;
var $host_name;
var $log_file;
var $relay_host;
var $debug;
var $auth;
var $user;
var $pass;
/* Private Variables */
var $sock;
/* Constractor */
function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)
{
$this->debug = FALSE;
$this->smtp_port = $smtp_port;
$this->relay_host = $relay_host;
$this->time_out = 30; //is used in fsockopen()
$this->auth = $auth;//auth
$this->user = $user;
$this->pass = $pass;
$this->host_name = "localhost"; //is used in HELO command
$this->log_file = "";
$this->sock = FALSE;
}
/* Main Function */
function sendmail($to, $from ,$fromname, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = ""){
$mail_from = $this->get_address($this->strip_comment($from));
$body = ereg_replace("(^|(rn))(.)", "1.3", $body);
$header .= "MIME-Version:1.0rn";
if($mailtype=="HTML"){
$header .= "Content-Type:text/html;charset=utf-8rn";
}
$header .= "To: ".$to."rn";
if ($cc != ""){
$header .= "Cc: ".$cc."rn";
}
$header .= "From: $fromnamern";
$header .= "Subject: ".$subject."rn";
$header .= $additional_headers;
$header .= "Date: ".date("r")."rn";
$header .= "X-Mailer:By Redhat (PHP/".php教程version().")rn";
list($msec, $sec) = explode(" ", microtime());
$header .= "Message-ID: rn";
$TO = explode(",", $this->strip_comment($to));
if ($cc != ""){
$TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
}
if ($bcc != ""){
$TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
}
$sent = TRUE;
foreach ($TO as $rcpt_to){
$rcpt_to = $this->get_address($rcpt_to);
if (!$this->smtp_sockopen($rcpt_to)){
$this->log_write("Error: Cannot send email to ".$rcpt_to."n");
$sent = FALSE;
continue;
}
if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)){
$this->log_write("E-mail has been sent to n");
}else{
$this->log_write("Error: Cannot send email to n");
$sent = FALSE;
}
fclose($this->sock);
$this->log_write("Disconnected from remote hostn");
}
return $sent;
}
/* Private Functions */
function smtp_send($helo, $from, $to, $header, $body = ""){
if (!$this->smtp_putcmd("HELO", $helo)){
return $this->smtp_error("sending HELO command");
}
#auth
if($this->auth){
if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))){
return $this->smtp_error("sending HELO command");
}
if (!$this->smtp_putcmd("", base64_encode($this->pass))){
return $this->smtp_error("sending HELO command");
}
}
if (!$this->smtp_putcmd("MAIL", "FROM:")){
return $this->smtp_error("sending MAIL FROM command");
}
if (!$this->smtp_putcmd("RCPT", "TO:")){
return $this->smtp_error("sending RCPT TO command");
}
if (!$this->smtp_putcmd("DATA")){
return $this->smtp_error("sending DATA command");
}
if (!$this->smtp_message($header, $body)){
return $this->smtp_error("sending message");
}
if (!$this->smtp_eom()){
return $this->smtp_error("sending
}
if (!$this->smtp_putcmd("QUIT")){
return $this->smtp_error("sending QUIT command");
}
return TRUE;
}
function smtp_sockopen($address){
if ($this->relay_host == ""){
return $this->smtp_sockopen_mx($address);
}
else{
return $this->smtp_sockopen_relay();
}
}
function smtp_sockopen_relay(){
$this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."n");
$this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
if (!($this->sock && $this->smtp_ok())){
$this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."n");
$this->log_write("Error: ".$errstr." (".$errno.")n");
return FALSE;
}
$this->log_write("Connected to relay host ".$this->relay_host."n");
return TRUE;;
}
function smtp_sockopen_mx($address){
$domain = ereg_replace("^.+@([^@]+)$", "1", $address);
if (!@getmxrr($domain, $MXHOSTS)){
$this->log_write("Error: Cannot resolve MX "".$domain.""n");
return FALSE;
}
foreach ($MXHOSTS as $host){
$this->log_write("Trying to ".$host.":".$this->smtp_port."n");
$this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
if (!($this->sock && $this->smtp_ok())){
$this->log_write("Warning: Cannot connect to mx host ".$host."n");
$this->log_write("Error: ".$errstr." (".$errno.")n");
continue;
}
$this->log_write("Connected to mx host ".$host."n");
return TRUE;
}
$this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")n");
return FALSE;
}
function smtp_message($header, $body){
fputs($this->sock, $header."rn".$body);
$this->smtp_debug("> ".str_replace("rn", "n"."> ", $header."n> ".$body."n> "));
return TRUE;
}
function smtp_eom(){
fputs($this->sock, "rn.rn");
$this->smtp_debug(". [EOM]n");
return $this->smtp_ok();
}
function smtp_ok(){
$response = str_replace("rn", "", fgets($this->sock, 512));
$this->smtp_debug($response."n");
if (!ereg("^[23]", $response)){
fputs($this->sock, "QUITrn");
fgets($this->sock, 512);
$this->log_write("Error: Remote host returned "".$response.""n");
return FALSE;
}
return TRUE;
}
function smtp_putcmd($cmd, $arg = ""){
if($arg != ""){
if($cmd==""){
$cmd = $arg;
}
else{
$cmd = $cmd." ".$arg;
}
}
fputs($this->sock, $cmd."rn");
$this->smtp_debug("> ".$cmd."n");
return $this->smtp_ok();
}
function smtp_error($string){
$this->log_write("Error: Error occurred while ".$string.".n");
return FALSE;
}
function log_write($message){
$this->smtp_debug($message);
if ($this->log_file == ""){
return TRUE;
}
$message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))){
$this->smtp_debug("Warning: Cannot open log file "".$this->log_file.""n");
return FALSE;;
}
flock($fp, LOCK_EX);
fputs($fp, $message);
fclose($fp);
return TRUE;
}
function strip_comment($address){//去除"()"
$comment = "([^()]*)";
while (ereg($comment, $address)){
$address = ereg_replace($comment, "", $address);
}
return $address;
}
function get_address($address){
$address = ereg_replace("([trn])+", "", $address);//t 制表符 r 回车符 n 换行符 +匹配前面的子表达式一次或多次
$address = ereg_replace("^.*.*$", "1", $address);
return $address;
}
function smtp_debug($message){
if ($this->debug){
echo $message;
}
}
}

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

Windows11的控制台中是否缺少Outlook郵件圖示?這一意外情況在一些依賴OutlookMail滿足通訊需求的個人中引起了困惑和擔憂。為什麼我的Outlook電子郵件沒有顯示在控制台中?控制台中沒有Outlook郵件圖示可能有幾個可能的原因:Outlook未正確安裝。從MicrosoftStore安裝Office應用程式不會將郵件小程式新增至控制台。 mlcfg32.cpl檔案在控制面板中的位置遺失。登錄中的mlcfg32.cpl檔案路徑不正確。作業系統目前未配置為運行此應用程式

如果您發現在使用Word列印郵件合併文件時出現空白頁,這篇文章將對您有所幫助。郵件合併是一項便捷的功能,讓您能夠輕鬆建立個人化文件並傳送給多個收件者。在MicrosoftWord中,郵件合併功能備受推崇,因為它能夠幫助使用者節省手動為每個收件者複製相同內容的時間。為了列印郵件合併文檔,您可以轉到郵件標籤。但有些Word使用者反映,在嘗試列印郵件合併文件時,印表機會列印空白頁或完全不列印。這可能是由於格式設定不正確或印表機設定問題。嘗試檢查文檔和印表機設置,確保列印前預覽文檔,以確保內容正確。如果

PHP非同步發送郵件:避免長時間等待郵件發送完成。導言:在Web開發中,發送郵件是常見的功能之一。但是,由於郵件發送需要與伺服器進行通信,往往會導致用戶在等待郵件發送完成的過程中出現長時間的等待。為了解決這個問題,我們可以使用PHP非同步發送郵件的方式來優化使用者體驗。本文將介紹如何透過具體的程式碼範例實現PHP非同步發送郵件,並避免長時間的等待。一、理解異步發送郵件

在抖音上,使用者不僅可以分享自己的生活點滴和才藝,還可以和其他使用者互動交流。在這個過程中,有時候我們需要向其他用戶發送文件,例如圖片、影片等。那麼,在抖音上如何發給別人文件呢?一、抖音上如何發給別人文件? 1.開啟抖音,進入你想要傳送檔案的聊天介面。 2.點選聊天介面中的「+」號,選擇「檔案」。 3.在檔案選項中,你可以選擇傳送圖片、影片、音訊等檔案。選擇你想要發送的文件後,點擊「發送」。 4.等待對方接受你的文件,一旦對方接受,文件就會傳輸成功。二、抖音上發給別人檔案怎麼刪除? 1.打開抖音,進入你發送文

WhatsApp推出了一個新選項,允許用戶透過訊息平台以高解析度發送照片和影片。繼續閱讀以了解它是如何完成的。 WhatsApp發布了一個更新,允許iPhone和Android用戶以高分辨率發送照片和視頻,最終解決了該服務的低品質媒體共享限制。該選項稱為“高清品質”,意味著用戶可以以最小的壓縮發送更清晰的照片和影片。例如,在iPhone上捕獲的圖像現在可以以3024x4032分辨率發送,而不是以前的最大920x1280分辨率,而視頻可以以1280×718分辨率發送,而不是848×476分辨率。

PHP郵件追蹤功能:了解使用者對郵件的行為和回饋在現代社會中,電子郵件已成為人們日常生活和工作中不可或缺的一部分。對企業來說,發送郵件是與客戶溝通、推廣產品或服務的重要方式之一。然而,一封郵件被發送出去後,我們如何知道它是否被收到、被讀取,或者用戶對郵件內容有何反應?這時,郵件追蹤功能就顯得格外重要了。郵件追蹤功能可以幫助我們了解使用者對郵件的行為和回饋

什麼是即時語音郵件轉錄?即時語音郵件轉錄是iOS16中引入的一項創新功能,可讓iPhone用戶在離開語音郵件時查看語音郵件的即時轉錄。此功能利用先進的語音識別技術將口語轉換為文本,提供了一種方便且易於訪問的方式來了解最新消息,而無需完全收聽它們。使用即時語音郵件轉錄的好處即時語音郵件轉錄為iPhone用戶提供了幾個優勢:提高工作效率:透過提供即時轉錄,即時語音郵件轉錄消除了收聽整個語音郵件的需要,從而節省了用戶的時間和精力。這允許用戶快速掃描語音郵件的內容並確定其回應的優先順序。聽力受損使用者的可訪

每年Apple發布新的iOS和macOS大版本之前,用戶都可以提前幾個月下載測試版搶先體驗一番。由於大眾和開發人員都使用該軟體,蘋果公司為兩者推出了developer和public版即開發者測試版的公共測試版。 iOS的developer版和public版有什麼差別呢?從字面上的意思來說,developer版是開發者測試版,public版是公共測試版。 developer版和public版面向的物件不同。 developer版是蘋果公司給開發者測試使用的,需要蘋果開發者帳號才能收到下載併升級,是
