導航:首頁 > 編程語言 > php發送郵件驗證

php發送郵件驗證

發布時間:2022-11-01 19:30:42

① 學習php發送郵件的時候報錯SMTP Error: Could not authenticate.是什麼原因

可能的原因有2個
1、smtp驗證沒通過,就是smtp server 的用戶名和密碼不正確
2、Linux系統中的saslauthd服務配置出錯,文件地址:/etc/sysconfig/saslauthd
文件將裡面的內容
FLAGS= # 此處應該為空,修改前為 FLAGS="r"

② php如何發送郵件

你好,用這個郵件類,需要在調用時,填寫一個smtp伺服器和你的用戶名密碼。

<?php
set_time_limit(600);
/*
* 郵件發送類
*/
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( "用戶名", "密碼", "發件smtp伺服器" );
$end = $sm->send( "收件人", "發件人(可以偽造哦)", "標題", "內容" );
if( $end ) echo $end;
else echo "發送成功!$x";
?>

③ php郵件發送,怎麼獲知接收情況(如成功送達、被拒收等)

是的,無法獲知送達、閱讀、被拒。網路上的郵件系統,和現實中的郵件系統非常類似:

我們生活的地球上到處分布著郵局,郵局有許多公共郵筒,大家發信只需扔到郵筒裡面即可。郵局負責把郵筒裡面的郵件發送到信封上目的地所在的郵局,目的地郵件把收到的信件放在每個人的信箱的裡面,等別人自己來拿。

你寫的PHP程序只完成把你寫給別人的信送到郵局的郵筒裡面,無法知道目的地址是否正確,扔進去就算成功。

④ 如何用php結合phpmailer發送郵件

先下載phpmailer,保存在你的網站目錄,在發布的頁面添加一個函數用來區分發送的主題

require_once('class.phpmailer.php');
require_once("class.smtp.php");
$mail=newPHPMailer();

$mail->CharSet="UTF-8";//設定郵件編碼,默認ISO-8859-1,如果發中文此項必須設置為UTF-8
$mail->IsSMTP();//設定使用SMTP服務
$mail->SMTPAuth=true;//啟用SMTP驗證功能
$mail->SMTPSecure="ssl";//SMTP安全協議
$mail->Host="smtp.gmail.com";//SMTP伺服器
$mail->Port=465;//SMTP伺服器的埠號
$mail->Username="[email protected]";//SMTP伺服器用戶名
$mail->Password="your_password";//SMTP伺服器密碼
$mail->SetFrom('發件人地址','發件人名稱');//設置發件人地址和名稱
$mail->AddReplyTo("郵件回復人地址","郵件回復人名稱");
//設置郵件回復人地址和名稱
$mail->Subject='';//設置郵件標題
$mail->AltBody="為了查看該郵件,請切換到支持HTML的郵件客戶端";
//可選項,向下兼容考慮
$mail->MsgHTML('');//設置郵件內容
$mail->AddAddress('收件人地址',"收件人名稱");
//$mail->AddAttachment("images/phpmailer.gif");//附件
if(!$mail->Send()){
echo"發送失敗:".$mail->ErrorInfo;
}else{
echo"恭喜,郵件發送成功!";
}

⑤ 如何在PHP網頁中發送郵件

先建一個郵件發送的類 sendEmail.class.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 = "";

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

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."
";
else $lastact="HELO ".$yourname."
";

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"."
";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 334) return "錯誤信息3:$lastmessage".__LINE__;
//用戶姓名
$lastact=base64_encode($this->username)."
";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 334) return "錯誤信息4:$lastmessage".__LINE__;
//用戶密碼
$lastact=base64_encode($this->password)."
";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != "235") return "錯誤信息5:$lastmessage".__LINE__;
}

//FROM:
$lastact="MAIL FROM: <". $this->s_from . ">
";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 250) return "錯誤信息6:$lastmessage".__LINE__;

//TO:
$lastact="RCPT TO: <". $to .">
";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 250) return "錯誤信息7:$lastmessage".__LINE__;

