Php郵件發送源碼

來源:互聯網
上載者:User

標籤:io   os   使用   ar   for   檔案   資料   cti   on   

好久冒寫點東西了.....最近生活壓抑的很....為生活而勞累,整理下郵件發送的執行個體了,網上也有很多,我這個也是提取整理好的,測試Ok,首頁郵件類smtp_email_class.php如下:
<?php
class email
{
function send_mail($to,$subject,$message,$from,$from_name,$mailformat=1)
{
if(function_exists(‘mail‘))
{

$headers = ‘From: ‘.$from_name.‘<‘.$from.‘>‘."\r\n";
$headers .= ‘TO: ‘.$to."\r\n";
if($mailformat)
{$headers .="Content-Type: text/html;\r\n";}
else
{$headers .="Content-Type: text/plain;\r\n";}
$headers .="charset=gb2312\r\n\r\n";


$message = str_replace("\r", ‘‘, $message);

[email protected]($to, str_replace("\n",‘ ‘,$subject), $message,$headers);

if(!$mail_return)
{
return $to.‘發送不成功‘;
}

return 1;
}
}

function send_win32_mail($to,$subject,$message,$from,$from_name,$host,$port,$mailformat=1)
{
ini_set(‘SMTP‘, $host);
ini_set(‘smtp_port‘, $port);
ini_set(‘sendmail_from‘, $from);

$headers = ‘From: ‘.$from_name.‘<‘.$from.‘>‘."\r\n";
$headers .= ‘TO: ‘.$to."\r\n";
if($mailformat)
{$headers .="Content-Type: text/html;\r\n";}
else
{$headers .="Content-Type: text/plain;\r\n";}
$headers .="charset=gb2312\r\n\r\n";

foreach(explode(‘,‘, $to) as $touser)
{
$touser = trim($touser);
if($touser)
{
[email protected]($touser, $subject, $message, $headers);
if(!$mail_return)
{
return $touser.‘發送不成功‘;
}
}
}
return 1;
}

//通過sock發送e_mail,不支援附件,
//-------------------------------------------------------------------------------------------------------
function email_sock($host,$port,$errno,$errstr,$timeout,$auth,$user,$pass,$from)//建構函式
{
$this->host = $host;
$this->port = $port;
$this->errno = $errno;
$this->errstr = $errstr;
$this->timeout = $timeout;
$this->auth = $auth;
$this->user = $user;
$this->pass = $pass;
$this->from = $from;
}

function send_mail_sock($subject,$message,$to,$from_name,$mailformat=0)//郵件標題,郵件內容,收件地址,郵件格式1=text|0=html,預設為0
{
$host = $this->host;
$port = $this->port;
$errno = $this->errno;
$errstr = $this->errstr;
$timeout = $this->timeout;
$auth = $this->auth;
$user = $this->user;
$pass = $this->pass;
$from = $this->from;

/*
1.建立sock,並開啟串連
2.設定為阻塞模式
3.測試smtp應答碼是否為220,220代表郵件服務就緒
4.發送使用者身分識別驗證,由使用者佈建
1=EHLO Host Domain \r\n
0=HELO Host Domain \r\n
?.讀取伺服器端發送給用戶端的返回資料
smtp.163.com 發送的資料為:
250-PIPELINING//流水命令,告訴用戶端可以一次發送多個命令來提高速度,在這裡PHP
並沒有使用,因為PHP單個檔案的運行還是單線程的
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250 8BITMIME//得到這一行也就是smtp伺服器發送結束了,等待用戶端發送命令
5.發送AUTH LOGIN命令
6.發送使用者名稱
7.發送密碼
?.身分識別驗證過成功後後,
8.向伺服器添加from
9.向伺服器添加to
10.發送DATA命令,開始輸入email資料,以"."號結束
11.書寫郵件內容
12.將郵件內容發送到smtp伺服器
13.發送QUIT命令,結束會話
*/
$fp = fsockopen($host,$port,$errno,$errstr,$timeout);//開啟sock的網路連接
if(!$fp){return ‘1.沒有設定好smtp服務‘;}

stream_set_blocking($fp, true);//設定為阻塞模式,此模式讀不到資料則會停止在那

$mail_return=fgets($fp, 512);//讀取512位元組內容
if(substr($mail_return, 0, 3) != ‘220‘)
{return $host.‘-2.返回應答碼為‘.substr($mail_return, 0, 3);}//返回應答碼所代表意思請參考‘smtp協議.txt‘


fputs($fp, ($auth ? ‘EHLO‘ : ‘HELO‘)." ".$host."\r\n");//伺服器標識使用者身份 1=身分識別驗證的標識,0=不需要身分識別驗證的標識
$mail_return = fgets($fp, 512);
if(substr($mail_return, 0, 3) != 220 && substr($mail_return, 0, 3) != 250)
{return $host.‘-3.返回應答碼為‘.substr($mail_return, 0, 3);}

while(true)
{
$mail_return = fgets($fp, 512);
if(substr($mail_return, 3, 1) != ‘-‘ || empty($mail_return))
{break;}
}


if($auth)
{
fputs($fp, "AUTH LOGIN\r\n");
$mail_return = fgets($fp, 512);
if(substr($mail_return, 0, 3) != 334)
{return $host.‘-5.返回應答碼為‘.substr($mail_return, 0, 3);}

fputs($fp, base64_encode($user)."\r\n");
$mail_return = fgets($fp, 512);
if(substr($mail_return, 0, 3) != 334)
{return $host.‘-6.返回應答碼為‘.substr($mail_return, 0, 3).‘user=‘.$user;}

fputs($fp, base64_encode($pass)."\r\n");
$mail_return=fgets($fp, 512);
if(substr($mail_return, 0, 3) != 235)
{return $host.‘-7.使用者驗證失敗,應答碼為‘.substr($mail_return, 0, 3);}
}

//向伺服器添加FROM and TO
//------------------------------------------------------------------------------------------------------------------------
fputs($fp, "MAIL FROM: ".$from."\r\n");//有兩種格式,MAIL FROM:[email protected]和MAIL FROM: <[email protected]>
$mail_return = fgets($fp, 512);
if(substr($mail_return, 0, 3) != 250)
{
fputs($fp, "MAIL FROM: <".$from.">\r\n");
$mail_return = fgets($fp, 512);
if(substr($mail_return, 0, 3) != 250)
{return $host.‘-8.返回應答碼為‘.substr($mail_return, 0, 3);}
}

foreach(explode(‘,‘, $to) as $mailto)
{
$mailto = trim($mailto);
if($mailto)
{
fputs($fp, "RCPT TO: ".$mailto."\r\n");
$mail_return = fgets($fp, 512);
if(substr($mail_return, 0, 3) != 250)
{
fputs($fp, "RCPT TO: <".$mailto.">\r\n");
$mail_return = fgets($fp, 512);
if(substr($mail_return, 0, 3) != 250)
{return $host.‘-9.返回應答碼為‘.substr($mail_return, 0, 3);}
}
}

}
//------------------------------------------------------------------------------------------------------------------------
fputs($fp, "DATA\r\n");//開始輸入email資料,以"."號結束
$mail_return = fgets($fp, 512);
if(substr($mail_return, 0, 3) != 354)
{return $host.‘-10.返回應答碼為‘.substr($mail_return, 0, 3);}

//郵件內容
//-----------------------------------------------------------
$mail_message = "From:".$from_name.‘<‘.$from.">\r\n";
$mail_message .= "To:".$to."\r\n";
$mail_message .= "Subject:".str_replace("\n",‘ ‘,$subject)."\r\n";
if($mailformat==1)
{$mail_message .= "Content-Type: text/html;\r\n"; }
else
{$mail_message .= "Content-Type: text/plain;\r\n";}
$mail_message .= "charset=gb2312\r\n\r\n";
$mail_message .= $message;
$mail_message .= "\r\n.\r\n";
//-----------------------------------------------------------

fputs($fp,$mail_message);
fputs($fp,"QUIT\r\n");

return 1;
}
}

