Last active
July 27, 2016 05:33
-
-
Save alash3al/4c89047bbb52ab928bce to your computer and use it in GitHub Desktop.
PHP mail() alternative, smtp()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Send a mail using smtp server | |
* | |
* @param array $options [from, to, server, tls, subject, message .. etc] | |
* @param array $headers for custome headers as key value pairs [key is case insensetive] | |
* | |
* @author Mohammed Al Ashaal <fb.com/alash3al, is.gd/alash3al, github.com/alash3al> | |
* @copyright (c) 2015, Mohammed Al Ashaal | |
* @version 1.0.0 | |
* | |
* @return object | |
*/ | |
function smtp(array $options = array(), array $headers = array()) | |
{ | |
// get the error-reporting state | |
$e = error_reporting(); | |
// disable errors [temp] | |
error_reporting(0); | |
// the options array | |
$options = array_merge(array( | |
'server' => '127.0.0.1', | |
'port' => 25, | |
'domain' => $_SERVER['SERVER_NAME'], | |
'user' => '', | |
'pass' => '', | |
'tls' => false, | |
'from' => '', | |
'to' => '', | |
'subject' => '', | |
'message' => '' | |
), $options); | |
// the headers array | |
$headers = array_merge(array | |
( | |
'to' => '<'.($options['to']).'>', | |
'from' => '<'.($options['from']).'>', | |
'subject' => $options['subject'], | |
'content-type' => 'text/html; charset=UTF-8', | |
'mime-version' => '1.0', | |
'x-mailer' => 'PHP/' . phpversion(), | |
'x-sender' => $options['from'] | |
), array_change_key_case($headers, CASE_LOWER)); | |
// the commands array | |
$cmds = array(); | |
// initialize the socket client | |
$soc = @stream_socket_client($options['server'], $errno, $errstr); | |
// return the error string if there is an error | |
if ( $errno || !$soc ) | |
return trim($errstr); | |
// fetch the remote info | |
$cmds['START'] = trim(fread($soc, 4096)); | |
// say hello | |
fputs($soc, 'HELO ' . $options['domain'] . PHP_EOL); | |
$cmds['HELO'] = trim(fread($soc, 4096)); | |
// only send STARTTLS if required | |
if ( $options['tls'] ) | |
{ | |
fputs($soc, 'STARTTLS' . PHP_EOL); | |
$cmds['TLS'] = trim(fread($soc, 4096)); | |
} | |
// auth. if required | |
if ( !empty($options['user']) && !empty($options['pass']) ) | |
{ | |
fputs($soc, 'AUTH LOGIN' . PHP_EOL); | |
$cmds['LOGIN'] = trim(fread($soc, 4096)); | |
$cmds['LOGIN'] = base64_decode(substr($cmds['LOGIN'], 4)); | |
fputs($soc, base64_encode($options['user']) . PHP_EOL); | |
$cmds['USER'] = trim(fread($soc, 4096)); | |
fputs($soc, base64_encode($options['pass']) . PHP_EOL); | |
$cmds['PASS'] = trim(fread($soc, 4096)); | |
} | |
// The From section | |
fputs($soc, sprintf('MAIL FROM: <%s>', $options['from']) . PHP_EOL); | |
$cmds['FROM'] = trim(fread($soc, 4096)); | |
// The To section | |
fputs($soc, sprintf('RCPT TO: <%s>', $options['to']) . PHP_EOL); | |
$cmds['RCPT'] = trim(fread($soc, 4096)); | |
// The Data command | |
fputs($soc, 'DATA' . PHP_EOL); | |
$cmds['DATA'] = trim(fread($soc, 4096)); | |
// the headers + body | |
$h = ''; | |
// iterate over the headers array and generate | |
// the valid string . | |
foreach ( $headers as $k => $v ) { | |
$h .= sprintf('%s: %s', str_replace(' ', '-', ucwords(str_replace('-', ' ', $k))), $v) . PHP_EOL; | |
unset($headers[$k]); | |
} | |
// \r\n\r\n | |
// message | |
// \r\n.\r\n | |
$h .= PHP_EOL . PHP_EOL; | |
$h .= $options['message'] . PHP_EOL; | |
$h .= '.' . PHP_EOL; | |
// send the headers+body | |
fputs($soc, $h); | |
$cmds['SEND'] = trim(fread($soc, 4096)); | |
// request it to exit | |
fputs($soc, 'QUIT' . PHP_EOL); | |
$cmds['QUIT'] = trim(fread($soc, 4096)); | |
// close the socket client | |
fclose($soc); | |
// roll-back the error-state | |
error_reporting($e); | |
// return an object of status-code + commands log | |
return (object) array( | |
'status_code' => (intval(substr($cmds['QUIT'], 0, 3)) == 221), | |
'log' => &$cmds | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: