phpemail
㈠ php發郵件需要什麼東西
PHP mail() 函數
PHP mail() 函數用於從腳本中發送電子郵件。
語法
mail(to,subject,message,headers,parameters)
參數 描述
to 必需。規定 email 接收者。
subject 必需。規定 email 的主題。注釋:該參數不能包含任何新行字元。
message 必需。定義要發送的消息。應使用 LF (\n) 來分隔各行。
headers
可選。規定附加的標題,比如 From、Cc 以及 Bcc。
應當使用 CRLF (\r\n) 分隔附加的標題。
parameters 可選。對郵件發送程序規定額外的參數。
注釋:PHP 需要一個已安裝且正在運行的郵件系統,以便使郵件函數可用。所用的程序通過在 php.ini 文件中的配置設置進行定義。請在我們的 PHP Mail 參考手冊閱讀更多內容。
PHP 簡易 E-Mail
通過 PHP 發送電子郵件的最簡單的方式是發送一封文本 email。
㈡ 怎麼利用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發送郵件php的mail函數不能用!
支持mail的伺服器 一般都是linux的 國內的好像不多
自己的電腦安裝mail伺服器不能往外發的 呵呵 可以自己測試用
現在很多管理系統都是用fsocketopen方式連接郵件伺服器並發送郵件的 可以使用163 126的郵箱 網上有一些模型的 就像是好多管理系統後台讓填入用戶名和密碼 就能群發一樣 如果你不介意的話 給你轉發一個以前我自己改過的可以利用fsocketopen方式群發或者單發email的一共三個文件
MailClass.php 》》》》》》
<?php
class Smtp
{
var $host; //主機
var $port; //埠 一般為25
var $user; //SMTP認證的帳號
var $pass; //認證密碼
var $debug = false; //是否顯示和伺服器會話信息?
var $conn;
var $result_str; //結果
var $in; //客戶機發送的命令
var $from; //收件人收到郵件顯示的源信箱
var $email; //真實的地址
var $to; //目標信箱
var $subject; //主題
var $body; //內容
var $error;
var $All;
function Smtp($array)
{
$this->host = $array['host'];
$this->port = $array['port'];
$this->email= $array['trueemail'];
$this->from = $array['from'];
$this->user = base64_encode($array['username']);
$this->pass = base64_encode($array['password']);
$this->debug = $array['debug'];
$this->socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP);
if($this->socket){
$this->result_str = "創建SOCKET:".socket_strerror(socket_last_error());
$this->debug_show($this->result_str);
}
else
die("初始化失敗,請檢查您的網路連接和參數");
$this->conn = socket_connect($this->socket,$this->host,$this->port);
if($this->conn){
$this->result_str = "創建SOCKET連接:".socket_strerror(socket_last_error());
$this->debug_show($this->result_str);
}
else
die("初始化失敗,請檢查您的網路連接和參數");
$this->result_str = "伺服器應答:<font color=#cc0000>".socket_read ($this->socket, 1024)."</font>";
$this->debug_show($this->result_str);
}
function debug_show($str)
{
if($this->debug)
{
echo $str."<p>\r\n";
}
}
function setmail($to,$subject,$body){
$this->to = $to;
$this->subject = $subject;
$this->body = $body;
$All ="Content-type:text/html;charset=gb2312\r\n"; //郵件的編碼方式可以根據自己的需要改
$All .= "From:".$this->from."\r\n";
$All .= "To:".$this->to."\r\n";
$All .= "Subject:".$this->subject."\r\n\r\n";
$All .= $this->body;
$this->All = $All;
}
/**
* 發送郵件部分
* 接收郵箱數組
*/
function send($toarray,$subject,$body)
{
//以下是和伺服器會話
$this->in = "EHLO HELO\r\n";
$this->docommand();
$this->in = "AUTH LOGIN\r\n";
$this->docommand();
$this->in = $this->user."\r\n";
$this->docommand();
$this->in = $this->pass."\r\n";
$this->docommand();
foreach( $toarray as $to ) {
$this -> setmail($to,$subject,$body);
$this->in = "RSET\r\n";
$this->docommand();
$this->in = "MAIL FROM:<".$this->email.">\r\n";
$this->docommand();
$this->in = "RCPT TO:<".$this->to.">\r\n";
$this->docommand();
$this->in = "DATA\r\n";
$this->docommand();
$this->in = $this->All."\r\n.\r\n";
$this->docommand();
}
$this->in = "QUIT\r\n";
$this->docommand();
//結束,關閉連接
}
function docommand()
{
socket_write ($this->socket, $this->in, strlen ($this->in));
$this->debug_show("Client Action:".$this->in);
$this->result_str = "Server:<font color=#cc0000>".socket_read ($this->socket, 1024)."</font>";
$this->debug_show($this->result_str);
}
}
?>
MailConfig.inc.php 》》》》》》
<?php
$mailconfig['host'] = "smtp.126.com"; //主機
$mailconfig['port'] = "25"; //埠 一般為25
$mailconfig['trueemail'] = "[email protected]"; //真實的地址
$mailconfig['username'] = "mhz1600"; //SMTP認證的帳號
$mailconfig['password'] = "*****"; //改成自己的
$mailconfig['debug'] = false; //是否顯示和伺服器會話信息?
$mailconfig['from'] = "[email protected]"; //顯示給用戶的發件人
include_once "MailClass.php";
set_time_limit(180);
?>
SendDemo.php 》》》》》》
<?php
include_once "MailConfig.inc.php";
//簡單的臨時碼驗證 當前時間(到小時)的驗證碼
//if( empty($_GET['s']) || $_GET['s'] != md5(date('Y-m-d-H',time())) ) {header("http/1.1 404"); die('');}
//發送email
if( isset($_POST['sendmail']) ) {
if( isset($_POST['from']) ) $mailconfig['from'] = $_POST['from'];
$smtp = new Smtp($mailconfig);
$title = $_POST['title'];
//獲取post的email正文
if( get_magic_quotes_gpc() ) $message = $_POST['message'];
else $message = addslashes($_POST['message']);
//從email列表/文檔中分離出所有的email地址
$pregstr = "@[a-zA-Z0-9\_][0-9a-zA-Z\.\-\_]+\@[0-aA-Za-z\-\_]+\.[0-9a-zA-Z\.\-\_]+@is";
$temp = array();
preg_match_all($pregstr,$_POST['emails'],$temp);
$toarray = $temp[0];
//var_mp($toarray);
$smtp->send($toarray,$title,$message);
die("操作完成!<A href=".$_SERVER['PHP_SELF']."?s=".md5(date('Y-m-d-H',time())).">繼續發送其他</a> <a href=# onclick=window.close()>關閉</a>");
}
else {
if( isset($_POST['emails']) ) {
if( is_array($_POST['emails']) )
$emails = implode("\t",$_POST['emails']);
else
$emails = $_POST['emails'];
}
else $emails = "";
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"><style type="text/css">
<!--
body,td,th {
font-size: 12px;
}
-->
</style></head>
<body>
<form id="form1" name="form1" method="post" action="">
<table width="600" border="1" align="center" cellpadding="3" cellspacing="0" bordercolordark="#FFFFFF" bordercolorlight="#eeeeee">
<tr>
<td width="66">發件人:</td>
<td width="516"><input name="from" type="text" value="<?php echo $mailconfig['from']; ?>"> 可以直接修改mailconfig文件中的email</td>
</tr>
<tr>
<td>郵件標題:</td>
<td><input name="title" type="text" value="郵件群發測試標題!" size="60"></td>
</tr>
<tr>
<td>收件人:<br></td>
<td><textarea name="emails" cols="60" rows="5"><?php echo $emails; ?></textarea></td>
</tr>
<tr>
<td>郵件正文:<br>
【html】</td>
<td><textarea name="message" cols="60" rows="10">郵件群發測試!謝謝~!</textarea></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="sendmail" value=" 發送郵件 "> </td>
</tr>
</table>
</form>
<?
}
?>
</body>
</html>
使用方式 運行senddemo.php就行 確定本地或者伺服器開啟了fsocketopen支持 在輸入框可以多種格式的的輸入很多email 程序用正則表達式匹配出所有的email地址 通過伺服器循環對話的方式不斷的發送郵件 看看那個demo的流程就明白了
【鄭重聲明:mailclass修改自網上的模型 其他本人原創,版權不究 歡迎分享】
+---------------------廣告-------------------------+
那一天:回憶,讓生活更美好
獨享人生中那個特別的日子,記錄從那一天開始的幸福
http://www.nayitian.net
期待您的加入,歡迎提供寶貴的意見建議
+--------------------------------------------------+
+--------------------補充--------------------+
發送郵件的伺服器(smtp)並不是網址 126發送郵件的伺服器是 smtp.126.com 網易163的發送郵件伺服器是 smtp.163.com 所有郵箱對於這個都有說明的 還有一個就是能夠使用這個功能的好象新注冊的郵箱不太好用 因為網易在2006年10對郵箱進行過調整 在此之前注冊的都沒問題 在這之後注冊的好像開通一些其他的功能並且使用了一段時間才行的
smtp伺服器的鏈接可以在命令提示行下測試 就是使用上面的命令:
首先 telnet smtp.126.com 25
因為smtp使用的25埠提供服務的 然後就會看到
220 126.com Anti-spam GT for Coremail System (126com[071018])
輸入 EHLO HELO
伺服器返回
250-mail
250-PIPELINING
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250 8BITMIME
輸入 AUTH LOGIN
伺服器返回
334 dXNlcm5hbWU6
然後再輸入通過base64加密的用戶名和密碼 就能通過命令來和伺服器對話 包括發送郵件等功能
具體的如果有興趣更多命令自己查一下
這個php的程序就是模擬這個功能來實現的
如果你用telnet直接連不上的話 說明伺服器是錯誤的 。。
㈣ 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
$server = "{bjmail.*.com/pop3}"; //郵件伺服器
$mailbox = "inbox"; //收件箱
$mailaccount="zhao**";//用戶名
$mailpasswd=" "; //密碼
$stream = @imap_open($server.$mailbox,$mailaccount,$mailpasswd);//打開IMAP 連結
$mail_number = imap_num_msg($stream);//信件的個數
if($mail_number < 1) { echo "No Message for $email"; }//如果信件數為0,顯示信息
for($i=$mail_number;$i>=$mail_number;$i--)
{
$headers = @imap_header($stream, $i);
$mail_header= imap_headerinfo($stream, $i);//郵件頭部
//var_mp ($mail_header);
$subject = $mail_header->subject;//郵件標題
$subject=decode_mime($subject);
echo $subject;
//編碼為簡體中文的標題的處理方法
// if(stristr($subject, "=?gb2312"))
// { //編碼為簡體中文的標題
// $subject=substr($subject,11);
// $subject=substr($subject,0,-2);
// $subject = base64_decode($subject);
// }
echo $from = $mail_header->fromaddress;//發件人
echo $date = $mail_header->date;//日期
$body = imap_fetchbody($stream, $i, 1);
$body = imap_base64($body);
$body = nl2br($body);
echo $body;
// $body = imap_qprint($body);
// echo $body;
// $body = imap_binary($body);
// $body = imap_base64($body);
//echo $body;
}
//對mime解碼
function decode_mime($string)
{
$pos = strpos($string,'=?');
if (!is_int($pos)) {
return $string;
}
$preceding = substr($string, 0, $pos); // save any preceding text
$search = substr($string, $pos+2); /* the mime header spec says this is the longest a single encoded Word can be */
$d1 = strpos($search, '?');
if (!is_int($d1)) {
return $string;
}
$charset = substr($string, $pos+2, $d1); //取出字元集的定義部分
$search = substr($search, $d1+1); //字元集定義以後的部分=>$search;
$d2 = strpos($search, '?');
if (!is_int($d2)) {
return $string;
}
$encoding = substr($search, 0, $d2); ////兩個?之間的部分編碼方式:q或b
$search = substr($search, $d2+1);
$end = strpos($search, '?='); //$d2+1 與 $end 之間是編碼了的內容:=> $endcoded_text;
if (!is_int($end)) {
return $string;
}
$encoded_text = substr($search, 0, $end);
$rest = substr($string, (strlen($preceding . $charset . $encoding . $encoded_text)+6)); //+6 是前面去掉的=????=六個字元
switch ($encoding) {
case 'Q':
case 'q':
//$encoded_text = str_replace('_', '%20', $encoded_text);
//$encoded_text = str_replace('=', '%', $encoded_text);
//$decoded = urldecode($encoded_text);
$decoded=quoted_printable_decode($encoded_text);
if (strtolower($charset) == 'windows-1251') {
$decoded = convert_cyr_string($decoded, 'w', 'k');
}
break;
case 'B':
case 'b':
$decoded = base64_decode($encoded_text);
if (strtolower($charset) == 'windows-1251') {
$decoded = convert_cyr_string($decoded, 'w', 'k');
}
break;
default:
$decoded = '=?' . $charset . '?' . $encoding . '?' . $encoded_text . '?=';
break;
}
return $preceding . $decoded .decode_mime($rest);
//return $preceding . $decoded . $this->decode_mime($rest);
}
㈥ 使用php 怎麼發送郵件
你這個是連接的郵件伺服器出錯了。
估計你本地應該沒有裝郵件伺服器吧,一般都會用第三方的郵件伺服器,如smtp.163.com,
去下載個phpmailer,從裡面把class.phpmailer.php提取出來,用很好用的。
㈦ php編程---email功能
*/
set_time_limit(120);
class smtp_mail
{
var $host; //主機
var $port; //埠 一般為25
var $user; //SMTP認證的帳號
var $pass; //認證密碼
var $debug = false; //是否顯示和伺服器會話信息?
var $conn;
var $result_str; //結果
var $in; //客戶機發送的命令
var $from; //源信箱
var $to; //目標信箱
var $subject; //主題
var $body; //內容
function smtp_mail($host,$port,$user,$pass,$debug=false)
{
$this->host = $host;
$this->port = $port;
$this->user = base64_encode($user);
$this->pass = base64_encode($pass);
$this->debug = $debug;
$this->socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP); //具體用法請參考手冊
if($this->socket)
{
$this->result_str = "創建SOCKET:".socket_strerror(socket_last_error());
$this->debug_show($this->result_str);
}
else
{
exit("初始化失敗,請檢查您的網路連接和參數");
}
$this->conn = socket_connect($this->socket,$this->host,$this->port);
if($this->conn)
{
$this->result_str = "創建SOCKET連接:".socket_strerror(socket_last_error());
$this->debug_show($this->result_str);
}
else
{
exit("初始化失敗,請檢查您的網路連接和參數");
}
$this->result_str = "伺服器應答:<font color=#cc0000>".socket_read ($this->socket, 1024)."</font>";
$this->debug_show($this->result_str);
}
function debug_show($str)
{
if($this->debug)
{
echo $str."<p>\r\n";
}
}
function send($from,$to,$subject,$body)
{
if($from == "" ¦ ¦ $to == "")
{
exit("請輸入信箱地址");
}
if($subject == "") $sebject = "無標題";
if($body == "") $body = "無內容";
$this->from = $from;
$this->to = $to;
$this->subject = $subject;
$this->body = $body;
$All = "From:".$this->from."\n";
$All .= "To:".$this->to."\n";
$All .= "Subject:".$this->subject."\n";
$All .= $this->body;
/*
如過把$All的內容再加處理,就可以實現發送MIME郵件了
不過還需要加很多程序
*/
//以下是和伺服器會話
$this->in = "EHLO HELO\r\n";
$this->docommand();
$this->in = "AUTH LOGIN\r\n";
$this->docommand();
$this->in = $this->user."\r\n";
$this->docommand();
$this->in = $this->pass."\r\n";
$this->docommand();
$this->in = "MAIL FROM:".$this->from."\r\n";
$this->docommand();
$this->in = "RCPT TO:".$this->to."\r\n";
$this->docommand();
$this->in = "DATA\r\n";
$this->docommand();
$this->in = $All."\r\n.\r\n";
$this->docommand();
$this->in = "QUIT\r\n";
$this->docommand();
//結束,關閉連接
}
function docommand()
{
socket_write ($this->socket, $this->in, strlen ($this->in));
$this->debug_show("客戶機命令:".$this->in);
$this->result_str = "伺服器應答:<font color=#cc0000>".socket_read ($this->socket, 1024)."</font>";
$this->debug_show($this->result_str);
}
}
//這個是我做的測試,我用的是smtp.163.com,那你的信箱也必須是163.com的,要不人家不讓你發!!
//你用這個類的時候你修改成你自己的信箱就可以了
$smtp = new smtp_mail("smtp.163.com","25","你的163.com的帳號","你的密碼");
//如果你需要顯示會話信息,請將上面的修改成
//$smtp = new smtp_mail("smtp.163.com","25","你的163.com的帳號","你的密碼",true);
$smtp->send("你的163.com的帳號@163.com","目標地址","你好","你好");
㈧ 如何在PHP中使用PHPMailer發送郵件
<?php
header("content-type:text/html;charset=utf-8");
ini_set("magic_quotes_runtime",0);
require'class.phpmailer.php';
try{
$mail=newPHPMailer(true);
$mail->IsSMTP();
$mail->CharSet='UTF-8';//設置郵件的字元編碼,這很重要,不然中文亂碼
$mail->SMTPAuth=true;//開啟認證
$mail->Port=25;
$mail->Host="smtp.163.com";
$mail->Username="[email protected]";
$mail->Password="這是密碼";
//$mail->IsSendmail();//如果沒有sendmail組件就注釋掉,否則出現「Couldnotexecute:/var/qmail/bin/sendmail」的錯誤提示
$mail->AddReplyTo("[email protected]","mckee");//回復地址
$mail->From="[email protected]";
$mail->FromName="www.phpddt.com";
$to="[email protected]";
$mail->AddAddress($to);
$mail->Subject="phpmailer測試標題";
$mail->Body="<h1>phpmail演示</h1>這是php點點通(<fontcolor=red>www.phpddt.com</font>)對phpmailer的測試內容";
$mail->AltBody="Toviewthemessage,!";//當郵件不支持html時備用顯示,可以省略
$mail->WordWrap=80;//設置每行字元串的長度
//$mail->AddAttachment("f:/test.png");//可以添加附件
$mail->IsHTML(true);
$mail->Send();
echo'郵件已發送';
}catch(phpmailerException$e){
echo"郵件發送失敗:".$e->errorMessage();
}
?>
㈨ 用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網頁中發送郵件
先建一個郵件發送的類 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>