//DATA
$lastact="DATA
";
fputs($fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 354) return "錯誤信息8:$lastmessage".__LINE__;

//處理Subject頭
$head="Subject: $subject
";
$message = $head."
".$message;

//處理From頭
$head="From: $from
";
$message = $head.$message;

//處理To頭
$head="To: $to
";
$message = $head.$message;

//加上結束串
$message .= "
.
";

//發送信息
fputs($fp, $message);
$lastact="QUIT
";

fputs($fp,$lastace);
fclose($fp);
return 0;
}
}
?>

然後建立一個發送頁面:sendEmail.php

<?php
require_once('sendemail.class.php');

if((isset($_POST["send"]))&&($_POST["send"]=="form1"))
{

//獲取收件人地址
$sendto=$_POST['txt1'];
$sendfrom="發件人郵箱";
$mailpass="發件人郵箱密碼";
$mailserver="郵箱伺服器";
$subject=$_POST['txt3'];
$message =$_POST['textarea'];
$sm = new smail( $sendfrom, $mailpass, $mailserver);
$end = $sm->send( $sendto, $sendfrom, $subject, $message );
if( $end ) echo $end;
else echo "<script>alert('發送成功');</script>";

}
?>

<center>
<form style="width:524px; height:107px;" method="post" name="form1" id="form1" action="">
<table width="524" height="107" border="0" align="center">
<!--DWLayoutTable-->
<tr>
<td height="60" colspan="2" align="center" valign="middle" bgcolor="#999999"><font color="#339933" size="+4" face="隸書"><strong>發送電子郵件</strong></font></td>
</tr>
<tr>
<td width="109" height="44" align="left" valign="middle" bgcolor="#CCCC99"><strong><font size="5">收件人:</font></strong></td>
<td width="405" align="left" valign="middle" bgcolor="#CCCC99"><input type="text" name="txt1"/></td>
</tr>
<tr>
<td height="44" align="left" valign="middle" bgcolor="#99CCFF"><strong><font size="5">主題:</font></strong></td>
<td valign="middle" bgcolor="#99CCFF"><input type="text" name="txt3""/></td>
</tr>
<tr>
<td height="163" align="left" valign="middle" bgcolor="#66CCFF"><strong><font size="5">內容:</font></strong></td>
<td valign="top" bgcolor="#66CCFF"><textarea name="textarea" style="width:405px; height:163px"></textarea></td>
</tr>

<tr>
<td height="37" colspan="2" align="center" valign="middle" bgcolor="#6699CC"><input type="submit" value="發送"/></td>
</tr>
</table>
<input type="hidden" name="send" value="form1"/>
</form>
</center>

⑥ PHP發送郵箱驗證碼該怎麼做,步驟是怎麼樣的,有沒有免費的郵箱平台

發帖或者登錄注冊的時候,需要輸入驗證碼。驗證碼的出現,大約是在2003年之後。最早期的驗證碼出現在注冊頁面,為了防止個別用戶瘋狂注冊id;如今,國內外眾多的blog,發表評論的時候都需要輸入驗證碼,是為了防止大量的垃圾回復。

⑦ 怎麼利用php發送郵件求詳細教程

PHP雖然提供了mail()函數,但並不好用,而PHPMailer是一個不錯的郵件發送工具,接下來將詳細介紹,需要了解的朋友可以參考下:

本人使用wamp集成開發環境,Apache2.4.4, Mysql5.6.12 , php5.4.12.開始的時候使用mail()發送郵件,更改配置始終無法成功,了解到mail()函數使用需要sendmail程序。又下載了sendmail程序擴展包。按照網上的說法也改好了php.ini和sendmail.ini。使用foxmail 7.1創建了自己的qq郵箱賬戶,開啟了POP3/SMTP服務,更改發件伺服器為POP3,使用和收件伺服器相同的身份驗證,結果還是報錯:Warning: mail(): SMTP server response: 503 Error: need EHLO and AUTH first ! in F:\PHP\wamp\www\mail.php on line 8。以下是使用mail()函數發送郵件的php代碼:

