packages/ssoinabox-webui/root/usr/local/share/ssoinabox/htdocs/includes/smtp.php
changeset 3 a044870a9d3d
equal deleted inserted replaced
2:700d61d93b1b 3:a044870a9d3d
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * Sends e-mail via SMTP.
       
     5  * @param string Source address
       
     6  * @param string Destination address
       
     7  * @param string Subject
       
     8  * @param string Message body
       
     9  * @param string Additional headers
       
    10  * @return bool
       
    11  */
       
    12 
       
    13 function smtp_mail($from, $to, $subject, $body, $headers = '')
       
    14 {
       
    15 	list($domain) = array_reverse(explode('@', $to));
       
    16 	
       
    17 	// look up MX record for $domain
       
    18 	$record = dns_get_record($domain, DNS_MX);
       
    19 	
       
    20 	if ( !isset($record[0]['target']) )
       
    21 		// failed to get target server
       
    22 		return false;
       
    23 		
       
    24 	// open socket
       
    25 	$sock = fsockopen($record[0]['target'], 25, $errno, $errstr, 5);
       
    26 	if ( !$sock )
       
    27 		// failed to open socket
       
    28 		return false;
       
    29 	
       
    30 	try
       
    31 	{
       
    32 		// wait for 220
       
    33 		if ( _smtp_get_response($sock) !== 220 )
       
    34 			throw new Exception("Expected 220");
       
    35 		
       
    36 		// HELO
       
    37 		// get server's FQDN
       
    38 		$hostname = gethostname();
       
    39 		if ( strpos($hostname, '.') === false )
       
    40 		{
       
    41 			$hostname .= '.' . trim(`dnsdomainname`);
       
    42 		}
       
    43 		fputs($sock, "HELO $hostname\r\n");
       
    44 		if ( _smtp_get_response($sock) !== 250 )
       
    45 			throw new Exception("Expected 250");
       
    46 		
       
    47 		// from
       
    48 		fputs($sock, "MAIL FROM: <$from>\r\n");
       
    49 		if ( _smtp_get_response($sock) !== 250 )
       
    50 			throw new Exception("Expected 250");
       
    51 		
       
    52 		// to
       
    53 		fputs($sock, "RCPT TO: <$to>\r\n");
       
    54 		if ( _smtp_get_response($sock) !== 250 )
       
    55 			throw new Exception("Expected 250");
       
    56 		
       
    57 		// data
       
    58 		fputs($sock, "DATA\r\n");
       
    59 		if ( !in_array(_smtp_get_response($sock), array(354, 250)) )
       
    60 			throw new Exception("Expected 250 or 354");
       
    61 		
       
    62 		// send headers
       
    63 		$full_headers = "Subject: $subject\r\n";
       
    64 		$full_headers .= "From: <$from>\r\n";
       
    65 		if ( !strstr($headers, "To: ") )
       
    66 			$full_headers .= "To: <$to>\r\n";
       
    67 		if ( !empty($headers) )
       
    68 			$full_headers .= trim(str_replace("\n", "\r\n", str_replace("\r\n", "\n", $headers))) . "\r\n";
       
    69 		
       
    70 		$full_headers .= "\r\n";
       
    71 		fputs($sock, $full_headers);
       
    72 		
       
    73 		// send body
       
    74 		$body = str_replace("\n", "\r\n", str_replace("\r\n", "\n", $body));
       
    75 		fputs($sock, $body);
       
    76 		
       
    77 		// send end marker
       
    78 		fputs($sock, "\r\n.\r\n");
       
    79 		if ( _smtp_get_response($sock) !== 250 )
       
    80 			throw new Exception("Expected 250");
       
    81 		
       
    82 		// end session
       
    83 		fputs($sock, "QUIT\r\n");
       
    84 		if ( _smtp_get_response($sock) !== 221 )
       
    85 			throw new Exception("Expected 221");
       
    86 		
       
    87 		fclose($sock);
       
    88 	} catch ( Exception $e )
       
    89 	{
       
    90 		fputs($sock, "QUIT\r\n");
       
    91 		_smtp_get_response($sock);
       
    92 		fclose($sock);
       
    93 		return false;
       
    94 	}
       
    95 	return true;
       
    96 }
       
    97 
       
    98 function _smtp_get_response($sock)
       
    99 {
       
   100 	while ( !feof($sock) && ($line = fgets($sock, 8192)) )
       
   101 	{
       
   102 		if ( preg_match('/^([0-9]+)(\s.+)?$/', $line, $match) )
       
   103 		{
       
   104 			return intval($match[1]);
       
   105 		}
       
   106 	}
       
   107 	return false;
       
   108 }
       
   109 
       
   110 // smtp_mail('plates@csh.rit.edu', 'plates@csh.rit.edu', 'Test e-mail', 'Testing', 'From: Plates <plates@csh.rit.edu>');
       
   111