?>


使用方法,建立send.php,源碼如下
<?php
//*
include(‘smtp_email_class.php‘);
//定義參數------------------------------------------------------
$host = ‘smtp.163.com‘;//smtp伺服器位址,我這裡又能夠的是163的,視情況而定
$from = ‘[email protected]‘;//自己的郵件地址
$port = 25;//連接埠
$errno = 0;//錯誤返回號
$errstr = ‘‘;//錯誤返回內容
$timeout = 10;//系統運行逾時
$auth = 1;//是否需要 AUTH LOGIN 驗證, 1=是, 0=否
$user = ‘你的郵件使用者名‘;//smtp伺服器使用者名稱
$pass = ‘你的郵件密碼‘;//smtp伺服器密碼
$from_name=‘你的大名‘;//連絡人名稱
//---------------------------------------------------------------
$send_mode=2;
$em=new email();//使用類
if($send_mode==1){
//使用PHP自己支援的mail函數發送郵件,發送郵件需要smtp服務支援
if($em->send_mail(‘發送給某人(張三)的郵件地址‘,‘標題‘,"內容",‘[email protected]‘,‘自己的名稱/連絡人名稱‘,0)){
echo("郵件發送成功....");
}
}elseif($send_mode==2){
$em->email_sock($host,$port,$errno,$errstr,$timeout,$auth,$user,$pass,$from);
if($em->send_mail_sock(‘標題‘,‘歡迎和諧‘,‘發送給某人(張三)的郵件地址‘,‘你的大名‘,0)){
//$em->send_mail_sock(‘標題‘,‘內容‘,‘[email protected]‘,‘連絡人名稱‘,0)
echo("郵件發送成功Ok....");
}
}

?>

Php郵件發送源碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.