Page 1 of 1

shows html code while sending mail problem ??

Posted: Fri Jun 08, 2007 4:18 am
by PHPycho
Hello forums !!
I am using my mail function to send the mail, mail successfully goes but the problem is its shows all the html codes and also the name is blank in the from section (Note: I am using outlook express)
i had used the following code:

Code: Select all

<?php
class Mailer{
	var $to;
	var $from;
	var $cc;
	var $bcc;
	var $headers;
	var $subject;
	var $message;
	
	function Mailer(){		
	    $this->to = "";		
		$this->from = "";
		$this->cc = "";		
		$this->bcc = "";		
		// To send HTML mail, the Content-type header must be set
		$headers  = 'MIME-Version: 1.0' . "\r\n";
		$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";		
		// Additional headers
		$headers .= 'To: '.$this->to . "\r\n";
		$headers .= 'From: '.$this->from . "\r\n";
		$headers .= 'Reply-To: '.$this->from . "\r\n";
		$headers .= 'Cc: '.$this->cc . "\r\n";
		$headers .= 'Bcc: '.$this->bcc . "\r\n";
		$this->headers = $headers;		
	}
	
	function setSubject(){
		$this->subject = "subjects goes here..";
	}
	
	function setMessage($message){			
		# set Message
$message = <<<EOF
<html>
<head>
</head>
  <body> 
      
<p>&nbsp;</p>
		<table width="455" border="0" align="center" cellpadding="0" cellspacing="10" bgcolor="#FFFFFF">	
			<tr>
				<td width="88"><span class="field">Message</span></td>
			  <td width="289"><span class="fieldValue">$message</span></td>
			</tr>
			
		</table>
	  
  </body>
</html>
EOF;
$this->message = $message;
	}
	
	function sendMail(){
		return mail($this->to, $this->subject, $this->message, $this->headers);
	}

}

/**
 * Usage
 */

	$Mailer = new Mailer();
	$Mailer->setSubject();
	$Mailer->setMessage("Message goes here...");
	$Mailer->sendMail();	
?>
How to make the code working (ie output should displayed as text not the code and also the from section should be visible (Note i m using outlook express))
Thanks in advance to all of you

Posted: Fri Jun 08, 2007 7:25 am
by Chris Corbyn
<side note>
Your From: header is set in the constructor and at present there's no way to change it.

Maybe better to do this:

Code: Select all

class Mailer {
  var $headers = array();
  
  function Mailer()
  {
    $this->setHeader('MIME-Version', '1.0');
    $this->setHeader('Content-type', 'text/html; charset=iso-8859-1');          
    // Additional headers
    $this->setHeader('To', $this->to);
    $this->setHeader('From', $this->from);
    // ... snip ....
  }
  
  function setHeader($name, $value)
  {
    $this->headers[$name] = $value;
  }

  function sendMail()
  {
    return mail($this->to, $this->subject, $this->message, implode("\r\n", $this->headers)); 
  }
}
But even that is not going to work very well if you want widespread delivery success + internationalization (charsets).

From looking at your headers however, I don't see a problem, except that if you're on a linux server you need \n not \r\n otherwise all your header line spaces get doubled up and your email is therefore broken.