Hi

I am using Xampp Server in my machine, i used php mailer (class.phpmailer.php), when i run the mail file, i am getting the below error msg

Could not execute: /var/qmail/bin/sendmail

Please help me to solve this issue.

Recommended Answers

All 15 Replies

what is the most recent error in phpmailer

echo $objMail->ErrorInfo;

Hi Friend

Thanks for your reply, here is below thing

I used the below code

<?php

if(mail('user@mydomain.com','test subject','test message')){

      echo('ok');
      echo $objMail->ErrorInfo;

    }

else{

      echo('not ok');

    }

?>

Warning: mail() [function.mail]: "sendmail_from" not set in php.ini or custom "From:" header missing in C:\xampp\htdocs\kattendance\kmail.php on line 3
not ok

Hi
Here is the code what i have used

<?php
$goto_after_mail = "thank.htm"; // this is the page which is shown after the mail is submitted
$from_mail = $_REQUEST['asasa@domain.com'];
$from_name = $_REQUEST['IT']; // use both value's from your form
$header = "From: \"$from_name\" <$from_mail>\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: text/plain; charset=\"utf-8\"\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n";
$subject = "Your webform posted at ".date("d-m-Y"); //your mailsubject incl. current date
foreach ($_REQUEST as $key => $val) {
    if ($key != "from_email" && $key != "from_name") { //skip, this values are already in the header
        $body .= $key . " : " . $val . "\r\n";
    }
}
if (mail("pppp@domain.com", $subject, $body, $header)) {
	header("Location: ".$goto_after_mail);
        exit;
}
?>

Warning: mail() [function.mail]: Failed to connect to mailserver at "192.168.1.80" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\xampp\htdocs\kattendance\kmail1.php on line 15

Any time I have used phpmailer in the past I have always used smtp. If you would rather use sendmail, I would go with the error that you are getting from phpmailer "Warning: mail() [function.mail]: "sendmail_from" not set in php.ini or custom "From:" header missing in C:\xampp\htdocs\kattendance\kmail.php on line 3" and do some research on that. It is a widely supported class and you should be able to get quite a bit of info on it. To use smtp you will need an smtp server and Try this example and see if it helps: http://phpcode.mypapit.net/sending-mail-using-smtp-with-phpmailer/26/

Hi Friend

Thanks for your response,

I have tried as per your suggestion. But i am getting the below error

Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\kattendance\plain\phpmailer\class.smtp.php on line 105

Warning: fsockopen() [function.fsockopen]: unable to connect to mail.gmail.com:25 (php_network_getaddresses: getaddrinfo failed: No such host is known. ) in C:\xampp\htdocs\kattendance\plain\phpmailer\class.smtp.php on line 105

There was an error in sending mail, please try again at a later time

Language string failed to load: connect_host

Post your smtp code.

Dear Friend

Thanks for your interest, here is the below code

example.php

<?php
include("config.php");
$today=date('Y-m-d');
$today1=date('d-m-Y');
require_once "dSendMail2.inc.php";
//$qmail = "select * from add_email";
//$qres=mysql_query($qmail,$conn);
//		while($qm=mysql_fetch_array($qres)){ 
// First of all: DISABLE TIME LIMIT and IGNORE USER ABORT! You really don't want to be interrupted in the middle of this.
//set_time_limit(0);
//ignore_user_abort(true);
// Basics - Setting the message...
$m = new dSendMail2;

$m->setSubject("Karismaafoundations site attendance on - $today1");
$m->setFrom("it@example.com");    // Visible "From:" header. Recipient will see this.
$m->setTo  ("ramesh@example.in");    // Visible and functional recipient(s).
//$m->setBcc ("palani@example.com");
//$m->setCc ("ramesh@example.com");
$m->setMessage(
	"Hi,<br><br>".
	"Attached is the Karismaafoundations site attendance on <b>$today1</b>.<br /><br />".
	"<b>Note:</b> This is automatic email system, please do not reply to this email. If any clarifications please contact IT Department.<br />"
	//"To embed an image, just call it here..<br />".
	//"<img src='my_image.jpg' /><br />".
	//"Image is shown above here.<br />"
);

// Important:
// - For embedded images to work, message MUST be set before the attachments.

// Attach the file.
$m->autoAttachFile("result.pdf", file_get_contents("result.pdf"));

// Attach embedded image.
//$m->autoAttachFile("my_image.jpg", file_get_contents("my_image.jpg"));

// Send message.
$m->send();

// Explanation: How does it work?
// - When you attach a file, class will see if you're using it in your HTML message.
// - If YES, it will embedded image. Otherwise, will only attach.
 
//$i++; 
//};

dSendMail2.inc.php

<?php
// Ps: Read "changelog.txt"

/**
	Como funciona o sistema de EML?
		Trabalha apenas com o body da mensagem.
		
		Exemplo:
		->setSubject(assunto)
		->setFrom(from)
		->setTo(to)
		->setBcc(to)
		->setEMLFile(eml_file) -- OU -- setHTMLFile(html_file[, auto_images=t/f]) -- OU -- 
		->setMessage(message)  -- OU -- importHTML(body[, images_dir])
		->send()
		
	Como enviar via SMTP ou MAIL():
		->sendThroughMail()
		->sendThroughSMTP($smtp_server, $port=25, $user=false, $pass=false, $ssl=[0=nao,1=sim,2=mixed])
	
	Como utilizar CALLBACK():
		->send($startInPart, $callBack)
		Callback($part=LoopStart|LoopEnd, $loopPart, $totalLoops)
			Retornos possíveis:
				"STOP" (Força a parada imediata da execução)
				
	Bloqueio de e-mails duplicados:
		->allowDupe()
		->blockDupe() (Default)
	
	Author: IMAGINACOM
	E-mail: contato AT IMAGINACOM.com
**/

class dSendMail2 extends htmlMimeMail{
	var $to        = '"noreply" <it@kgsmilestone.com>';
	var $error     = false;
	var $debug     = 0;
	var $timeout   = 15;
	
	var $delay        = 1;
	var $groupAmnt    = false;
	var $sendThrough  = false;
	var $blockDupe    = true;
	var $localhost    = false;
	
	var $logFolder = false;
	var $logFile   = false;
	
	static $StaticSendThrough = 'default';
	
	/** Static **/
	Function getVersion(){
		return "v2.37";
	}
	
	/** Public **/
	Function __construct(){
		$this->htmlMimeMail();
		
		if(self::$StaticSendThrough !== 'default')
			$this->sendThrough = self::$StaticSendThrough;
		
		/** Default values: **/
		$this->setGroupAmount(20);
		$this->setSMTPTimeout(15);
		$this->blockDupe();
		$this->setPriority(3);
		$this->setCrlf("\n");
		$this->setCharset('ISO-8859-1');
	}
	static Function easyMail      ($to, $subject, $message, $from=false, $html=false, $attach=false){
		$this->setTo($to);
		$this->setSubject($subject);
		$this->setMessage($message, $html);
		
		if($from)   $this->setFrom($from);
		if($attach) foreach($attach as $att){
			if(!isset($att[1]))
				die("Attach precisa ser: Array(Array(filename, filedata), Array(filename, filedata), ...)");
			
			$this->autoAttachFile($att[0], $att[1]);
		}
		
		return $this->send();
	}
	static Function createFromMail($to, $subject, $message, $headers=false){
		$m = new dSendMail2;
		$m->headers  = $headers?self::_convertStrHeaderToArray($headers):Array();
		
		if(!isset($m->headers['From']) || !$m->headers['From']){
			if(isset($_SERVER['SERVER_HOST']))
				$m->setFrom("nobody@{$_SERVER['SERVER_NAME']}");
			elseif(isset($_SERVER['HTTP_HOST']) && preg_match("/^[A-Za-z0-9\-\_\.\:]+$", $_SERVER['HTTP_HOST']))
				$m->setFrom("nobody@{$_SERVER['SERVER_HOST']}");
			elseif(isset($_SERVER['REMOTE_ADDR']))
				$m->setFrom("nobody@{$_SERVER['REMOTE_ADDR']}");
			elseif(isset($_SERVER['USERDOMAIN']))
				$m->setFrom("nobody@{$_SERVER['USERDOMAIN']}");
			else
				$m->setFrom('nobody@nowhere.com');
		}			
		
		$isHTML = (isset($m->headers['Content-Type']) && stripos($m->headers['Content-Type'], "text/html") !== false);
		
		$m->setTo($to);
		$m->setSubject($subject);
		$m->setMessage($message, $isHTML);
		return $m;
	}
	
