php實現傳送郵件

2021-09-07 02:19:09 字數 3750 閱讀 1987

smtp.php:

<?php

class smtp

/* main function */

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

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

if ($cc != "")  

$header .= "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: \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");

} 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:"))  

if (!$this->smtp_putcmd("rcpt", "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 = "")

else }

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

} ?>

test.php:

<?php

//引入傳送郵件類

require("smtp.php");

//使用163郵箱伺服器

$smtpserver = "smtp.163.com";

//163郵箱伺服器端口

$smtpserverport = 25;

//你的163伺服器郵箱賬號

$smtpusermail = "*********@163.com";

//收件人郵箱

$smtpemailto = "*********[email protected]";

//你的郵箱賬號(去掉@163.com)

$smtpuser = "zhanghao";//smtp伺服器的使用者帳號

//你的郵箱密碼

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

//郵件主題

$mailsubject = "測試郵件傳送";

//郵件內容

$mailbody = "你好,這是lsrc checked 的漏洞,請查收!";

//郵件格式(html/txt),txt為文字郵件

$mailtype = "txt";

//這裡面的乙個true是表示使用身份驗證,否則不使用身份驗證.

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

//是否顯示傳送的除錯資訊

$smtp->debug = true;

//傳送郵件

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

?>

執行截圖:

PHP傳送郵件功能實現

第一步 我用的是163郵箱傳送郵件,做乙個嘗試,在嘗試之前,需要要開啟163郵箱的授權碼如圖所示,請記住您的授權碼,將在之後的步驟中用到 第二步 第三步咱們該寫 了 html 對應的controller的php public functionchangepwd 傳送郵件來修改密碼 this disp...

PHPMailer實現PHP郵件傳送

2.解壓 從中取出class.phpmailer.php 和 class.smtp.php 放到你的專案的資料夾,因為我們等下會引用到它們.3.建立傳送郵件的函式,其中你需要配置smtp伺服器 function postmail to,subject body else 4.使用函式 postmai...

php郵件傳送

今天要用到php的郵件傳送,便用phpmailer弄了個 類檔案 emaillclass.php class phpmailer function send addr,fromname,title,content smtp array array ehlo hello lb,220,250 ehlo...