001 | <?php |
002 | bfconfig = Array ( |
003 | 'sitename' => '网站名称', |
004 | ); |
005 | |
006 | $mail = Array ( |
007 | 'state' => 1, |
008 | 'server' => 'smtp.abc.com', |
009 | 'port' => 25, |
010 | 'auth' => 1, |
011 | 'username' => 'admin@abc.com', |
012 | 'password' => '123456', |
013 | 'charset' => 'gbk', |
014 | 'mailfrom' => 'admin@abc.com' |
015 | ); |
016 | |
017 | function sendmail($mail_to, $mail_subject, $mail_message) { |
018 | |
019 | global $mail, $bfconfig; |
020 | |
021 | date_default_timezone_set('PRC'); |
022 | |
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))); |
025 | |
026 | $headers .= ""; |
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"; |
034 | |
035 | if(!$fp = fsockopen($mail['server'], $mail['port'], $errno, $errstr, 30)) { |
036 | exit("CONNECT - Unable to connect to the SMTP server"); |
037 | } |
038 | |
039 | stream_set_blocking($fp, true); |
040 | |
041 | $lastmessage = fgets($fp, 512); |
042 | if(substr($lastmessage, 0, 3) != '220') { |
043 | exit("CONNECT - ".$lastmessage); |
044 | } |
045 | |
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); |
050 | } |
051 | |
052 | while(1) { |
053 | if(substr($lastmessage, 3, 1) != '-' || empty($lastmessage)) { |
054 | break; |
055 | } |
056 | $lastmessage = fgets($fp, 512); |
057 | } |
058 | |
059 | if($mail['auth']) { |
060 | fputs($fp, "AUTH LOGIN\r\n"); |
061 | $lastmessage = fgets($fp, 512); |
062 | if(substr($lastmessage, 0, 3) != 334) { |
063 | exit($lastmessage); |
064 | } |
065 | |
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); |
070 | } |
071 | |
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); |
076 | } |
077 | |
078 | $email_from = $mail['mailfrom']; |
079 | } |
080 | |
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); |
088 | } |
089 | } |
090 | |
091 | foreach(explode(',', $mail_to) as $touser) { |
092 | $touser = trim($touser); |
093 | if($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); |
100 | } |
101 | } |
102 | } |
103 | |
104 | fputs($fp, "DATA\r\n"); |
105 | $lastmessage = fgets($fp, 512); |
106 | if(substr($lastmessage, 0, 3) != 354) { |
107 | exit("DATA - ".$lastmessage); |
108 | } |
109 | |
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); |
118 | } |
119 | |
120 | fputs($fp, "QUIT\r\n"); |
121 | ?> |