	Function setHTMLFile   ($filename, $importImages=true){  // Can receive an HTML File, and auto-attach all images to mass send
		$this->error = false;
		if(!is_readable($filename)){
			$this->error = "Erro lendo o arquivo enviado.";
			return false;
		}
		
		if($importImages === true){
			$importImages = dirname($filename);
		}
		
		$this->importHTML(file_get_contents($filename), $importImages);
		return true;
	}
	Function setEMLFile    ($filename){                      // Can receive an EML File to mass send
		$this->error = false;
		if(!is_readable($filename)){
			$this->error = "Erro lendo o arquivo enviado.";
			return false;
		}
		
		$this->importEML(file_get_contents($filename));
		return true;
	}
	Function setMessage    ($body, $html=true, $nl2br=false){// Defines the message contents
		($html)?
			$this->html = ($nl2br?nl2br($body):$body):
			$this->text = ($body);
	}
	Function replaceMessage($from, $to){                     // Useful if you want to change something after calling importHTML
		$this->html = str_replace($from, $to, $this->html);
	}
	Function setPriority   ($priority){                      // Defines the message priority (1=High 3=Normal 5=Low, only for Outlook)
		if(isset($this->headers['X-Priority']) || in_array($priority, Array(1, 5))){
			$this->headers['X-Priority'] = $priority;
		}
	}
	Function setCharset    ($charset){                       // Defines the charset. ISO-8859-1 is the default.
		// Usually: ISO-8859-1 or UTF-8
		// Default: ISO-8859-1 (Latin1)
		$this->headers['Content-Type']       = $charset;
        $this->build_params['html_charset']  = $charset;
        $this->build_params['text_charset']  = $charset;
        $this->build_params['head_charset']  = $charset;
	}
	Function setSubject    ($subject){
		$subject = str_replace(Array("\r", "\n", "\r\n"), "_", $subject);
		return parent::setSubject($subject);
	}
	Function setTo         ($to){                            // Se the 'To' (Visible)
		if(!is_array($to)){
			$to = str_replace(";", ",", $to);
			$to = explode(",", $to);
		}
		$to = array_map(Array($this, '_normalizeEmailAddress'), $to);
		if($this->blockDupe)
			$to = array_unique($to);
		$to = implode(",", $to);
		
		$this->to = $to;
	}
	Function setCc         ($to){
		if(!is_array($to)){
			$to = str_replace(";", ",", $to);
			$to = explode(",", $to);
		}
		$to = array_map(Array($this, '_normalizeEmailAddress'), $to);
		if($this->blockDupe)
			$to = array_unique($to);
		$to = implode(",", $to);
		
		return parent::setCc($to);
	}
	Function setBcc        ($to){
		if(!is_array($to)){
			$to = str_replace(";", ",", $to);
			$to = explode(",", $to);
		}
		$to = array_map(Array($this, '_normalizeEmailAddress'), $to);
		if($this->blockDupe)
			$to = array_unique($to);
		$to = implode(",", $to);
		
		return parent::setBcc($to);
	}
	Function setFrom       ($from, $nome=false){
		if($nome)
			$from = "\"{$nome}\" <$from>";
		
		$from = str_replace(Array("\r", "\n", "\r\n"), "_", $from);
		return parent::setFrom($from);
	}
	Function setReplyTo    ($email, $nome=false){
		if($nome)
			$email = "\"{$nome}\" <$email>";
		
		$email = str_replace(Array("\r", "\n", "\r\n"), "_", $email);
		$this->headers['Reply-To'] = $email;
	}
	Function setReturnPath ($email, $nome=false){
		if($nome)
			$email = "\"{$nome}\" <$email>";
		
		$email = str_replace(Array("\r", "\n", "\r\n"), "_", $email);
		$this->headers['Return-Path'] = $email;
	}
	Function send          ($startInPart=1, $callBack=false){ // Send the message, loop if necessary, save to database if necessary
		$this->error = false;
		if(!$this->is_built)
            $this->buildMessage();
		
		$this->output          = str_replace("\r\n", "\n", $this->output);
		
		if(!isset($this->headers['From']))
			die("Cannot proceed: You must call 'setFrom(email, name)' before sending e-mails.\r\n");
		
		if(!isset($this->to))
			die("Cannot proceed: You must call 'setTo(email)' before sending e-mails.\r\n");
		
		// Normaliza campo "From" e "To:"
		$this->headers['From'] = $this->_normalizeEmail($this->headers['From']);
		$this->to              = $this->_normalizeEmail($this->to);
		
		/** Define variáveis importantes para a elaboração dos vários laços **/
		$hasCc  = !empty($this->headers['Cc']);
		$hasBcc = !empty($this->headers['Bcc']);
		
		if($hasBcc){
			$parts_bcc = explode(",", $this->headers['Bcc']);
			unset($this->headers['Bcc']);
		}
		$sizeTo = substr_count($this->to, ",")+1;
		$sizeCc = $hasCc ?(substr_count($this->headers['Cc'], ",")+1):0;
		$sizeBcc= $hasBcc?count($parts_bcc):0;
		
		$loopSize  = $this->groupAmnt;
		$loopSize -= ($sizeTo+$sizeCc); // Considera os 'destinos' fixos, que são enviados em todos os laços.
		
		$loopPart   = $startInPart?$startInPart:1;
		$totalLoops = ceil($sizeBcc/$loopSize);
		
		// Prepara variáveis para o padrão
		$subject = $this->headers['Subject'];
		unset($this->headers['Subject']);
		
		// Get flat representation of headers
		foreach ($this->headers as $name => $value)
			$headers[] = $name . ': ' . $this->_encodeHeader($value, $this->build_params['head_charset']);
		
		$to = $this->_encodeHeader($this->to, $this->build_params['head_charset']);
		
		$this->_log("Iniciando envio . . .");
		$this->_log("From:    {$this->headers['From']}");
		$this->_log("To:      {$this->to}");
		$this->_log("Subject: {$subject}");
		$this->_log("--------------------------");
		$this->_log("Headers:");
		$this->_log(print_r($this->headers, true));
		$this->_log("Output:");
		$this->_log(print_r($this->output, true));
		$this->_log("--------------------------");
		$this->_log("Delay:     {$this->delay}");
		$this->_log("sizeTo:    {$sizeTo}");
		$this->_log("sizeCc:    {$sizeCc}");
		$this->_log("sizeBcc:   {$sizeBcc}");
		$this->_log("groupAmnt: {$this->groupAmnt}");
		$this->_log("Tamanho do laço: {$loopSize}");
		$this->_log("Loop inicial:    {$startInPart}");
		$this->_log("Loop máximo:     {$totalLoops}");
		$this->_log("--------------------------");
		
		$orHeaders = $headers;
		/** Entra no loop para enviar mensagens **/
		do{
			$this->_log("Loop #{$loopPart}");
			if($callBack){
				$cbResult = call_user_func($callBack, 'LoopStart', $loopPart, $totalLoops);
				$this->_log("Resultado do callback LoopStart: {$cbResult}");
				if(substr($cbResult, 0, 4) == 'STOP')
					break;
			}
			
			$headers = $orHeaders;
			if($hasBcc){
				$headbcc   = join(",", array_slice($parts_bcc, ($loopPart-1)*$loopSize, $loopSize));
				$headers[] = "Bcc: $headbcc";
				$this->_log("+ Destinos ocultos: {$headbcc}");
			}
			
			if($this->delay && $loopPart > 1){
				$this->_log("+ Aguardando {$this->delay} para nao sobrecarregar servidor");
				sleep($this->delay);
			}
			$headersJoined = implode("\n", $headers);
			$result = $this->callMail($subject, $headersJoined);
			
			$this->_log("+ Resultado do envio: ".(($result===true)?"Sucesso!":"Falha no envio."));
			if(!$result){
				$this->_log("FALHA CRÍTICA: O loop #{$loopPart} (total de {$totalLoops}) naão foi entregue com sucesso.");
				$this->_log("FALHA CRÍTICA: Loop interrompido.");
				$this->error = "Falha no envio do laco $loopPart de $totalLoops. {$this->error}\n";
				break;
			}
			unset($headers, $headersJoined, $result, $headbcc);
			
			if($callBack){
				$cbResult = call_user_func($callBack, 'LoopEnd', $loopPart, $totalLoops);
				$this->_log("Resultado do callback LoopEnd: {$cbResult}");
				if($cbResult == 'STOP')
					break;
			}
		} while($loopPart++ < $totalLoops);
		$this->_log("--------------------------");
		$this->_log(". . . Concluído!");
		
		// Reset the subject in case mail is resent
		if ($subject !== ''){
			$this->headers['Subject'] = $subject;
		}
		
		return $this->error?false:true;
	}
	
