Created
May 2, 2012 15:50
-
-
Save JeffLutzenberger/2577687 to your computer and use it in GitHub Desktop.
smtpmail class
This file contains 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 | |
class SmtpMail | |
{ | |
private $server; | |
private $user; | |
private $pass; | |
private $port = 25; | |
public function __construct($server="localhost", $port=25, $user=null, $pass=null) | |
{ | |
$this->server = $server; | |
$this->user = base64_encode($user); | |
$this->pass = base64_encode($pass); | |
if (is_numeric($port)) | |
$this->port = $port; | |
} | |
public function sendMail($pairdata) | |
{ | |
$ret_val = array(); | |
if ($sock = fsockopen($this->server, $this->port)) | |
{ | |
// TODO: get the correct hostname via product config... | |
// also, smtpmail() uses HELO, but our smtp servers support ESMTP, so this | |
// should be passed as EHLO to tell the server that we want ESMTP, not SMTP | |
fputs ($sock, "EHLO webwc10.int.rightnowtech.com\r\n"); | |
$ret_val["hello"] = fgets ($sock, 1024); | |
// RightNow servers don't require auth, but here is support if needed | |
if (isset($this->user) && $this->user != null && | |
isset($this->pass) && $this->pass != null) | |
{ | |
fputs($sock, "auth login\r\n"); | |
$ret_val["res"] = fgets($sock, 1024); | |
fputs($sock, $this->user."\r\n"); | |
$ret_val["user"] = fgets($sock, 1024); | |
fputs($sock, $this->pass."\r\n"); | |
$ret_val["pass"] = fgets($sock, 256); | |
} | |
fputs ($sock, "MAIL FROM: <".$pairdata["from"].">\r\n"); | |
$ret_val["from"] = fgets($sock, 1024); | |
fputs ($sock, "RCPT TO: <".$pairdata["to"].">\r\n"); | |
$ret_val["to"] = fgets($sock, 1024); | |
fputs($sock, "DATA\r\n"); | |
$ret_val["data"] = fgets($sock, 1024); | |
$msg = $this->_generateData($pairdata); | |
fputs($sock, $msg); | |
$ret_val["send"] = fgets($sock, 256); | |
// tell the server to disconnect, and close the socket | |
fputs ($sock, "QUIT\r\n"); | |
fclose($sock); | |
} | |
// clean up the ret_val | |
foreach (array_keys($ret_val) as $k) | |
$ret_val[$k] = trim($ret_val[$k]); | |
return $ret_val; | |
} | |
private function _generateData(&$arr) | |
{ | |
$msg = sprintf("To: %s\r\nFrom: %s\r\nSubject: %s\r\n", $arr["to"], $arr["from"], $arr["subject"]); | |
// add custom headers | |
if (isset($arr["custom_headers"]) && is_array($arr["custom_headers"])) | |
{ | |
foreach ($arr["custom_headers"] as $k => $v) | |
{ | |
$msg .= sprintf("%s: %s\r\n", $k, $v); | |
} | |
} | |
// add the body of the msg, and put a . on a blank line by itself to indicate the end of the msg to the server | |
// per the RFC | |
$msg .= sprintf("\r\n%s\r\n.\r\n", $arr["body"]); | |
return $msg; | |
} | |
} | |
$pairdata = array( | |
"from" => "Luke Davison <ldavison@rightnow.com>", | |
"to" => "Luke Davison <ldavison@rightnow.com>", | |
"subject" => "this is a test subject", | |
"body" => "this is a test body", | |
"custom_headers" => array( | |
"X-Mailer" => "my super awesome happy mailer script", | |
"X-Priority" => "3 (normal)", | |
), | |
); | |
header("Content-type: text/plain"); | |
$mail = new SmtpMail(); | |
$res = $mail->sendMail($pairdata); | |
print_r($res); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment