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