	Function autoAttachFile($filename, &$filedata){           // Auto-detect if need to attach or embed the attachment. This need to be called after setMessage()
		// $filename = basename($filename);
		if($this->html && preg_match('/(?:"|\')'.preg_quote($filename, '/').'(?:"|\')/Ui', $this->html)){
			$this->embedFile($filename, $filedata);
			$this->_log("Adicionando HTMLImage: $filename (".strlen($filedata)." bytes)");
			return 1;
		}
		else{
			$this->attachFile(basename($filename), $filedata);
			$this->_log("Adicionando Attachment: $filename (".strlen($filedata)." bytes)");
			return 2;
		}
	}
	Function attachFile($filename, &$filedata){
		return $this->addAttachment($filedata, basename($filename), $this->_getAutoMimeType($filename));
	}
	Function embedFile ($filename, &$filedata){
		$cid = $this->addHtmlImage($filedata, $filename, $this->_getAutoMimeType($filename));
		return $cid?"CID:{$cid}":false;
	}
	
	Function allowDupe($yesno=true){  // Default: False
		$this->blockDupe = !$yesno;
	}
	Function blockDupe($yesno=true){  // Default: True
		$this->allowDupe(!$yesno);
	}
	Function setGroupAmount($amount){ // Default: 20
		$this->groupAmnt = $amount;
	}
	
	Function importHTML($body, $baseDir=false, $importImages=true){// Auto-detect all images inside an HTML body and embed it as attachments.
		if(!$baseDir)
			$importImages = false;
		
		$this->setMessage($body, true, false); // body, html, force_nl2br
		if($importImages){
			if($importImages && strpos($baseDir, '/') === null)
				die("dSendMail2 - importHTML() - Parâmetro '\$baseDir' deve ter um caminho absoluto, não relativo. Atualmente, '{$baseDir}'.");
			
			$tags    = preg_match_all("/<.+?(src|background)=[\"']?(.+?)[\"' ].*?>/is", $body, $out);
			if($tags) foreach($out[2] as $addFile){
				if(strpos($addFile, "://"))
					continue;
				
				$dieCritical = false;
				if(file_exists("$baseDir/$addFile")){
					$this->autoAttachFile($addFile, file_get_contents("$baseDir/$addFile"));
				}
				else{
					echo "HTML Pediu o arquivo: '{$addFile}', mas este não existe em '{$baseDir}/{$addFile}'<br />\r\n";
					$dieCritical = true;
				}
				if($dieCritical)
					die("Arquivos encontrados no HTML mas não foram encontrados na pasta. Cancelando envio.\r\n");
			}
		}
		return true;
	}
	Function importEML ($fileBody, $importAll=false){
		/** Primeiro passo: Separar header/body e importar o header**/
		preg_match("/^(.+?)\r?\n\r?\n(.+)$/s", $fileBody, $emlContentsParts);
		
		/** Segundo passo: Definir construção da mensagem **/
		$this->setMessage("Esta mensagem foi enviada diretamente em formato EML.\nPara ver seu conteúdo, será necessário fazer o download.");
		$this->headers  = $this->_convertStrHeaderToArray($emlContentsParts[1]);
		$this->output   = isset($emlContentsParts[2])?$emlContentsParts[2]:'';
		$this->is_built = true;
		
		if(!$importAll){
			// Remove headers that will be ignored or overwritted
			unset($this->headers["X-Unsent"]);
			unset($this->headers["From"]);
			unset($this->headers["To"]);
			unset($this->headers["Cc"]);
			unset($this->headers["Bcc"]);
		}
		else{
			// Import all headers, and parse some of them
			$this->setTo($this->headers['To']);
			unset($this->headers["To"]);
		}
	}
	Function exportEML($setUnsent=false){
		if(!$this->is_built)
			$this->buildMessage();
		
		$ret = "";
		foreach($this->headers as $key=>$value)
			$ret .= "{$key}: ".trim($value)."\n";
		
		if($setUnsent)
			$ret .= "X-Unsent: 1\n";
		
		$ret .= "\n";
		$ret .= $this->output;
		return $ret;
	}
	
	Function setSMTPTimeout($timeout){  // Default: 15 (seconds)
		$this->timeout = $timeout;
	}
	Function sendThroughSMTP($smtp_server, $port=25, $user=false, $pass=false, $ssl=false){
		$this->sendThrough = Array($smtp_server, $port, $user, $pass, $ssl);
	}
	Function sendThroughMail(){
		$this->sendThrough = false;
	}
	Function sendThroughGMail  ($user, $pass){
		$this->sendThroughSMTP('smtp.gmail.com', 465, $user, $pass, 1);
	}
	Function sendThroughHotMail($user, $pass){
		$this->sendThroughSMTP('smtp.live.com', 25, $user, $pass, 2);
	}
	Function sendThroughYahoo  ($user, $pass){
		$this->sendThroughSMTP('smtp.mail.yahoo.com', 465, $user, $pass, 1);
	}
	Function sendThroughExchangeserver($user,$pass){
		$this->sendThroughSMTP('red003.mail.apac.microsoftonline.com', 587, $user, $pass, 2);
	}
	Function sendThroughNowhere($randomError=0){
		// Used ONLY for test purpose.
		// $randomError between 0 and 1
		$this->sendThrough = Array('NULL', $randomError);
	}
	Function sendThroughSetDefault(){
		self::$StaticSendThrough = $this->sendThrough;
	}
	
	Function getError(){
		return $this->error;
	}
	
