download this file: func.sendmail.php view text/plain: func.sendmail.php file encoding: UTF-8 [goback]
<?php
## sendmail function
##
function __get_mta($email)
{
$email = preg_replace('/[><]/','',trim($email));
list(,$host) = explode('@',$email);
if(checkdnsrr($host,"MX") && getmxrr($host,$mx,$weight))
{
asort($weight);
$keys = array_keys($weight);
foreach($keys AS $k) $hosts[] = $mx[$k];
}
else $hosts[] = $host;
return $hosts; // array
}
## http://kr.php.net/manual/en/ref.mail.php
##
function _sendmailauth($to, $from, $subject, $message)
{
/* your configuration here */
//$smtpServer = "mail.yourdomain.com"; //ip accepted as well
$port = 25; // should be 25 by default
$timeout = 10; //typical timeout. try 45 for slow servers
$username = 'webform'; //the login for your smtp
$password = 'passhere'; //the pass for your smtp
$localhost = '127.0.0.1'; //this seems to work always
$newLine = "\r\n"; //var just for nelines in MS
$secure = 0; //change to 1 if you need a secure connect
$smtps = __get_mta($to); // array
$to = preg_replace('/[><]/','',$to);
$from = preg_replace('/[><]/','',$from);
/* you shouldn't need to mod anything else */
//connect to the host and port
## patch san2@2008.03.11
for($rt=0; $rt<4; $rt++)
{
foreach($smtps AS $smtpServer)
{
if($smtpConnect=@fsockopen($smtpServer,$port,$errno,$errstr,$timeout))
{ break; }
}
if($smtpConnect) break;
}
$smtpResponse = @fgets($smtpConnect, 4096);
if(empty($smtpConnect))
{
$output = "Failed to connect: $smtpResponse";
return $output;
} else
{
$logArray['connection'] = "Connected to: $smtpResponse";
}
//say HELO to our little friend
@fputs($smtpConnect, "HELO $localhost". $newLine);
$smtpResponse = @fgets($smtpConnect, 4096);
$logArray['heloresponse'] = "$smtpResponse";
//start a tls session if needed
if($secure)
{
@fputs($smtpConnect, "STARTTLS". $newLine);
$smtpResponse = @fgets($smtpConnect, 4096);
$logArray['tlsresponse'] = "$smtpResponse";
//you have to say HELO again after TLS is started
@fputs($smtpConnect, "HELO $localhost". $newLine);
$smtpResponse = @fgets($smtpConnect, 4096);
$logArray['heloresponse2'] = "$smtpResponse";
}
/***
//request for auth login
fputs($smtpConnect,"AUTH LOGIN" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authrequest'] = "$smtpResponse";
//send the username
fputs($smtpConnect, base64_encode($username) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authusername'] = "$smtpResponse";
//send the password
fputs($smtpConnect, base64_encode($password) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authpassword'] = "$smtpResponse";
***/
//email from
@fputs($smtpConnect, "MAIL FROM: <".$from.">".$newLine);
$smtpResponse = @fgets($smtpConnect, 4096);
$logArray['mailfromresponse'] = "$smtpResponse";
//email to
@fputs($smtpConnect, "RCPT TO: <".$to.">".$newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['mailtoresponse'] = "$smtpResponse";
//the email
@fputs($smtpConnect, "DATA" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['data1response'] = "$smtpResponse";
//construct headers
$headers = "MIME-Version: 1.0" . $newLine;
$headers .= "Content-type: text/html; charset=euc-kr" . $newLine;
$headers .= "To: <".$to.">" . $newLine;
$headers .= "From: <".$from.">" . $newLine;
$headers .= "Subject: $subject" . $newLine;
//observe the . after the newline, it signals the end of message
@fputs($smtpConnect, "$headers\r\n\r\n$message\r\n.\r\n");
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['data2response'] = "$smtpResponse";
// say goodbye
@fputs($smtpConnect,"QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['quitresponse'] = "$smtpResponse";
$logArray['quitcode'] = substr($smtpResponse,0,3);
@fclose($smtpConnect);
//a return value of 221 in $retVal["quitcode"] is a success
return $logArray;
}
function sendmail($tos, $from, $title, $body, $servicename='', $add='')
{
//$chkfile = '/dev/shm/.'.$servicename.'.send.email';
/***
if(file_exists($chkfile))
{
$atime = @fileatime($chkfile);
if((__TIME__-$atime) <= 300) return 'atime'; // don't send SMS
}
***/
$tos = preg_replace('/[><\s]/','',trim($tos));
$tos = preg_replace('/^,\s*/','',$tos);
$tos = preg_replace('/,\s*$/','',$tos);
$tos = preg_replace('/,,/',',',$tos);
$tos = preg_split('/,+/',$tos);
$tos = array_values(array_unique($tos));
$size = sizeof($tos);
for($i=0; $i<$size; $i++)
{
if(!$to = trim($tos[$i])) continue;
//$bodyadd = preg_match('/mycompany/i',$to) ? '<P>'.str_replace('https://','http://',$add) : '';
$status = _sendmailauth($to, $from, $title, $body.$bodyadd); // array
$rr = ($status['quitcode']==221) ? 'OK' : 'FAIL';
//echo '<PRE>';
//print_r($status);
$result .= $rr."($to) ";
}
//if($i > 0) @touch($chkfile);
return "SENT($i/$size) $result";
}
?>