php兩個非常好用的mail,php三大特性

本文目錄一覽:

如何使用php的mail函數

可以,但是這要需要smtp.163.com的用戶名與密碼

我給你一個郵件發送類,你可以直接用

/*

名稱:smtpsend.class.php

功能:郵件發送類,實現郵件發送功能 ,可以發送html郵件;可驗證用戶是否具有發送許可權;可實現從任意伺服器郵箱發送郵件,並非局限於本機。

時間:2008-03

author:三生石

$smtpserver; SMTP伺服器

$smtpserverport; SMTP伺服器埠

$smtpuser; SMTP伺服器的用戶帳號

$smtppass; SMTP伺服器的用戶密碼

$fromMail; SMTP伺服器的用戶郵箱

$toMail; 發送給誰

$subject; 郵件主題

$content; 郵件內容

$mailtype; 郵件格式(HTML/TXT),TXT為文本郵件

*/

class smtpclass

{

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 = 60; //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, $subject = “”, $body = “”, $mailtype, $returnpath = “”, $cc = “”, $bcc = “”, $additional_headers = “”)

{

$mail_from = $this-get_address($this-strip_comment($from));

$body = ereg_replace(“(^|(\r\n))(\.)”, “\1.\3”, $body);

$header = “”;

if(isset($returnpath) $returnpath != “”)

{

$header .= “Reply-To:”.$returnpath.”\r\n”;

}

$header .= “MIME-Version:1.0\r\n”;

if($mailtype==”HTML”){

$header .= ‘Content-Type:text/html; charset=gb2312’ . “\r\n”;

}

$header .= “To: “.$to.”\r\n”;

if ($cc != “”) {

$header .= “Cc: “.$cc.”\r\n”;

}

$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 != “”) {

$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 “.$rcpt_to.”\n”);

} else {

$this-log_write(“Error: Cannot send email to “.$rcpt_to.”\n”);

$sent = FALSE;

}

fclose($this-sock);

$this-log_write(“Disconnected from remote host\n”);

}

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:”.$from.””)) {

return $this-smtp_error(“sending MAIL FROM command”);

}

if (!$this-smtp_putcmd(“RCPT”, “TO:”.$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 CRLF.CRLF [EOM]”);

}

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.”\r\n”.$body);

$this-smtp_debug(” “.str_replace(“\r\n”, “\n”.” “, $header.”\n “.$body.”\n “));

return TRUE;

}

function smtp_eom()

{

fputs($this-sock, “\r\n.\r\n”);

$this-smtp_debug(“. [EOM]\n”);

return $this-smtp_ok();

}

function smtp_ok()

{

$response = str_replace(“\r\n”, “”, fgets($this-sock, 512));

$this-smtp_debug($response.”\n”);

if (!ereg(“^[23]”, $response)) {

fputs($this-sock, “QUIT\r\n”);

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.”\r\n”);

$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(“([ \t\r\n])+”, “”, $address);

$address = ereg_replace(“^.*(.+).*$”, “\1”, $address);

return $address;

}

function smtp_debug($message)

{

if ($this-debug) {

echo $message;

}

}

function senduserMail($smtpserver,$smtpserverport=25,$smtpuser,$smtppass,$fromMail,$toMail,$subject,$content,$mailtype=’HTML’)

{

if ($fromMail == “”) {

die(“發件人不能為空!”);

}

if ($smtpuser == “”) {

die(“用戶名不能為空!”);

}

if ($smtppass == “”) {

die(“用戶密碼不能為空!”);

}

if ($subject == “”) {

die(“標題不能為空!”);

}

if ($toMail == “”) {

die(“收件人不能為空!”);

}

if ($content == “”) {

die(“內容不能為空!”);

}

$smtpserver = $smtpserver;//SMTP伺服器

$smtpserverport = $smtpserverport;//SMTP伺服器埠

$smtpuser = $smtpuser;//SMTP伺服器的用戶帳號

$smtppass = $smtppass;//SMTP伺服器的用戶密碼

$smtpusermail = $fromMail;//SMTP伺服器的用戶郵箱

$smtpemailto = $toMail;//發送給誰

$mailsubject = $subject;//郵件主題

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

$mailtype = $mailtype;//郵件格式(HTML/TXT),TXT為文本郵件

$this-smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//這裡面的一個true是表示使用身份驗證,否則不使用身份驗證.

//$smtp-debug = TRUE;//是否顯示發送的調試信息

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

}

}

//$gao = new smtpclass();

//$gao-senduserMail(“mail.163.com”,25,”用戶名”,”密碼”,”blog.admin@bnet.com.cn”,”sss@zdnet.com.cn”,”測試”,”測試一下”,”TXT”);

用PHP發送電子郵件的方法?

?

/*

* 郵件發送類

*/