	/** Private **/
	Function callMail($subject, $headersJoined){
		if(!$this->sendThrough){
			$this->_log("Enviando através da função MAIL()");
			return mail($this->to, $subject, $this->output, $headersJoined);
		}
		elseif($this->sendThrough[0] == 'NULL'){
			$errProb = $this->sendThrough[1];
			$this->_log("Modo de testes, não enviando para ninguém. Possibilidade de erro: {$errProb}.");
			if($errProb && rand(0, 100) < (($errProb>1)?$errProb:$errProb*100)){
				$this->error = "Simulando erro de testes.";
				return false;
			}
			return true;
		}
		else{
			$this->_log("Enviando através de SMTP");
			
			$user = $this->sendThrough[2];
			$pass = $this->sendThrough[3];
			if($user){
				$user  = explode("@", $this->sendThrough[2], 2);
				$realm = isset($user[1])?$user[1]:false;
				$user  = $user[0];
			}
			
			$smtp = new smtp_class;
			$smtp->host_name = $this->sendThrough[0];
			$smtp->host_port = $this->sendThrough[1];
			$smtp->ssl       = ($this->sendThrough[4]===1||($this->sendThrough[4]===true));
			$smtp->start_tls = ($this->sendThrough[4]===2);
			$smtp->localhost = ($this->localhost?$this->localhost:(isset($_SERVER['SERVER_NAME'])?$_SERVER['SERVER_NAME']:"localhost"));
			$smtp->timeout      = $this->timeout;
			$smtp->data_timeout = 0;
			$smtp->debug        = $this->debug;
			$smtp->html_debug   = !true;
			$smtp->pop3_auth_host = "";
			$smtp->user     = $user?$user :false;
			$smtp->realm    = $user?$realm:false;
			$smtp->password = $user?$pass :false;
			
			$arheader = $this->_convertStrHeaderToArray($headersJoined);
			if(!isset($arheader['From']))   $arheader['From']    = $this->headers['From'];
			if(!isset($arheader['To']))     $arheader['To']      = $this->to;
			if(!isset($arheader['Subject']))$arheader['Subject'] = $subject;
			if(!isset($arheader['Date']))   $arheader['Date']    = date('r');
			
			$bccTo = false;
			if(isset($arheader['Bcc'])){
				$bccTo = $arheader['Bcc'];
				unset($arheader['Bcc']);;
			}
			
			$newheader = Array();
			foreach($arheader as $key=>$headerline){
				$newheader[] = "{$key}: {$headerline}";
			}
			$ok = $smtp->SendMessage($this->_normalizeEmail($this->headers['From'], true), explode(",", $this->to.($bccTo?",{$bccTo}":"")), $newheader, $this->output);
			if(!$ok){
				$this->error = $smtp->error;
				return false;
			}
			return true;
		}
		
		die("Don't know how to send.");
	}
	Function _log($event){
		if($this->logFolder){
			if(!is_writable($this->logFolder))
				die("Impossível enviar e-mails - Pasta de log sem permissões. ($this->logFolder)");
			
			if(!$this->logFile)
				$this->logFile = fopen("{$this->logFolder}/log-".date('d.m.Y-H.i.s').'-'.substr(uniqid(), -4).".txt", "a+");
			
			$toLog  = date('d/m/Y H:i:s')." ".trim($event)."\r\n";
			fwrite($this->logFile, $toLog);
		}
	}
	Function _getAutoMimeType($filename){
		$ext = strtolower(substr($filename, -3));
		switch($ext){
			case ".xls":
				$content_type="application/excel";
				break;
			case ".hqx":
				$content_type="application/macbinhex40";
				break;
			case ".doc":
			case ".dot":
			case ".wrd":
				$content_type="application/msword";
				break;
			case ".pdf":
				$content_type="application/pdf";
				break;
			case ".pgp":
				$content_type="application/pgp";
				break;
			case ".ps":
			case ".eps":
			case ".ai":
				$content_type="application/postscript";
				break;
			case ".ppt":
				$content_type="application/powerpoint";
				break;
			case ".rtf":
				$content_type="application/rtf";
				break;
			case ".tgz":
			case ".gtar":
				$content_type="application/x-gtar";
				break;
			case ".gz":
				$content_type="application/x-gzip";
				break;
			case ".php":
			case ".php3":
				$content_type="application/x-httpd-php";
				break;
			case ".js":
				$content_type="application/x-javascript";
				break;
			case ".ppd":
			case ".psd":
				$content_type="application/x-photoshop";
				break;
			case ".swf":
			case ".swc":
			case ".rf":
				$content_type="application/x-shockwave-flash";
				break;
			case ".tar":
				$content_type="application/x-tar";
				break;
			case ".zip":
				$content_type="application/zip";
				break;
			case ".mid":
			case ".midi":
			case ".kar":
				$content_type="audio/midi";
				break;
			case ".mp2":
			case ".mp3":
			case ".mpga":
				$content_type="audio/mpeg";
				break;
			case ".ra":
				$content_type="audio/x-realaudio";
				break;
			case ".wav":
				$content_type="audio/wav";
				break;
			case ".bmp":
				$content_type="image/bitmap";
				break;
			case ".gif":
				$content_type="image/gif";
				break;
			case ".iff":
				$content_type="image/iff";
				break;
			case ".jb2":
				$content_type="image/jb2";
				break;
			case ".jpg":
			case ".jpe":
			case ".jpeg":
				$content_type="image/jpeg";
				break;
			case ".jpx":
				$content_type="image/jpx";
				break;
			case ".png":
				$content_type="image/png";
				break;
			case ".tif":
			case ".tiff":
				$content_type="image/tiff";
				break;
			case ".wbmp":
				$content_type="image/vnd.wap.wbmp";
				break;
			case ".xbm":
				$content_type="image/xbm";
				break;
			case ".css":
				$content_type="text/css";
				break;
			case ".txt":
				$content_type="text/plain";
				break;
			case ".htm":
			case ".html":
				$content_type="text/html";
				break;
			case ".xml":
				$content_type="text/xml";
				break;
			case ".mpg":
			case ".mpe":
			case ".mpeg":
				$content_type="video/mpeg";
				break;
			case ".qt":
			case ".mov":
				$content_type="video/quicktime";
				break;
			case ".avi":
				$content_type="video/x-ms-video";
				break;
			case ".eml":
				$content_type="message/rfc822";
				break;
			default:
				$content_type="application/octet-stream";
				break;
		}
		return $content_type;
	}
	Function &_convertStrHeaderToArray($header){
		$headers   = Array();
		$strHeader = explode("\n", $header);
		foreach($strHeader as $lineHeader){
			$parts = explode(": ", $lineHeader, 2);
			if(sizeof($parts) == 2){
				// If correct format (Title: value)
				$lastHeaderTitle = implode("-", array_map('ucfirst', explode("-", $parts[0])));
				if(preg_match("/^(from|to|cc|bcc|subject)$/i", $lastHeaderTitle))     // Check if the essential headers are correctly written
					$lastHeaderTitle = ucfirst(strtolower($lastHeaderTitle)); // The whole string is lower-case, but the first letter
				$headers[$lastHeaderTitle] = rtrim($parts[1]);
			}
			else // It's a continuation of the previous Title
				if(!isset($lastHeaderTitle)) // But there's no previous title! Weird...
					$headers[rtrim($parts[0])] = "";
				else
					$headers[$lastHeaderTitle] .= "\n".rtrim($parts[0]);
		}
		return $headers;
	}
	Function _normalizeEmail($email, $only_address=false){
		if(!strpos($email, "<")){
			// Padrão: a@b.c
			return trim(str_replace(Array(" ", "<", ">"), "", strtolower($email)));
		}
		
		// Padrão: "Nome" <a@b.c>
		$nome  = trim(str_replace("\"", "", substr($email, 0, strpos($email, "<"))));
		$email = substr($email, strpos($email, "<"));
		$email = trim(str_replace(Array(" ", "<", ">"), "", $email));
		$email = strtolower($email);
		
		return $only_address?
			$email:
			"\"{$nome}\" <$email>";
	}
	Function _normalizeEmailAddress($email){
		return $this->_normalizeEmail($email, true);
	}
}

// MimeMessage Support
class htmlMimeMail  {
    var $html;
    var $text;
    var $output;
    var $html_text;
    var $html_images;
    var $image_types;
    var $build_params;
    var $attachments;
    var $headers;
    var $is_built;
    var $return_path;
    var $smtp_params;
    
	function htmlMimeMail(){
        $this->html_images = array();
        $this->headers     = array();
        $this->is_built    = false;

        $this->image_types = array(
                                    'gif'	=> 'image/gif',
                                    'jpg'	=> 'image/jpeg',
                                    'jpeg'	=> 'image/jpeg',
                                    'jpe'	=> 'image/jpeg',
                                    'bmp'	=> 'image/bmp',
                                    'png'	=> 'image/png',
                                    'tif'	=> 'image/tiff',
                                    'tiff'	=> 'image/tiff',
                                    'swf'	=> 'application/x-shockwave-flash'
                                  );

        /**
        * Set these up
        */
        $this->build_params['html_encoding'] = '7bit';
        $this->build_params['text_encoding'] = '7bit';
        $this->build_params['html_charset']  = 'ISO-8859-1';
        $this->build_params['text_charset']  = 'ISO-8859-1';
        $this->build_params['head_charset']  = 'ISO-8859-1';
        $this->build_params['text_wrap']     = 998;

        /**
        * Defaults for smtp sending
        */
        if (!empty($GLOBALS['HTTP_SERVER_VARS']['HTTP_HOST'])) {
            $helo = $GLOBALS['HTTP_SERVER_VARS']['HTTP_HOST'];
        } elseif (!empty($GLOBALS['HTTP_SERVER_VARS']['SERVER_NAME'])) {
            $helo = $GLOBALS['HTTP_SERVER_VARS']['SERVER_NAME'];
        } else {
            $helo = 'localhost';
        }

        $this->smtp_params['host'] = 'localhost';
        $this->smtp_params['port'] = 25;
        $this->smtp_params['helo'] = $helo;
        $this->smtp_params['auth'] = false;
        $this->smtp_params['user'] = '';
        $this->smtp_params['pass'] = '';

        /**
        * Make sure the MIME version header is first.
        */
        $this->headers['MIME-Version'] = '1.0';
    }

    /**
    * This function will read a file in
    * from a supplied filename and return
    * it. This can then be given as the first
    * argument of the the functions
    * add_html_image() or add_attachment().
    */
    function getFile($filename){
        $return = '';
        if ($fp = fopen($filename, 'rb')) {
            while (!feof($fp)) {
                $return .= fread($fp, 1024);
            }
            fclose($fp);
            return $return;

        } else {
            return false;
        }
    }

    /**
    * Accessor to set the CRLF style
    */
    function setCrlf($crlf = "\n"){
        if (!defined('CRLF')) {
            define('CRLF', $crlf, true);
        }

        if (!defined('MAIL_MIMEPART_CRLF')) {
            define('MAIL_MIMEPART_CRLF', $crlf, true);
        }
    }

    /**
    * Accessor to set the SMTP parameters
    */
    function setSMTPParams($host = null, $port = null, $helo = null, $auth = null, $user = null, $pass = null){
        if (!is_null($host)) $this->smtp_params['host'] = $host;
        if (!is_null($port)) $this->smtp_params['port'] = $port;
        if (!is_null($helo)) $this->smtp_params['helo'] = $helo;
        if (!is_null($auth)) $this->smtp_params['auth'] = $auth;
        if (!is_null($user)) $this->smtp_params['user'] = $user;
        if (!is_null($pass)) $this->smtp_params['pass'] = $pass;
    }

    /**
    * Accessor function to set the text encoding
    */
    function setTextEncoding($encoding = '7bit'){
        $this->build_params['text_encoding'] = $encoding;
    }

