[ Index ] |
|
Code source de Dotclear 2.0-beta6 |
1 <?php 2 # ***** BEGIN LICENSE BLOCK ***** 3 # This file is part of Clearbricks. 4 # Copyright (c) 2006 Olivier Meunier and contributors. All rights 5 # reserved. 6 # 7 # Clearbricks is free software; you can redistribute it and/or modify 8 # it under the terms of the GNU General Public License as published by 9 # the Free Software Foundation; either version 2 of the License, or 10 # (at your option) any later version. 11 # 12 # Clearbricks is distributed in the hope that it will be useful, 13 # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 # GNU General Public License for more details. 16 # 17 # You should have received a copy of the GNU General Public License 18 # along with Clearbricks; if not, write to the Free Software 19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 # 21 # ***** END LICENSE BLOCK ***** 22 23 class socketMail 24 { 25 public static $fp; 26 public static $timeout = 10; 27 public static $smtp_relay = null; 28 29 public static function mail($to,$subject,$message,$headers=null) 30 { 31 $from = self::getFrom($headers); 32 33 $H = 'Return-Path: <'.$from.">\r\n"; 34 35 $from_host = explode('@',$from); 36 $from_host = $from_host[1]; 37 38 $to_host = explode('@',$to); 39 $to_host = $to_host[1]; 40 41 if (self::$smtp_relay != null) { 42 $mx = array(gethostbyname(self::$smtp_relay) => 1); 43 } else { 44 $mx = mail::getMX($to_host); 45 } 46 47 foreach ($mx as $h => $w) 48 { 49 self::$fp = @fsockopen($h,25,$errno,$errstr,self::$timeout); 50 51 if (self::$fp !== false) { 52 break; 53 } 54 } 55 56 if (!is_resource(self::$fp)) { 57 self::$fp = null; 58 throw new Exception('Unable to open socket'); 59 } 60 61 # We need to read the first line 62 fgets(self::$fp); 63 64 $data = ''; 65 # HELO cmd 66 if (!self::cmd('HELO '.$from_host,$data)) { 67 self::quit(); 68 throw new Exception($data); 69 } 70 71 # MAIL FROM: <...> 72 if (!self::cmd('MAIL FROM: <'.$from.'>',$data)) { 73 self::quit(); 74 throw new Exception($data); 75 } 76 77 # RCPT TO: <...> 78 if (!self::cmd('RCPT TO: <'.$to.'>',$data)) { 79 self::quit(); 80 throw new Exception($data); 81 } 82 83 # Compose mail and send it with DATA 84 $H = 'Return-Path: <'.$from.">\r\n"; 85 $H .= 'To: <'.$to.">\r\n"; 86 $H .= 'Subject: '.$subject."\r\n"; 87 $H .= $headers."\r\n"; 88 89 $message = $H."\r\n\r\n".$message; 90 91 if (!self::sendMessage($message,$data)) { 92 self::quit(); 93 throw new Exception($data); 94 } 95 96 97 self::quit(); 98 } 99 100 private static function getFrom($headers) 101 { 102 $f = ''; 103 if (preg_match('/^from:(?:[ <]+)(.*?)(?:$|>)/msi',$headers,$m)) { 104 $f = trim($m[1]); 105 } else { 106 $f = trim(ini_get('sendmail_from')); 107 } 108 109 if (!$f) { 110 throw new Exception('No valid from e-mail address'); 111 } 112 113 return $f; 114 } 115 116 private static function cmd($out,&$data='') 117 { 118 fwrite(self::$fp,$out."\r\n"); 119 $data = self::data(); 120 121 if (substr($data,0,3) != '250') { 122 return false; 123 } 124 125 return true; 126 } 127 128 private static function data() 129 { 130 $s=''; 131 stream_set_timeout(self::$fp, 2); 132 133 for($i=0;$i<2;$i++) { 134 $s .= fgets(self::$fp, 1024); 135 } 136 137 return $s; 138 } 139 140 private static function sendMessage($msg,&$data) 141 { 142 $msg .= "\r\n."; 143 144 self::cmd('DATA',$data); 145 146 if (substr($data,0,3) != '354') { 147 return false; 148 } 149 150 return self::cmd($msg,$data); 151 } 152 153 private static function quit() 154 { 155 self::cmd('QUIT'); 156 fclose(self::$fp); 157 self::$fp = null; 158 } 159 } 160 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Feb 23 22:16:06 2007 | par Balluche grâce à PHPXref 0.7 |