class smail {

//您的SMTP 伺服器供應商,可以是域名或IP地址

var $smtp = “”;

//SMTP需要要身份驗證設值為 1 不需要身份驗證值為 0,現在大多數的SMTP服務商都要驗證,如不清楚請與你的smtp 服務商聯繫。

var $check = 1;

//您的email帳號名稱

var $username = “”;

//您的email密碼

var $password = “”;

//此email 必需是發信伺服器上的email

var $s_from = “”;

/*

* 功能:發信初始化設置

* $from 你的發信伺服器上的郵箱

* $password 你的郵箱密碼

* $smtp 您的SMTP 伺服器供應商,可以是域名或IP地址

* $check SMTP需要要身份驗證設值為 1 不需要身份驗證值為 0,現在大多數的SMTP服務商都要驗證

*/

function smail ( $from, $password, $smtp, $check = 1 ) {

if( preg_match(“/^[^\d\-_][\w\-]*[^\-_]@[^\-][a-zA-Z\d\-]+[^\-](\.[^\-][a-zA-Z\d\-]*[^\-])*\.[a-zA-Z]{2,3}/”, $from ) ) {

$this-username = substr( $from, 0, strpos( $from , “@” ) );

$this-password = $password;

$this-smtp = $smtp ? $smtp : $this-smtp;

$this-check = $check;

$this-s_from = $from;

}

}

/*

* 功能:發送郵件

* $to 目標郵箱

* $from 來源郵箱

* $subject 郵件標題

* $message 郵件內容

*/

function send ( $to, $from, $subject, $message ) {

//連接伺服器

$fp = fsockopen ( $this-smtp, 25, $errno, $errstr, 60);

if (!$fp ) return “聯接伺服器失敗”.__LINE__;

set_socket_blocking($fp, true );

$lastmessage=fgets($fp,512);

if ( substr($lastmessage,0,3) != 220 ) return “錯誤信息1:$lastmessage”.__LINE__;

//HELO

$yourname = “YOURNAME”;

if($this-check == “1”) $lastact=”EHLO “.$yourname.”\r\n”;

else $lastact=”HELO “.$yourname.”\r\n”;

fputs($fp, $lastact);

$lastmessage == fgets($fp,512);

if (substr($lastmessage,0,3) != 220 ) return “錯誤信息2:$lastmessage”.__LINE__;

while (true) {

$lastmessage = fgets($fp,512);

if ( (substr($lastmessage,3,1) != “-“) or (empty($lastmessage)) )

break;

}

//身份驗證

if ($this-check==”1″) {

//驗證開始

$lastact=”AUTH LOGIN”.”\r\n”;

fputs( $fp, $lastact);

$lastmessage = fgets ($fp,512);

if (substr($lastmessage,0,3) != 334) return “錯誤信息3:$lastmessage”.__LINE__;

//用戶姓名

$lastact=base64_encode($this-username).”\r\n”;

fputs( $fp, $lastact);

$lastmessage = fgets ($fp,512);

if (substr($lastmessage,0,3) != 334) return “錯誤信息4:$lastmessage”.__LINE__;

//用戶密碼

$lastact=base64_encode($this-password).”\r\n”;

fputs( $fp, $lastact);

$lastmessage = fgets ($fp,512);

if (substr($lastmessage,0,3) != “235”) return “錯誤信息5:$lastmessage”.__LINE__;

}

//FROM:

$lastact=”MAIL FROM: “. $this-s_from . “\r\n”;

fputs( $fp, $lastact);

$lastmessage = fgets ($fp,512);

if (substr($lastmessage,0,3) != 250) return “錯誤信息6:$lastmessage”.__LINE__;

//TO:

$lastact=”RCPT TO: “. $to .” \r\n”;

fputs( $fp, $lastact);

$lastmessage = fgets ($fp,512);

if (substr($lastmessage,0,3) != 250) return “錯誤信息7:$lastmessage”.__LINE__;

//DATA

$lastact=”DATA\r\n”;

fputs($fp, $lastact);

$lastmessage = fgets ($fp,512);

if (substr($lastmessage,0,3) != 354) return “錯誤信息8:$lastmessage”.__LINE__;

//處理Subject頭

$head=”Subject: $subject\r\n”;

$message = $head.”\r\n”.$message;

//處理From頭

$head=”From: $from\r\n”;

$message = $head.$message;

//處理To頭

$head=”To: $to\r\n”;

$message = $head.$message;

//加上結束串

$message .= “\r\n.\r\n”;

//發送信息

fputs($fp, $message);

$lastact=”QUIT\r\n”;

fputs($fp,$lastace);

fclose($fp);

return 0;

}

}

/*發送示例

$sm = new smail( “用戶名@163.com”, “密碼”, “smtp.163.com” );

$end = $sm-send( “目標郵箱”, “來源郵箱”, “這是標題”, “這是郵件內容” );

if( $end ) echo $end;

else echo “發送成功!”;

*/

?

php下大家都使用什麼發送郵件

直接用mail即可

sina的郵箱除了名的兼容差

不是mail的問題

PHP 能用自帶的email函數發兩封郵件么

可以在php上配置一個PEAR,裡面有Mail.php這個庫,通過這個庫文件就可以發送郵件了。

如何在php用mail發送郵件

有兩種方法

一、使用PHP內置的mail()函數

二、使用封裝SMTP協議的郵件類

具體可以參考這篇文章,希望對你有幫助

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/196012.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-03 09:51
下一篇 2024-12-03 09:52

相關推薦

發表回復

登錄後才能評論