    /**
    * Accessor function to set the HTML encoding
    */
    function setHtmlEncoding($encoding = 'quoted-printable'){
        $this->build_params['html_encoding'] = $encoding;
    }

    /**
    * Accessor function to set the text charset
    */
    function setTextCharset($charset = 'ISO-8859-1')
    {
        $this->build_params['text_charset'] = $charset;
    }

    /**
    * Accessor function to set the HTML charset
    */
    function setHtmlCharset($charset = 'ISO-8859-1')
    {
        $this->build_params['html_charset'] = $charset;
    }

    /**
    * Accessor function to set the header encoding charset
    */
    function setHeadCharset($charset = 'ISO-8859-1')
    {
        $this->build_params['head_charset'] = $charset;
    }

    /**
    * Accessor function to set the text wrap count
    */
    function setTextWrap($count = 998)
    {
        $this->build_params['text_wrap'] = $count;
    }

    /**
    * Accessor to set a header
    */
    function setHeader($name, $value)
    {
        $this->headers[$name] = $value;
    }

    /**
    * Accessor to add a Subject: header
    */
    function setSubject($subject)
    {
        $this->headers['Subject'] = $subject;
    }

    /**
    * Accessor to add a From: header
    */
    function setFrom($from)
    {
        $this->headers['From'] = $from;
    }

    /**
    * Accessor to set the return path
    */
    function setReturnPath($return_path)
    {
        $this->return_path = $return_path;
    }

    /**
    * Accessor to add a Cc: header
    */
    function setCc($cc)
    {
        $this->headers['Cc'] = $cc;
    }

    /**
    * Accessor to add a Bcc: header
    */
    function setBcc($bcc)
    {
        $this->headers['Bcc'] = $bcc;
    }

    /**
    * Adds plain text. Use this function
    * when NOT sending html email
    */
    function setText($text = '')
    {
        $this->text = $text;
    }

    /**
    * Adds a html part to the mail.
    * Also replaces image names with
    * content-id's.
    */
    function setHtml($html, $text = null, $images_dir = null)
    {
        $this->html      = $html;
        $this->html_text = $text;
        if (isset($images_dir)) {
            $this->_findHtmlImages($images_dir);
        }
    }

    /**
    * Function for extracting images from
    * html source. This function will look
    * through the html code supplied by add_html()
    * and find any file that ends in one of the
    * extensions defined in $obj->image_types.
    * If the file exists it will read it in and
    * embed it, (not an attachment).
    *
    * @author Dan Allen
    */
    function _findHtmlImages($images_dir)
    {
        // Build the list of image extensions
        while (list($key,) = each($this->image_types)) {
            $extensions[] = $key;
        }

        preg_match_all('/(?:"|\')([^"\']+\.('.implode('|', $extensions).'))(?:"|\')/Ui', $this->html, $images);

        for ($i=0; $i<count($images[1]); $i++) {
            if (file_exists($images_dir . $images[1][$i])) {
                $html_images[] = $images[1][$i];
                $this->html = str_replace($images[1][$i], basename($images[1][$i]), $this->html);
            }
        }

        if (!empty($html_images)) {

            // If duplicate images are embedded, they may show up as attachments, so remove them.
            $html_images = array_unique($html_images);
            sort($html_images);
    
            for ($i=0; $i<count($html_images); $i++) {
                if ($image = $this->getFile($images_dir.$html_images[$i])) {
                    $ext = substr($html_images[$i], strrpos($html_images[$i], '.') + 1);
                    $content_type = $this->image_types[strtolower($ext)];
                    $this->addHtmlImage($image, basename($html_images[$i]), $content_type);
                }
            }
        }
    }

    /**
    * Adds an image to the list of embedded
    * images.
    */
    function addHtmlImage($file, $name = '', $c_type='application/octet-stream')
    {
		$cid = md5(uniqid());
        $this->html_images[] = array(
                                        'body'   => $file,
                                        'name'   => $name,
                                        'c_type' => $c_type,
                                        'cid'    => $cid
                                    );
		return $cid;
    }


    /**
    * Adds a file to the list of attachments.
    */
    function addAttachment($file, $name = '', $c_type='application/octet-stream', $encoding = 'base64')
    {
        $this->attachments[] = array(
                                    'body'		=> $file,
                                    'name'		=> $name,
                                    'c_type'	=> $c_type,
                                    'encoding'	=> $encoding
                                  );
    }

    /**
    * Adds a text subpart to a mime_part object
    */
    function &_addTextPart(&$obj, $text)
    {
        $params['content_type'] = 'text/plain';
        $params['encoding']     = $this->build_params['text_encoding'];
        $params['charset']      = $this->build_params['text_charset'];
        if (is_object($obj)) {
            $return = $obj->addSubpart($text, $params);
        } else {
            $return = new Mail_mimePart($text, $params);
        }
        
        return $return;
    }

    /**
    * Adds a html subpart to a mime_part object
    */
    function &_addHtmlPart(&$obj)
    {
        $params['content_type'] = 'text/html';
        $params['encoding']     = $this->build_params['html_encoding'];
        $params['charset']      = $this->build_params['html_charset'];
        if (is_object($obj)) {
            $return = $obj->addSubpart($this->html, $params);
        } else {
            $return = new Mail_mimePart($this->html, $params);
        }
        
        return $return;
    }

    /**
    * Starts a message with a mixed part
    */
    function &_addMixedPart()
    {
        $params['content_type'] = 'multipart/mixed';
        $return = new Mail_mimePart('', $params);
        
        return $return;
    }

    /**
    * Adds an alternative part to a mime_part object
    */
    function &_addAlternativePart(&$obj)
    {
        $params['content_type'] = 'multipart/alternative';
        if (is_object($obj)) {
            $return = $obj->addSubpart('', $params);
        } else {
            $return = new Mail_mimePart('', $params);
        }
        
        return $return;
    }

    /**
    * Adds a html subpart to a mime_part object
    */
    function &_addRelatedPart(&$obj)
    {
        $params['content_type'] = 'multipart/related';
        if (is_object($obj)) {
            $return = $obj->addSubpart('', $params);
        } else {
            $return = new Mail_mimePart('', $params);
        }
        
        return $return;
    }

    /**
    * Adds an html image subpart to a mime_part object
    */
    function _addHtmlImagePart(&$obj, $value)
    {
        $params['content_type'] = $value['c_type'];
        $params['encoding']     = 'base64';
        $params['disposition']  = 'inline';
        $params['dfilename']    = $value['name'];
        $params['cid']          = $value['cid'];
        $obj->addSubpart($value['body'], $params);
    }

    /**
    * Adds an attachment subpart to a mime_part object
    */
    function _addAttachmentPart(&$obj, $value)
    {
        $params['content_type'] = $value['c_type'];
        $params['encoding']     = $value['encoding'];
        $params['disposition']  = 'attachment';
        $params['dfilename']    = $value['name'];
        $obj->addSubpart($value['body'], $params);
    }

