shows html code while sending mail problem ??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

shows html code while sending mail problem ??

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
Post Reply