PHP SMTP发送邮件函数
作者:jao 发布于:2012-12-16 0:17
分类:PHP教程
003 |
'sitename' => '网站名称' , |
008 |
'server' => 'smtp.abc.com' , |
011 |
'username' => 'admin@abc.com' , |
012 |
'password' => '123456' , |
014 |
'mailfrom' => 'admin@abc.com' |
017 |
function sendmail( $mail_to , $mail_subject , $mail_message ) { |
019 |
global $mail , $bfconfig ; |
021 |
date_default_timezone_set( 'PRC' ); |
023 |
$mail_subject = '=?' . $mail [ 'charset' ]. '?B?' . base64_encode ( $mail_subject ). '?=' ; |
024 |
$mail_message = chunk_split ( base64_encode (preg_replace( "/(^|(\r\n))(\.)/" , "\1.\3" , $mail_message ))); |
027 |
$headers .= "MIME-Version:1.0\r\n" ; |
028 |
$headers .= "Content-type:text/html\r\n" ; |
029 |
$headers .= "Content-Transfer-Encoding: base64\r\n" ; |
030 |
$headers .= "From: " . $bfconfig [ 'sitename' ]. "<" . $mail [ 'mailfrom' ]. ">\r\n" ; |
031 |
$headers .= "Date: " . date ( "r" ). "\r\n" ; |
032 |
list( $msec , $sec ) = explode ( " " , microtime()); |
033 |
$headers .= "Message-ID: <" . date ( "YmdHis" , $sec ). "." .( $msec * 1000000). "." . $mail [ 'mailfrom' ]. ">\r\n" ; |
035 |
if (! $fp = fsockopen ( $mail [ 'server' ], $mail [ 'port' ], $errno , $errstr , 30)) { |
036 |
exit ( "CONNECT - Unable to connect to the SMTP server" ); |
039 |
stream_set_blocking( $fp , true); |
041 |
$lastmessage = fgets ( $fp , 512); |
042 |
if ( substr ( $lastmessage , 0, 3) != '220' ) { |
043 |
exit ( "CONNECT - " . $lastmessage ); |
046 |
fputs ( $fp , ( $mail [ 'auth' ] ? 'EHLO' : 'HELO' ). " befen\r\n" ); |
047 |
$lastmessage = fgets ( $fp , 512); |
048 |
if ( substr ( $lastmessage , 0, 3) != 220 && substr ( $lastmessage , 0, 3) != 250) { |
049 |
exit ( "HELO/EHLO - " . $lastmessage ); |
053 |
if ( substr ( $lastmessage , 3, 1) != '-' || empty ( $lastmessage )) { |
056 |
$lastmessage = fgets ( $fp , 512); |
060 |
fputs ( $fp , "AUTH LOGIN\r\n" ); |
061 |
$lastmessage = fgets ( $fp , 512); |
062 |
if ( substr ( $lastmessage , 0, 3) != 334) { |
066 |
fputs ( $fp , base64_encode ( $mail [ 'username' ]). "\r\n" ); |
067 |
$lastmessage = fgets ( $fp , 512); |
068 |
if ( substr ( $lastmessage , 0, 3) != 334) { |
069 |
exit ( "AUTH LOGIN - " . $lastmessage ); |
072 |
fputs ( $fp , base64_encode ( $mail [ 'password' ]). "\r\n" ); |
073 |
$lastmessage = fgets ( $fp , 512); |
074 |
if ( substr ( $lastmessage , 0, 3) != 235) { |
075 |
exit ( "AUTH LOGIN - " . $lastmessage ); |
078 |
$email_from = $mail [ 'mailfrom' ]; |
081 |
fputs ( $fp , "MAIL FROM: <" .preg_replace( "/.*\<(.+?)\>.*/" , "\\1" , $email_from ). ">\r\n" ); |
082 |
$lastmessage = fgets ( $fp , 512); |
083 |
if ( substr ( $lastmessage , 0, 3) != 250) { |
084 |
fputs ( $fp , "MAIL FROM: <" .preg_replace( "/.*\<(.+?)\>.*/" , "\\1" , $email_from ). ">\r\n" ); |
085 |
$lastmessage = fgets ( $fp , 512); |
086 |
if ( substr ( $lastmessage , 0, 3) != 250) { |
087 |
exit ( "MAIL FROM - " . $lastmessage ); |
091 |
foreach ( explode ( ',' , $mail_to ) as $touser ) { |
092 |
$touser = trim( $touser ); |
094 |
fputs ( $fp , "RCPT TO: <" .preg_replace( "/.*\<(.+?)\>.*/" , "\\1" , $touser ). ">\r\n" ); |
095 |
$lastmessage = fgets ( $fp , 512); |
096 |
if ( substr ( $lastmessage , 0, 3) != 250) { |
097 |
fputs ( $fp , "RCPT TO: <" .preg_replace( "/.*\<(.+?)\>.*/" , "\\1" , $touser ). ">\r\n" ); |
098 |
$lastmessage = fgets ( $fp , 512); |
099 |
exit ( "RCPT TO - " . $lastmessage ); |
104 |
fputs ( $fp , "DATA\r\n" ); |
105 |
$lastmessage = fgets ( $fp , 512); |
106 |
if ( substr ( $lastmessage , 0, 3) != 354) { |
107 |
exit ( "DATA - " . $lastmessage ); |
110 |
fputs ( $fp , $headers ); |
111 |
fputs ( $fp , "To: " . $mail_to . "\r\n" ); |
112 |
fputs ( $fp , "Subject: $mail_subject\r\n" ); |
113 |
fputs ( $fp , "\r\n\r\n" ); |
114 |
fputs ( $fp , "$mail_message\r\n.\r\n" ); |
115 |
$lastmessage = fgets ( $fp , 512); |
116 |
if ( substr ( $lastmessage , 0, 3) != 250) { |
117 |
exit ( "END - " . $lastmessage ); |
120 |
fputs ( $fp , "QUIT\r\n" ); |
标签: php教程 PHP
et_highlighter