    /**
    * Builds the multipart message from the
    * list ($this->_parts). $params is an
    * array of parameters that shape the building
    * of the message. Currently supported are:
    *
    * $params['html_encoding'] - The type of encoding to use on html. Valid options are
    *                            "7bit", "quoted-printable" or "base64" (all without quotes).
    *                            7bit is EXPRESSLY NOT RECOMMENDED. Default is quoted-printable
    * $params['text_encoding'] - The type of encoding to use on plain text Valid options are
    *                            "7bit", "quoted-printable" or "base64" (all without quotes).
    *                            Default is 7bit
    * $params['text_wrap']     - The character count at which to wrap 7bit encoded data.
    *                            Default this is 998.
    * $params['html_charset']  - The character set to use for a html section.
    *                            Default is ISO-8859-1
    * $params['text_charset']  - The character set to use for a text section.
    *                          - Default is ISO-8859-1
    * $params['head_charset']  - The character set to use for header encoding should it be needed.
    *                          - Default is ISO-8859-1
    */
    function buildMessage($params = array())
    {
        if (!empty($params)) {
            while (list($key, $value) = each($params)) {
                $this->build_params[$key] = $value;
            }
        }

        if (!empty($this->html_images)) {
            foreach ($this->html_images as $value) {
                $this->html = str_replace($value['name'], 'cid:'.$value['cid'], $this->html);
            }
        }

        $null        = null;
        $attachments = !empty($this->attachments) ? true : false;
        $html_images = !empty($this->html_images) ? true : false;
        $html        = !empty($this->html)        ? true : false;
        $text        = isset($this->text)         ? true : false;

        switch (true) {
            case $text AND !$attachments:
                $message = &$this->_addTextPart($null, $this->text);
                break;

            case !$text AND $attachments AND !$html:
                $message = &$this->_addMixedPart();

                for ($i=0; $i<count($this->attachments); $i++) {
                    $this->_addAttachmentPart($message, $this->attachments[$i]);
                }
                break;

            case $text AND $attachments:
                $message = &$this->_addMixedPart();
                $this->_addTextPart($message, $this->text);

                for ($i=0; $i<count($this->attachments); $i++) {
                    $this->_addAttachmentPart($message, $this->attachments[$i]);
                }
                break;

            case $html AND !$attachments AND !$html_images:
                if (!is_null($this->html_text)) {
                    $message = &$this->_addAlternativePart($null);
                    $this->_addTextPart($message, $this->html_text);
                    $this->_addHtmlPart($message);
                } else {
                    $message = &$this->_addHtmlPart($null);
                }
                break;

            case $html AND !$attachments AND $html_images:
                if (!is_null($this->html_text)) {
                    $message = &$this->_addAlternativePart($null);
                    $this->_addTextPart($message, $this->html_text);
                    $related = &$this->_addRelatedPart($message);
                } else {
                    $message = &$this->_addRelatedPart($null);
                    $related = &$message;
                }
                $this->_addHtmlPart($related);
                for ($i=0; $i<count($this->html_images); $i++) {
                    $this->_addHtmlImagePart($related, $this->html_images[$i]);
                }
                break;

            case $html AND $attachments AND !$html_images:
                $message = &$this->_addMixedPart();
                if (!is_null($this->html_text)) {
                    $alt = &$this->_addAlternativePart($message);
                    $this->_addTextPart($alt, $this->html_text);
                    $this->_addHtmlPart($alt);
                } else {
                    $this->_addHtmlPart($message);
                }
                for ($i=0; $i<count($this->attachments); $i++) {
                    $this->_addAttachmentPart($message, $this->attachments[$i]);
                }
                break;

            case $html AND $attachments AND $html_images:
                $message = &$this->_addMixedPart();
                if (!is_null($this->html_text)) {
                    $alt = &$this->_addAlternativePart($message);
                    $this->_addTextPart($alt, $this->html_text);
                    $rel = &$this->_addRelatedPart($alt);
                } else {
                    $rel = &$this->_addRelatedPart($message);
                }
                $this->_addHtmlPart($rel);
                for ($i=0; $i<count($this->html_images); $i++) {
                    $this->_addHtmlImagePart($rel, $this->html_images[$i]);
                }
                for ($i=0; $i<count($this->attachments); $i++) {
                    $this->_addAttachmentPart($message, $this->attachments[$i]);
                }
                break;
			
        }
		
        if (isset($message)) {
            $output = $message->encode();
            $this->output   = $output['body'];
            $this->headers  = array_merge($this->headers, $output['headers']);

            // Add message ID header
            srand((double)microtime()*10000000);
            $message_id = sprintf('<%s.%s@%s>', base_convert(time(), 10, 36), base_convert(rand(), 10, 36), isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'LOCALHOST');
            $this->headers['Message-ID'] = $message_id;

            $this->is_built = true;
            return true;
        } else {
            return false;
        }
    }

    /**
    * Function to encode a header if necessary
    * according to RFC2047
    */
    function _encodeHeader($input, $charset = 'ISO-8859-1')
    {
        preg_match_all('/(\s?\w*[\x80-\xFF]+\w*\s?)/', $input, $matches);
        foreach ($matches[1] as $value) {
            $replacement = preg_replace('/([\x20\x80-\xFF])/e', '"=" . strtoupper(dechex(ord("\1")))', $value);
            $input = str_replace($value, '=?' . $charset . '?Q?' . $replacement . '?=', $input);
        }
        
        return $input;
    }

    /**
    * Sends the mail.
    *
    * @param  array  $recipients
    * @param  string $type OPTIONAL
    * @return mixed
    */
    function send($recipients, $type = 'mail')
    {
        if (!defined('CRLF')) {
            $this->setCrlf($type == 'mail' ? "\n" : "\r\n");
        }

        if (!$this->is_built) {
            $this->buildMessage();
        }

        switch ($type) {
            case 'mail':
                $subject = '';
                if (!empty($this->headers['Subject'])) {
                    $subject = $this->_encodeHeader($this->headers['Subject'], $this->build_params['head_charset']);
                    unset($this->headers['Subject']);
                }

                // Get flat representation of headers
                foreach ($this->headers as $name => $value) {
                    $headers[] = $name . ': ' . $this->_encodeHeader($value, $this->build_params['head_charset']);
                }

                $to = $this->_encodeHeader(implode(', ', $recipients), $this->build_params['head_charset']);

                if (!empty($this->return_path)) {
                    $result = mail($to, $subject, $this->output, implode(CRLF, $headers), '-f' . $this->return_path);
                } else {
                    $result = mail($to, $subject, $this->output, implode(CRLF, $headers));
                }
                
                // Reset the subject in case mail is resent
                if ($subject !== '') {
                    $this->headers['Subject'] = $subject;
                }
                
                // Return
                return $result;
                break;

            case 'smtp':
				die("Classe SMTP não está implementada.");
                break;
        }
    }

    /**
    * Use this method to return the email
    * in message/rfc822 format. Useful for
    * adding an email to another email as
    * an attachment. there's a commented
    * out example in example.php.
    */
    function getRFC822($recipients, $type='mail')
    {
        // Make up the date header as according to RFC822
        $this->setHeader('Date', date('D, d M y H:i:s O'));

        if (!defined('CRLF')) {
            $this->setCrlf("\n");
        }

        if (!$this->is_built) {
            $this->buildMessage();
        }

        // Return path ?
        if (isset($this->return_path)) {
            $headers[] = 'Return-Path: ' . $this->return_path;
        }

        // Get flat representation of headers
        foreach ($this->headers as $name => $value) {
            $headers[] = $name . ': ' . $value;
        }
        $headers[] = 'To: ' . implode(', ', $recipients);

        return implode(CRLF, $headers) . CRLF . CRLF . $this->output;
    }
} // End of class.
class Mail_mimePart {
    var $_encoding; // The encoding type of this part
    var $_subparts; // An array of subparts
    var $_encoded;  // The output of this part after being built
    var $_headers;  // Headers for this part
    var $_body;     // The body of this part (not encoded)

    /**
     * Constructor.
     *
     * Sets up the object.
     *
     * @param $body   - The body of the mime part if any.
     * @param $params - An associative array of parameters:
     *                  content_type - The content type for this part eg multipart/mixed
     *                  encoding     - The encoding to use, 7bit, 8bit, base64, or quoted-printable
     *                  cid          - Content ID to apply
     *                  disposition  - Content disposition, inline or attachment
     *                  dfilename    - Optional filename parameter for content disposition
     *                  description  - Content description
     *                  charset      - Character set to use
     * @access public
     */
    function Mail_mimePart($body = '', $params = array()){
        if (!defined('MAIL_MIMEPART_CRLF')) {
            define('MAIL_MIMEPART_CRLF', defined('MAIL_MIME_CRLF') ? MAIL_MIME_CRLF : "\r\n", TRUE);
        }

        foreach ($params as $key => $value) {
            switch ($key) {
                case 'content_type':
                    $headers['Content-Type'] = $value . (isset($charset) ? '; charset="' . $charset . '"' : '');
                    break;

                case 'encoding':
                    $this->_encoding = $value;
                    $headers['Content-Transfer-Encoding'] = $value;
                    break;

                case 'cid':
                    $headers['Content-ID'] = '<' . $value . '>';
                    break;

                case 'disposition':
                    $headers['Content-Disposition'] = $value . (isset($dfilename) ? '; filename="' . $dfilename . '"' : '');
                    break;

                case 'dfilename':
                    if (isset($headers['Content-Disposition'])) {
                        $headers['Content-Disposition'] .= '; filename="' . $value . '"';
                    } else {
                        $dfilename = $value;
                    }
                    break;

                case 'description':
                    $headers['Content-Description'] = $value;
                    break;

                case 'charset':
                    if (isset($headers['Content-Type'])) {
                        $headers['Content-Type'] .= '; charset="' . $value . '"';
                    } else {
                        $charset = $value;
                    }
                    break;
            }
        }

        // Default content-type
        if (!isset($headers['Content-Type'])) {
            $headers['Content-Type'] = 'text/plain';
        }

        //Default encoding
        if (!isset($this->_encoding)) {
            $this->_encoding = '7bit';
        }

        // Assign stuff to member variables
        $this->_encoded  = array();
        $this->_headers  = $headers;
        $this->_body     = $body;
    }

