強大的PHP發郵件類

2021-06-06 15:31:56 字數 3753 閱讀 9457

<?php

class smtp

function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")

$header .= "to: ".$to."\r\n";

if ($cc != "")

$header .= "from: $from<".$from.">\r\n";

$header .= "subject: ".$subject."\r\n";

$header .= $additional_headers;

$header .= "date: ".date("r")."\r\n";

$header .= "x-mailer:by redhat (php/".phpversion().")\r\n";

list($msec, $sec) = explode(" ", microtime());

$header .= "message-id: <".date("ymdhis", $sec).".".($msec*1000000).".".$mail_from.">\r\n";

$to = explode(",", $this->strip_comment($to));

if ($cc != "")

if ($bcc != "")

$sent = true;

foreach ($to as $rcpt_to)

if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) else

fclose($this->sock);

$this->log_write("disconnected from remote host\n");

}echo "

";echo $header;

return $sent;

}/* private functions */

function smtp_send($helo, $from, $to, $header, $body = "")

#auth

if($this->auth)

if (!$this->smtp_putcmd("", base64_encode($this->pass))) }#

if (!$this->smtp_putcmd("mail", "from:<".$from.">"))

if (!$this->smtp_putcmd("rcpt", "to:<".$to.">"))

if (!$this->smtp_putcmd("data"))

if (!$this->smtp_message($header, $body))

if (!$this->smtp_eom())

if (!$this->smtp_putcmd("quit"))

return true;

}function smtp_sockopen($address)

else

}function smtp_sockopen_relay()

$this->log_write("connected to relay host ".$this->relay_host."\n");

return true;;

}function smtp_sockopen_mx($address)

foreach ($mxhosts as $host)

$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)

function smtp_eom()

function smtp_ok()

return true;

}function smtp_putcmd($cmd, $arg = "")

fputs($this->sock, $cmd."\r\n");

$this->smtp_debug("> ".$cmd."\n");

return $this->smtp_ok();

}function smtp_error($string)

function log_write($message)

$message = date("m d h:i:s ").get_current_user()."[".getmypid()."]: ".$message;

if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a")))

flock($fp, lock_ex);

fputs($fp, $message);

fclose($fp);

return true;

}function strip_comment($address)

return $address;

}function get_address($address)

function smtp_debug($message)

}function get_attach_type($image_tag)

return $filedata;}}

/***例項化郵件類

*/$smtpserver = "smtp.163.com"; //smtp伺服器

$smtpserverport =25; //smtp伺服器端口

$smtpusermail = "[email protected]"; //smtp伺服器的使用者郵箱

$smtpemailto = "[email protected]"; //傳送給誰

$smtpuser = "[email protected]"; //smtp伺服器的使用者帳號

$smtppass = "123147mcl"; //smtp伺服器的使用者密碼

$mailsubject = "php100測試郵件系統"; //郵件主題

$mailbody = ""; //郵件內容

$mailtype = "html"; //郵件格式(html/txt),txt為文字郵件

$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);

$smtp->debug = true; //是否顯示傳送的除錯資訊

$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);

?>

使用php發郵件二(發郵件流程)

傳送郵件過程 1 配置好你的郵箱服務 qq郵箱為例 2 使用socket連線,建立乙個套接字 fp fsockopen hostname,port,errno,errmsg,30 3 向對方郵件伺服器發出的標識自己的身份的命令fputs fp,ehlo ki r n 4 即將進行身份認證fputs ...

使用php發郵件三(郵件找回密碼)

11 組裝郵件並傳送郵件 12 傳送完畢,跳轉 接收到郵件點解郵件後 1 通過auth查詢user 2 判斷驗證鏈結時間是否過期 通過儲存的authtime與當前時間比較 3 將修改表set authvalue auth authtime 0 password authvalue 4 列印修改資訊 ...

PHP下使用Pear發郵件

執行pear.bat直接回車 不用管直接自動安裝完成 再執行pear install mail pear install net smtp 重啟apache 測試 require once mail.php body from gbk?b?base64 encode to gbk?b?base64 ...