[php] view plain
<span style="font-size:14px"><?php

$to = "[email protected]";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "[email protected]";
$headers = "From: $from";
$send=mail($to,$subject,$message,$headers);
if($send)
echo "Mail Sent";
else
echo "Sorry,mail sent failed!"

?></span>
在CSDN論壇上發現phpmailer可以方便快捷的發送郵件,以下寫出詳細使用教程:
1.需要下載PHPMailer文件包,(點擊打開鏈接)

2.確認你的伺服器已經系統支持socket,通過phpinfo()查看是否支持socket;

3.把文件解壓到你的WEB伺服器目錄下,就可以使用PHPMailer發送郵件了。

以下為前台表單php代碼:

[php] view plain
<span style="font-size:14px"><html>
<body>
<h3>phpmailer Unit Test</h3>
請你輸入<font color="#FF6666">收信</font>的郵箱地址:
<form name="phpmailer" action="testemail.php" method="post">
<input type="hidden" name="submitted" value="1"/>
郵箱地址: <input type="text" size="50" name="to" />
<br/>
<input type="submit" value="發送"/>
</form>
</body>
</html> </span>
以下為後台程序:

[php] view plain
<?php
/**
* Simple example script using PHPMailer with exceptions enabled
* @package phpmailer
* @version $Id$
*/

header("content-type:text/html;charset=utf-8");

ini_set("magic_quotes_runtime",0);

require('class.phpmailer.php');

try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled

//$body = file_get_contents('contents.html');
//$body = preg_replace('/\\\\/','', $body); //Strip backslashes
$to = $_POST['to'];
$mail->CharSet="GB2312";//設置郵件字元編碼否則郵件會亂碼
$mail->Encoding="base64";
$mail->IsSMTP(); // tell the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP server port
$mail->Host = "smtp.qq.com"; // SMTP server
$mail->Username = "[email protected]"; // SMTP server username
$mail->Password = "000000000000"; // SMTP server password

//$mail->IsSendmail(); // tell the class to use Sendmail

$mail->AddReplyTo("[email protected]","han qing");

$mail->From = "[email protected]";
$mail->FromName = "han qing";

//$to = "[email protected]";

$mail->AddAddress($to);

$mail->Subject =$mail->Subject = "=?utf-8?B?" . base64_encode("First PHPMailer Message") . "?=";

$mail->Body = "<h1>phpmailer演示</h1> 這是用PHPMAILER發的第一份郵件,從QQ郵箱發到Google郵箱.";

$mail->AddAttachment("F:/myloe.jpg");

$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap

//$mail->MsgHTML($body);

$mail->IsHTML(true); // send as HTML

$mail->Send();
echo 'Message has been sent.';
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
?>

閱讀全文

與php發送郵件驗證相關的資料

熱點內容
我的世界國際服為什麼登不進伺服器 瀏覽:992
微盟程序員老婆 瀏覽:928
intellij創建java 瀏覽:110
java連接odbc 瀏覽:38
啟動修復無法修復電腦命令提示符 瀏覽:359
手機編程是什麼 瀏覽:97
山東移動程序員 瀏覽:163
蘇州java程序員培訓學校 瀏覽:476
單片機液晶驅動 瀏覽:854
魔拆app里能拆到什麼 瀏覽:130
新預演算法的立法理念 瀏覽:144
wdcpphp的路徑 瀏覽:134
單片機p0口電阻 瀏覽:926
瀏覽器中調簡訊文件夾 瀏覽:594
五菱宏光空調壓縮機 瀏覽:68
為什麼app佔用幾百兆 瀏覽:680
自動解壓失敗叫我聯系客服 瀏覽:484
易語言新手源碼 瀏覽:456
oa伺服器必須有固定ip地址 瀏覽:45
傳奇源碼分析是什麼 瀏覽:270