    /**
     * encode()
     *
     * Encodes and returns the email. Also stores
     * it in the encoded member variable
     *
     * @return An associative array containing two elements,
     *         body and headers. The headers element is itself
     *         an indexed array.
     * @access public
     */
    function encode(){
        $encoded =& $this->_encoded;

        if (!empty($this->_subparts)) {
            srand((double)microtime()*1000000);
            $boundary = '=_' . md5(uniqid(rand()) . microtime());
            $this->_headers['Content-Type'] .= ';' . MAIL_MIMEPART_CRLF . "\t" . 'boundary="' . $boundary . '"';
			
            // Add body parts to $subparts
            for ($i = 0; $i < count($this->_subparts); $i++) {
                $headers = array();
                $tmp = $this->_subparts[$i]->encode();
                foreach ($tmp['headers'] as $key => $value) {
                    $headers[] = $key . ': ' . $value;
                }
                $subparts[] = implode(MAIL_MIMEPART_CRLF, $headers) . MAIL_MIMEPART_CRLF . MAIL_MIMEPART_CRLF . $tmp['body'];
            }
			
            $encoded['body'] = '--' . $boundary . MAIL_MIMEPART_CRLF .
                               implode('--' . $boundary . MAIL_MIMEPART_CRLF, $subparts) .
                               '--' . $boundary.'--' . MAIL_MIMEPART_CRLF;
			
        } else {
            $encoded['body'] = $this->_getEncodedData($this->_body, $this->_encoding) . MAIL_MIMEPART_CRLF . MAIL_MIMEPART_CRLF;
        }
		
        // Add headers to $encoded
        $encoded['headers'] =& $this->_headers;
		
        return $encoded;
    }

    /**
     * &addSubPart()
     *
     * Adds a subpart to current mime part and returns
     * a reference to it
     *
     * @param $body   The body of the subpart, if any.
     * @param $params The parameters for the subpart, same
     *                as the $params argument for constructor.
     * @return A reference to the part you just added. It is
     *         crucial if using multipart/* in your subparts that
     *         you use =& in your script when calling this function,
     *         otherwise you will not be able to add further subparts.
     * @access public
     */
    function &addSubPart($body, $params)
    {
        $this->_subparts[] = new Mail_mimePart($body, $params);
        return $this->_subparts[count($this->_subparts) - 1];
    }

    /**
     * _getEncodedData()
     *
     * Returns encoded data based upon encoding passed to it
     *
     * @param $data     The data to encode.
     * @param $encoding The encoding type to use, 7bit, base64,
     *                  or quoted-printable.
     * @access private
     */
    function _getEncodedData($data, $encoding)
    {
        switch ($encoding) {
            case '8bit':
            case '7bit':
                return $data;
                break;

            case 'quoted-printable':
                return $this->_quotedPrintableEncode($data);
                break;

            case 'base64':
                return rtrim(chunk_split(base64_encode($data), 76, MAIL_MIMEPART_CRLF));
                break;

            default:
                return $data;
        }
    }

    /**
     * quoteadPrintableEncode()
     *
     * Encodes data to quoted-printable standard.
     *
     * @param $input    The data to encode
     * @param $line_max Optional max line length. Should
     *                  not be more than 76 chars
     *
     * @access private
     */
    function _quotedPrintableEncode($input , $line_max = 76)
    {
        $lines  = preg_split("/\r?\n/", $input);
        $eol    = MAIL_MIMEPART_CRLF;
        $escape = '=';
        $output = '';

        while(list(, $line) = each($lines)){

            $linlen     = strlen($line);
            $newline = '';

            for ($i = 0; $i < $linlen; $i++) {
                $char = substr($line, $i, 1);
                $dec  = ord($char);

                if (($dec == 32) AND ($i == ($linlen - 1))){    // convert space at eol only
                    $char = '=20';

                } elseif($dec == 9) {
                    ; // Do nothing if a tab.
                } elseif(($dec == 61) OR ($dec < 32 ) OR ($dec > 126)) {
                    $char = $escape . strtoupper(sprintf('%02s', dechex($dec)));
                }

                if ((strlen($newline) + strlen($char)) >= $line_max) {        // MAIL_MIMEPART_CRLF is not counted
                    $output  .= $newline . $escape . $eol;                    // soft line break; " =\r\n" is okay
                    $newline  = '';
                }
                $newline .= $char;
            } // end of for
            $output .= $newline . $eol;
        }
        $output = substr($output, 0, -1 * strlen($eol)); // Don't want last crlf
        return $output;
    }
} // End of class
class smtp_class{
// SMTP Support
/*
	<version>@(#) $Id: smtp.php,v 1.41 2009/04/12 06:15:06 mlemos Exp $</version>
	<copyright>Copyright © (C) Manuel Lemos 1999-2009</copyright>
	<title>Sending e-mail messages via SMTP protocol</title>
	<author>Manuel Lemos</author>
	<authoraddress>mlemos-at-acm.org</authoraddress>
*/
	var $user="";
	var $realm="";
	var $password="";
	var $workstation="";
	var $authentication_mechanism="";
	var $host_name="";
	var $host_port=25;
	var $ssl=0;
	var $start_tls = 0;
	var $localhost="";
	var $timeout=0;
	var $data_timeout=0;
	var $direct_delivery=0;
	var $error="";
	var $debug=0;
	var $html_debug=0;
	var $esmtp=1;
	var $esmtp_extensions=array();
	var $exclude_address="";
	var $getmxrr="GetMXRR";
	var $pop3_auth_host="";
	var $pop3_auth_port=110;
	
	/* private variables - DO NOT ACCESS */

	var $state="Disconnected";
	var $connection=0;
	var $pending_recipients=0;
	var $next_token="";
	var $direct_sender="";
	var $connected_domain="";
	var $result_code;
	var $disconnected_error=0;
	var $esmtp_host="";
	var $maximum_piped_recipients=100;

	/* Private methods - DO NOT CALL */

	Function Tokenize($string,$separator="")
	{
		if(!strcmp($separator,""))
		{
			$separator=$string;
			$string=$this->next_token;
		}
		for($character=0;$character<strlen($separator);$character++)
		{
			if(GetType($position=strpos($string,$separator[$character]))=="integer")
				$found=(IsSet($found) ? min($found,$position) : $position);
		}
		if(IsSet($found))
		{
			$this->next_token=substr($string,$found+1);
			return(substr($string,0,$found));
		}
		else
		{
			$this->next_token="";
			return($string);
		}
	}
	
	Function setError($error){
		$this->error = $error;
		$this->OutputDebug("Erro ocurred: {$error}");
	}
	Function OutputDebug($message){
		if($this->debug){
			$message.="\n";
			if($this->html_debug)
				$message=str_replace("\n","<br />\n",HtmlEntities($message));
			echo $message;
			flush();
		}
	}

	Function SetDataAccessError($error)
	{
		$this->setError($error);
		if(function_exists("socket_get_status"))
		{
			$status=socket_get_status($this->connection);
			if($status["timed_out"])
				$this->error.=": data access time out";
			elseif($status["eof"])
			{
				$this->error.=": the server disconnected";
				$this->disconnected_error=1;
			}
		}
	}

	Function GetLine()
	{
		for($line="";;)
		{
			if(feof($this->connection))
			{
				$this->setError("reached the end of data while reading from the SMTP server conection");
				return("");
			}
			if(GetType($data=@fgets($this->connection,100))!="string" || strlen($data)==0){
				$this->SetDataAccessError("it was not possible to read line from the SMTP server");
				return("");
			}
			$line  .= $data;
			$length = strlen($line);
			if($length>=1 && substr($line,-1)=="\n"){
				$this->OutputDebug("S ".str_replace(Array("\r\n", "\r", "\n"), Array("<CRNL>", "<CR>", "<NL>"), $line));
				$line = rtrim($line, "\r\n");
				return($line);
			}
		}
	}

	Function PutLine($line)
	{
		$this->OutputDebug("C {$line}<CRNL>");
		if(!@fputs($this->connection,"$line\r\n"))
		{
			$this->SetDataAccessError("it was not possible to send a line to the SMTP server");
			return(0);
		}
		return(1);
	}

	Function PutData(&$data)
	{
		if(strlen($data))
		{
			$this->OutputDebug("C $data");
			if(!@fputs($this->connection,$data))
			{
				$this->SetDataAccessError("it was not possible to send data to the SMTP server");
				return(0);
			}
		}
		return(1);
	}

