CURL邮件发送
我的服务器是虚拟主机,主机提供商在php.ini里设置了禁止开Socket,结果任何一个邮件服务器都连不上,更别谈发邮件了,又是windows主机,也没有配置sendmail.exe,搞得很不爽,死活发布出去,,值得庆幸的是,还是可以使用CURL,于是我专门针对webmail.cgi程序写了个CURL的PHP客户端用来发邮件。。嘿嘿,问题搞定。
<?php
define("WM_MAIN_URL", "http://mail.youdomain.com/cgi-bin/webmail.cgi");
class WebMailer
{
var $referer="http://mail.youdomain.com/cgi-bin/webmail.cgi";
var $agent="Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1";
var $handler;
function WebMailer() {
$this->handle=curl_init();
}
function close()
{
if($this->handle)
{
curl_close($this->handle);
}
}
function login()
{
$postdata = "";
$postdata .= "username=webmaster";
$postdata .= "&logindomain=yourdomain.com";
$postdata .= "&password=yourpassword";
$postdata .= "&do.login=".urlencode("账号登录");
$postdata .= "&locale=zh-cn";
$postdata .= "&sameip=0";
curl_setopt($this->handle, CURLOPT_HEADER, 1);
curl_setopt($this->handle, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($this->handle, CURLOPT_POST, 1);
curl_setopt($this->handle, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($this->handle, CURLOPT_REFERER, $this->referer);
curl_setopt($this->handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->handle, CURLOPT_URL, WM_MAIN_URL);
curl_setopt($this->handle, CURLOPT_USERAGENT,$this->agent);
$response=curl_exec($this->handle);
if($response)
{
$rurl= curl_getinfo($this->handle,CURLINFO_EFFECTIVE_URL);
$this->baseURL=substr($rurl,0,strlen($rurl)-23);
}
else
{
return "login error,connect failed!";
}
return "ok";
}
function sendmail($from, $to,$subject,$message)
{
$sentFormURL=$this->baseURL."?folder=INBOX&newmsg=1&form=newmsg";
curl_setopt($this->handle, CURLOPT_URL, $sentFormURL);
curl_setopt($this->handle, CURLOPT_HEADER, 1);
curl_setopt($this->handle, CURLOPT_GET, 1);
curl_setopt($this->handle, CURLOPT_REFERER,$this->baseURL."?folder=INBOX&newmsg=1&form=left");
$response=curl_exec($this->handle);
if($response)
{
$response=new CurlResponse($response);
if($response->headers['Status-Code'] != '200')
{
return "get token error,not 200,actually is [".$response->headers['Status-Code']."]";
}
}
else
{
return "get token error,connect failed!";
}
$tofind="<input type=\"hidden\" name=\"msgtoken\" value=\"";
$token=strstr($response->body,$tofind);
if(!$token)
{
return "get token error,can't locate token string!content is [".$response->body."]";
}
//find the end quote
$tokenEndPos=strpos($token,"\"",strlen($tofind));
$token=substr($token,strlen($tofind),$tokenEndPos+1-strlen($tofind));
//request the final send mail
$postdata = "";
$postdata .= "folder=INBOX";
$postdata .= "&form=donewmsg";
$postdata .= "&pos=";
$postdata .= "&focusto=headers";
$postdata .= "&msgtoken=".$token;
$postdata .= "&headerto=".urlencode($to);
$postdata .= "&headercc=";
$postdata .= "&headerbcc=";
$postdata .= "&headerfrom=".urlencode($from);
$postdata .= "&headerreply-to=";
$postdata .= "&headersubject=".urlencode($subject);
$postdata .= "&message=".urlencode($message);
$postdata .= "&fcc=on";
$postdata .= "&sendmsg=%B7%A2%CB%CD";
curl_setopt($this->handle, CURLOPT_URL, $this->baseURL);
curl_setopt($this->handle, CURLOPT_POST, 1);
curl_setopt($this->handle, CURLOPT_HEADER, 1);
curl_setopt($this->handle, CURLOPT_REFERER,$sentFormURL);
curl_setopt($this->handle, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($this->handle, CURLOPT_RETURNTRANSFER, false);
$response=curl_exec($this->handle);
if(!$response)
{
return "send error, connect failed!";
}
return "ok";
}
}
class CurlResponse {
var $body = '';
var $headers = array();
function CurlResponse($response) {
# Extract headers from response
$pattern = '#HTTP/\d\.\d.*?$.*?\r\n\r\n#ims';
preg_match_all($pattern, $response, $matches);
$headers = split("\r\n", str_replace("\r\n\r\n", '', array_pop($matches[0])));
# Extract the version and status from the first header
$version_and_status = array_shift($headers);
preg_match('#HTTP/(\d\.\d)\s(\d\d\d)\s(.*)#', $version_and_status, $matches);
$this->headers['Http-Version'] = $matches[1];
$this->headers['Status-Code'] = $matches[2];
$this->headers['Status'] = $matches[2].' '.$matches[3];
# Convert headers into an associative array
foreach ($headers as $header) {
preg_match('#(.*?)\:\s(.*)#', $header, $matches);
$this->headers[$matches[1]] = $matches[2];
}
# Remove the headers from the response body
$this->body = preg_replace($pattern, '', $response);
}
function toString() {
return $this->body;
}
}
?>
找个使用webmail.cgi的邮件服务器,注册个用户,修改一下yourdomain.com,以及用户名密码就可以用了,当然也得改一下source/function_sendmail.php,改成调用WebMailer的:
} elseif($mail['mailsend'] == 2) {
$wm = new WebMailer();
$retVal=$wm->login();
if($retVal!="ok")
{
runlog("WebMailer",$retVal,0);
return false;
}
$bRet=true;
$email_subject= '['.$_SCONFIG['sitename'].'] '.$subject;
foreach($tousers as $touser) {
$touser = trim($touser);
if($touser) {
$retVal=$wm->sendmail($from,$touser, $email_subject,$message);
if($retVal != 'ok')
{
runlog("WebMailer",$retVal,0);
$bRet=false;
}
}
}
$wm->close();
return $bRet;
}