Forum Moderators: coopster
<?php
class mimemail {
var $headers= array(
'MIME-version'=> "1.0",
'Return-path'=> "",
'Date'=> "",
'From'=> "",
'Subject'=> "",
'To'=> array(),
'Cc'=> array(),
'Bcc'=> array(),
'X-Mailer'=> "",
'Content-type'=> "",
);
var $message= "";
var $charset= "iso-8859-1";
var $boundary= array();
var $filetypes= array(
'gif'=> "image/gif",
);
var $versionhtml= "";
var $versionplain= "";
var $parts= array();
var $files= array();
var $attachments= array();
function mimemail($flags=array()) {
$this->headers['Date'] = date("D, d M Y H:i:s O",time()); // should be "...O (T)" but T sometimes returns the longer version of the time zone
$this->boundary['mixed'] = md5(uniqid(microtime()));
$this->boundary['related'] = md5(uniqid(microtime()));
$this->boundary['alternative'] = md5(uniqid(microtime()));
}
function compilemail() {
if((empty($this->headers['To']) && empty($this->headers['Cc']) && empty($this->headers['Bcc'])) ¦¦ (empty($this->headers['From']) && empty($this->headers['Return-path'])))
return $this->error("Some required headers are missing.");
if($this->versionplain == "" && $this->versionhtml!= "")
$this->versionplain = strip_tags($this->versionhtml);
if(!empty($this->attachments)) {
$this->headers['Content-type'] = "multipart/mixed; boundary=\"Part-{$this->boundary['mixed']}\"";
$this->message .= "--Part-{$this->boundary['mixed']}\r\n";
}
else if(!empty($this->files))
$this->headers['Content-type'] = "multipart/related; boundary=\"Part-{$this->boundary['related']}\"";
else if(!empty($this->versionhtml))
$this->headers['Content-type'] = "multipart/alternative; boundary=\"Part-{$this->boundary['alternative']}\"";
else
$this->headers['Content-type'] = "text/plain; charset=\"us-ascii\"";
if(!empty($this->files) &&!empty($this->attachments))
$this->message .= $this->wrapheader("Content-type: multipart/related; boundary=\"Part-{$this->boundary['related']}\"\r\n\r\n");
if(!empty($this->files))
$this->message .= "--Part-{$this->boundary['related']}\r\n";
if(!empty($this->versionhtml) && (!empty($this->files) ¦¦!empty($this->attachments)))
$this->message .= $this->wrapheader("Content-type: multipart/alternative; boundary=\"Part-{$this->boundary['alternative']}\"\r\n\r\n");
if(!empty($this->versionhtml))
$this->message .= "--Part-{$this->boundary['alternative']}\r\n";
if(!empty($this->versionhtml) ¦¦!empty($this->files) ¦¦!empty($this->attachments)) {
$this->message .= "Content-type: text/plain; charset=\"us-ascii\"\r\n";
$this->message .= "Content-transfer-encoding: 7bit\r\n\r\n";
$this->message .= $this->versionplain."\r\n\r\n";
}
else
$this->message = $this->versionplain;
if(!empty($this->versionhtml)) {
$this->message .= "--Part-{$this->boundary['alternative']}\r\n";
$this->message .= "Content-type: text/html; charset=\"{$this->charset}\"\r\n";
$this->message .= "Content-transfer-encoding: quoted-printable\r\n\r\n";
$this->message .= $this->versionhtml."\r\n\r\n";
$this->message .= "--Part-{$this->boundary['alternative']}--\r\n";
}
if(!empty($this->files)) {
$this->compileembedded();
$this->message .= "--Part-{$this->boundary['related']}--\r\n";
}
if(!empty($this->attachments)) {
$this->compileattachments();
$this->message .= "--Part-{$this->boundary['mixed']}--\r\n";
}
$headers = array();
foreach($this->headers as $k => $v) {
if(is_array($v) &&!empty($v))
$headers[$k] = $v;
else if($v!= "" &&!empty($v))
$headers[$k] = wordwrap("$k: $v",75,"\r\n ");
}
$this->headers = $headers;
}
function compileembedded() {
foreach($this->files as $current) {
$this->message .= "--Part-{$this->boundary['related']}\r\n";
$this->message .= $this->wrapheader("Content-type: {$current['type']}; name=\"{$current['name']}\"\r\n");
$this->message .= "Content-ID: <{$current['cid']}>\r\n";
$this->message .= "Content-transfer-encoding: base64\r\n\r\n";
$this->message .= "{$current['contents']}\r\n\r\n";
}
}
function compileattachments() {
foreach($this->attachments as $current) {
$this->message .= "--Part-{$this->boundary['mixed']}\r\n";
$this->message .= $this->wrapheader("Content-type: {$current['type']}; name=\"{$current['name']}\"\r\n");
$this->message .= "Content-disposition: attachment; filename=\"{$current['name']}\"\r\n";
$this->message .= "Content-transfer-encoding: base64\r\n\r\n";
$this->message .= "{$current['contents']}\r\n\r\n";
}
}
function sethtml($data) {
$this->versionhtml = $this->toquotedprintable($this->parsehtml($data));
//echo($data);
//echo($this->versionhtml);
}
function setplain($data) {
$this->versionplain = $this->to7bit($data);
}
function setheader($name,$data) {
if(is_array($this->headers[$name]))
$this->headers[$name][] = $data;
else
$this->headers[$name] = $data;
}
function setcharset($data) {
$this->charset = $data;
}
function setsender($email,$name=null) {
if(!$this->checkemail($email))
return $this->error("$email is not a valid sender address.");
$this->headers['From'] = "$name <$email>";
if(empty($this->headers['Return-path']))
$this->headers['Return-path'] = "$name <$email>";
}
function setreturn($email,$name=null) {
if(!$this->checkemail($email))
return $this->error("$email is not a valid return address.");
$this->headers['Return-path'] = "$name <$email>";
}
function setsubject($data) {
$this->headers['Subject'] = $data;
}
function addrecipient($email,$type) {
$type = ucfirst($type);
if(($type!= "To" && $type!= "Cc" && $type!= "Bcc") ¦¦!$this->checkemail($email))
return $this->error("$email is not a valid recipient.");
$this->headers[$type][] = $email;
}
function setxmailer($data) {
$this->headers['X-Mailer'] = $data;
}
function addattachment($filename,$data=null) {
$file = array();
if($data == null) {
if($fp = @fopen($filename,"rb")) {
$data = fread($fp,filesize($filename));
@fclose($fp);
}
else
$data = "";
}
$file['name'] = substr($filename,strstr($filename,"/")? strrpos($filename,"/")+1 : 0);
$file['type'] = strstr($filename,".")? substr($filename,strrpos($filename,".")+1) : "";
$file['contents'] = $this->tobase64($data);
if(!empty($this->attachtypes[$file['type']]))
$file['type'] = $this->attachtypes[$file['type']];
else
$file['type'] = "application/octet-stream";
$this->attachments[] = $file;
}
function addembedded($data,$filename) {
$file = array();
$file['cid'] = md5(uniqid(microtime()));
$file['name'] = substr($filename,strstr($filename,"/")? strrpos($filename,"/")+1 : 0);
$file['type'] = $this->filetypes[substr($filename,strrpos($filename,".")+1)];
$file['contents'] = $this->tobase64($data);
$this->files[] = $file;
return $file['cid'];
}
function parsehtml($data) {
global $HTTP_SERVER_VARS;
preg_match_all("/([\"\']{1}[^(\"¦\')]+\.(".(implode("¦",array_flip($this->filetypes))).")[\"\']{1})/Ui",$data,$filelist);
$filelist = array_unique($filelist[0]);
foreach($filelist as $current) {
$current = substr($current,1,strlen($current)-2);
if(preg_match("/^((http:\/\/)¦\/)/",$current) &&!empty($HTTP_SERVER_VARS['DOCUMENT_ROOT'])) {
$temp = preg_replace("/^((http:\/\/([^\/])+\/)¦\/){1}/Ui",$HTTP_SERVER_VARS['DOCUMENT_ROOT']."/",$current);
if($fp = @fopen($temp,"rb")) {
$filedata = fread($fp,filesize($temp));
@fclose($fp);
}
}
if(empty($filedata) && $fp = @fopen($current,"rb")) {
$filedata = fread($fp,1048576);
@fclose($fp);
}
if(empty($filedata))
$filesrc = preg_replace("/^(\/){1}[^\/]+\//Ui","http://".$HTTP_SERVER_VARS['HTTP_HOST']."/",$current);
else
$filesrc = "cid:" . $this->addembedded($filedata,substr($current,strstr($current,"/")? strrpos($current,"/")+1 : 0));
$data = str_replace($current,$filesrc,$data);
}
return $data;
}
function wrapheader($data) {
return wordwrap($data,75,"\r\n ",1);
}
function to7bit($data) {
$data = str_replace("\r\n","\n",$data);
$data = str_replace("\r","\n",$data);
$data = str_replace("\n","\r\n",$data);
return wordwrap($data,75,"\r\n",1);
}
function toquotedprintable($data) {
for($whitespace = $encoded = "",$line = 0,$i = 0; $i < strlen($data); $i++) {
$character = $data[$i];
$order = ord($character);
$encode = 0;
switch($order) {
case 9:
case 32:
$whitespace = $character;
$character = "";
break;
case 10:
case 13:
$encoded .= $character;
$line = 0;
continue 2;
default:
if($order < 32 ¦¦ $order > 127 ¦¦!strcmp($character,"="))
$encode = 1;
break;
}
if(strcmp($whitespace,"")) {
if($line + 1 > 75) {
$encoded .= "=\r\n";
$line = 0;
}
$encoded .= $whitespace;
$whitespace = "";
$line++;
}
if(strcmp($character,"")) {
if($encode) {
$character = sprintf("=%02X",$order);
$lengthencoded = 3;
}
else
$lengthencoded = 1;
if($line + $lengthencoded > 75) {
$encoded .= "=\r\n";
$line = 0;
}
$encoded .= $character;
$line += $lengthencoded;
}
}
return $encoded;
}
function tobase64($data) {
return wordwrap(base64_encode($data),76,"\r\n",1);
}
function checkemail($data) {
if(preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[_a-z0-9-]+)*$/i",$data))
return 1;
return 0;
}
function error($error) {
$this->errors[] = $error;
return 0;
}
}
class smtpmail {
var $hostname= "";
var $hostaddr= "";
var $smtpport= "";
function smtpmail($hostname,$smtpport=25) {
$this->hostname = $hostname;
$this->hostaddr = gethostbyname($hostname);
$this->smtpport = $smtpport;
//echo("hai $this->hostname :$this->hostaddr");
}
function sendmail($headers,$message) {
//echo("$headers :$message hai this ");
fputs($this->server,"MAIL FROM:".(substr($headers['Return-path'],strrpos($headers['Return-path'],"<")+1,strlen($headers['Return-path'])-strrpos($headers['Return-path'],"<")-2))."\r\n");
$response = fgets($this->server,1024);
if(substr($response,0,3)!= "250")
return $this->error("Invalid from address.");
//echo("hai sendmail2");
$first = 1;
if(!empty($headers['To']))
foreach($headers['To'] as $k => $v) {
fputs($this->server,"RCPT TO:$v\r\n");
//echo("hai sendmail3");
if(!empty($first)) {
fgets($this->server,1024);
unset($first);
}
$response = fgets($this->server,1024);
if(substr($response,0,3)!= "250" && substr($response,0,3)!= "251")
unset($headers['To'][$k]);
}
if(!empty($headers['Cc']))
foreach($headers['Cc'] as $k => $v) {
fputs($this->server,"RCPT TO:$v\r\n");
if(!empty($first)) {
fgets($this->server,1024);
unset($first);
}
$response = fgets($this->server,1024);
if(substr($response,0,3)!= "250" && substr($response,0,3)!= "251")
unset($headers['Cc'][$k]);
}
if(!empty($headers['Bcc']))
foreach($headers['Bcc'] as $k => $v) {
fputs($this->server,"RCPT TO:$v\r\n");
//echo("hai sendmail13");
if(!empty($first)) {
fgets($this->server,1024);
unset($first);
}
echo("hai sendmail14");
$response = fgets($this->server,1024);
if(substr($response,0,3)!= "250" && substr($response,0,3)!= "251")
unset($headers['Bcc'][$k]);
}
if(empty($headers['To']) && empty($headers['Cc']) && empty($headers['Bcc'])) {
fputs($this->server,"RSET\r\n");
return $this->error("No recipients specified.");
}
else {
if(!empty($headers['To']))
$headers['To'] = "To: ".implode(", ",$headers['To']);
if(!empty($headers['Cc']))
$headers['Cc'] = "Cc: ".implode(", ",$headers['Cc']);
if(!empty($headers['Bcc']))
unset($headers['Bcc']);
}
fputs($this->server,"DATA\r\n");
$response = fgets($this->server,1024);
if(substr($response,0,3)!= "354")
return $this->error("Cannot send data.");
$returnpath = $headers['Return-path'];
unset($headers['Return-path']);
$message = str_replace("\r\n.\r\n","\r\n..\r\n",$message);
$headers = implode("\r\n",$headers);
fputs($this->server,$headers."\r\n".$message."\r\n.\r\n");
$response = fgets($this->server,1024);
if(substr($response,0,3)!= "250")
return $this->error("Cannot send data.");
}
function openconnection() {
if(!$this->server = @fsockopen($this->hostaddr,$this->smtpport))
return $this->error("Could not connect to SMTP server.");
fputs($this->server,"HELO {$this->hostname}\r\n");
$response = fgets($this->server,1024);
if(substr($response,0,3)!= "220")
return $this->error("Could not connect to SMTP server.");
}
function closeconnection() {
fputs($this->server,"QUIT\r\n");
$response = fgets($this->server,1024);
if(substr($response,0,3)!= "221")
return $this->error("Could not close connection to SMTP server.");
@fclose($this->server);
}
function error($error) {
$this->errors[] = $error;
return 0;
}
}?>
//and example of using : and i am not able to send mail ****
<?php
require("./mail.php");
$testmail = new mimemail();
$testserver = new smtpmail("shawmail");
$testmail->addrecipient("darksnoopy@shaw.ca", "To");
$testmail->setsender("example@test.com", "A. J. Somebody");
$testmail->setreturn("darksnoopy@shaw.ca");
$testmail->setsubject("This is my example email");
$testmail->sethtml($htmlexample);
// This call is unnecessary, as if it is not made, the same thing
// will be send as plain text anyways.
$testmail->setplain("This is the example email.");
$testmail->addattachment("./mail.php");
$testmail->addattachment("Test.txt", "This is my example attachment");
$testmail->compilemail();
$testserver->openconnection();
$testserver->sendmail($testmail->headers, $testmail->message);
$testserver->closeconnection();?>
plz help me...i am not able to send mail through this code..is there any problem in php setting.
error_reporting(E_ALL); to the top of this script. Post any errors that may occur. Good luck!