	Function VerifyResultLines($code,&$responses)
	{
		$responses=array();
		Unset($this->result_code);
		while(strlen($line=$this->GetLine($this->connection))){
			if(IsSet($this->result_code)){
				if(strcmp($this->Tokenize($line," -"),$this->result_code)){
					$this->setError($line);
					return(0);
				}
			}
			else{
				$this->result_code=$this->Tokenize($line," -");
				if(GetType($code)=="array"){
					for($codes=0;$codes<count($code) && strcmp($this->result_code,$code[$codes]);$codes++);
					if($codes>=count($code)){
						$this->setError($line);
						return(0);
					}
				}
				else{
					if(strcmp($this->result_code,$code)){
						$this->setError($line);
						return(0);
					}
				}
			}
			$responses[]=$this->Tokenize("");
			if(!strcmp($this->result_code,$this->Tokenize($line," ")))
				return(1);
		}
		return(-1);
	}

	Function FlushRecipients()
	{
		if($this->pending_sender)
		{
			if($this->VerifyResultLines("250",$responses)<=0)
				return(0);
			$this->pending_sender=0;
		}
		for(;$this->pending_recipients;$this->pending_recipients--)
		{
			if($this->VerifyResultLines(array("250","251"),$responses)<=0)
				return(0);
		}
		return(1);
	}

	Function ConnectToHost($domain, $port, $resolve_message)
	{
		if($this->ssl)
		{
			$version=explode(".",function_exists("phpversion") ? phpversion() : "3.0.7");
			$php_version=intval($version[0])*1000000+intval($version[1])*1000+intval($version[2]);
			if($php_version<4003000)
				return("establishing SSL connections requires at least PHP version 4.3.0");
			if(!function_exists("extension_loaded")
			|| !extension_loaded("openssl"))
				return("establishing SSL connections requires the OpenSSL extension enabled");
		}
		if(function_exists('preg_match') ? preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $domain) : ereg('^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$',$domain))
			$ip=$domain;
		else
		{
			$this->OutputDebug($resolve_message);
			if(!strcmp($ip=@gethostbyname($domain),$domain))
				return("could not resolve host \"".$domain."\"");
		}
		if(strlen($this->exclude_address)
		&& !strcmp(@gethostbyname($this->exclude_address),$ip))
			return("domain \"".$domain."\" resolved to an address excluded to be valid");
		$this->OutputDebug("Connecting to host address \"".$ip."\" port ".$port."...");
		if(($this->connection=($this->timeout ? @fsockopen(($this->ssl ? "ssl://" : "").$ip,$port,$errno,$error,$this->timeout) : @fsockopen(($this->ssl ? "ssl://" : "").$ip,$port))))
			return("");
		$error=($this->timeout ? strval($error) : "??");
		switch($error)
		{
			case "-3":
				return("-3 socket could not be created");
			case "-4":
				return("-4 dns lookup on hostname \"".$domain."\" failed");
			case "-5":
				return("-5 connection refused or timed out");
			case "-6":
				return("-6 fdopen() call failed");
			case "-7":
				return("-7 setvbuf() call failed");
		}
		return("could not connect to the host \"".$domain."\": ".$error);
	}

	Function SASLAuthenticate($mechanisms, $credentials, &$authenticated, &$mechanism)
	{
		$authenticated=0;
		if(!function_exists("class_exists")
		|| !class_exists("sasl_client_class"))
		{
			$this->setError("it is not possible to authenticate using the specified mechanism because the SASL library class is not loaded");
			return(0);
		}
		$sasl=new sasl_client_class;
		$sasl->SetCredential("user",$credentials["user"]);
		$sasl->SetCredential("password",$credentials["password"]);
		if(IsSet($credentials["realm"]))
			$sasl->SetCredential("realm",$credentials["realm"]);
		if(IsSet($credentials["workstation"]))
			$sasl->SetCredential("workstation",$credentials["workstation"]);
		if(IsSet($credentials["mode"]))
			$sasl->SetCredential("mode",$credentials["mode"]);
		do
		{
			$status=$sasl->Start($mechanisms,$message,$interactions);
		}
		while($status==SASL_INTERACT);
		switch($status)
		{
			case SASL_CONTINUE:
				break;
			case SASL_NOMECH:
				if(strlen($this->authentication_mechanism)){
					$this->setError("authenticated mechanism ".$this->authentication_mechanism." may not be used: ".$sasl->error);
					return(0);
				}
				break;
			default:
				$this->setError("Could not start the SASL authentication client: ".$sasl->error);
				return(0);
		}
		if(strlen($mechanism=$sasl->mechanism)){
			if($this->PutLine("AUTH ".$sasl->mechanism.(IsSet($message) ? " ".base64_encode($message) : ""))==0){
				$this->setError("Could not send the AUTH command");
				return(0);
			}
			if(!$this->VerifyResultLines(array("235","334"),$responses))
				return(0);
			switch($this->result_code)
			{
				case "235":
					$response="";
					$authenticated=1;
					break;
				case "334":
					$response=base64_decode($responses[0]);
					break;
				default:
					$this->setError("Authentication error: ".$responses[0]);
					return(0);
			}
			for(;!$authenticated;)
			{
				do
				{
					$status=$sasl->Step($response,$message,$interactions);
				}
				while($status==SASL_INTERACT);
				switch($status)
				{
					case SASL_CONTINUE:
						if($this->PutLine(base64_encode($message))==0)
						{
							$this->setError("Could not send the authentication step message");
							return(0);
						}
						if(!$this->VerifyResultLines(array("235","334"),$responses))
							return(0);
						switch($this->result_code)
						{
							case "235":
								$response="";
								$authenticated=1;
								break;
							case "334":
								$response=base64_decode($responses[0]);
								break;
							default:
								$this->setError("Authentication error: ".$responses[0]);
								return(0);
						}
						break;
					default:
						$this->setError("Could not process the SASL authentication step: ".$sasl->error);
						return(0);
				}
			}
		}
		return(1);
	}
	
	Function StartSMTP($localhost)
	{
		$success = 1;
		$this->esmtp_extensions = array();
		$fallback=1;
		if($this->esmtp
		|| strlen($this->user))
		{
			if($this->PutLine('EHLO '.$localhost))
			{
				if(($success_code=$this->VerifyResultLines('250',$responses))>0)
				{
					$this->esmtp_host=$this->Tokenize($responses[0]," ");
					for($response=1;$response<count($responses);$response++)
					{
						$extension=strtoupper($this->Tokenize($responses[$response]," "));
						$this->esmtp_extensions[$extension]=$this->Tokenize("");
					}
					$success=1;
					$fallback=0;
				}
				else
				{
					if($success_code==0)
					{
						$code=$this->Tokenize($this->error," -");
						switch($code)
						{
							case "421":
								$fallback=0;
								break;
						}
					}
		

OK, that was an insane amount of code to send an email. I include "phpMailer/class.phpmailer.php" and here is my code. Tested and works. This really not all that much different than setting up an email client with smtp, it works the same way so I would read the documentation from your smtp provider and see if there are any catches or anything else is needed for the smtp server other than standard authentication.

<?php
require("phpMailer/class.phpmailer.php");
$objMail = new PHPmailer();
$objMail->SetLanguage("en", "phpmailer/language");
$objMail->From = "you@youremail.com"; // the email address that the email is from
$objMail->FromName = "your name"; // the name that the email is from
$objMail->Host = "your.smtpserver.com"; // the smpt host server
$objMail->Mailer   = "smtp"; // the protocol
$objMail->Password = "your_smtp_password"; // your smtp password for the server
$objMail->Username = "your_smtp_username"; // your user name for your smtp server, usually your email address
$objMail->Subject = "this is a test of phpmailer";
$objMail->SMTPAuth  =  "true"; // smtp requires authentication
$objMail->Body = "this is a test email, please do not repy";
$objMail->AddAddress("emailaddress@tosendto.com");  // the email address you want to send to
$objMail->AddReplyTo("you@youremail.com");  // the reply to email address
if (!$objMail->Send())
{
echo "There was an error in sending mail, please try again at a later time\n\n";
}
$objMail->ClearAddresses();
?>

Dear Friend,

First of all i would like to convey my Thanks for your reply,

I used your suggested script and i am getting the below error

AT present we moved our mail service to Microsoft exchange,

Warning: fsockopen() [function.fsockopen]: unable to connect to red003.mail.apac.microsoftonline.com:25 (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ) in C:\xampp\htdocs\kattendance\phpmailer\class.smtp.php on line 89
There was an error in sending mail, please try again at a later time

red003.mail.apac.microsoftonline.com is an exchange address, not an smtp server. You can contact microsoft to find out what your smtp server address is or how to configure it or you can set up a standard email account through gmail or some other company that does provide smtp services. I would have no idea how to connect and send through exchange.

Dear Friend

Thanks for your advice

As per your advice, i used gmail smtp

I am getting the below error

There was an error in sending mail, please try again at a later time

Hi Friend

Thank for your advice,

if this is solved, go ahead and mark it as solved. thx.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.