I was wondering if there was a way to create a PHP Contact Form where
the email that is sent can be formatted for html. So that for example
the email that is received is formatted like an html page where it
contains a background, images & formatted text. I can't figure out how
to do this. I tried something like "$MESSAGE_BODY .= "<img src="images/
bgaccess.jpg" width="800" height="653" />;" to insert an image but I
received an error. I would think there was a way to format the email.
Can anyone help?

I have the following code so far:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
if ($_POST["email"]<>'') {
        $ToEmail = '...@email.com';
        $EmailSubject = 'Site contact form ';
        $mailheader = "From: ".$_POST["email"]."\r\n";
        $mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
        $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
        $MESSAGE_BODY = "<img src="images/bgaccess.jpg" width="800"
height="653" />";
        $MESSAGE_BODY .= "Name: ".$_POST["name"]."<br>";
        $MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>";
        $MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])."<br>";
        mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die
("Failure");
?>
Your message was sent
<?php
} else {

?>
<form action="test.php" method="post">
<table width="400" border="0" cellspacing="2" cellpadding="0">
<tr>
<td width="29%" class="bodytext">Your name:</td>
<td width="71%"><input name="name" type="text" id="name" size="32"></
td>
</tr>
<tr>
<td class="bodytext">Email address:</td>
<td><input name="email" type="text" id="email" size="32"></td>
</tr>
<tr>
<td class="bodytext">Comment:</td>
<td><textarea name="comment" cols="45" rows="6" id="comment"
class="bodytext"></textarea></td>
</tr>
<tr>
<td class="bodytext">&nbsp;</td>
<td align="left" valign="top"><input type="submit" name="Submit"
value="Send"></td>
</tr>
</table>
</form>
<?php
};

?>
</body>
</html>

Recommended Answers

All 7 Replies

For my html mail the only thing I did was alter my headers and there was really no problem. It's not that much different from what you already have. I suppose this is something I could stand to learn a little more about ... once I got it working I just moved on to something else ;)

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Somebody' . "\r\n" ;
$headers .= 'Reply-To: somebody@some_site.com' . "\r\n" ;
$headers .= 'X-Mailer: PHP/' . phpversion();

The one thing I would do different here is to ad your url

$MESSAGE_BODY .= '<img src="http://yourURL.com/images/
bgaccess.jpg" width="800" height="653" />';

Also be aware that most new email clients will ignore background images and behavioural css.

Really? See that ... there really is no limit to the things you learn every day.

<?
ob_start();
extract($_REQUEST);
include("includes/class.phpmailer.php");
include("includes/class.smtp.php");
if($_POST)
{
//echo $attach;exit;
/////////////////////////////////////////////////////////////////////////////////////////////
///////             send email               /////////
///////////////////////////////////////////////////////////////////////////////////////////
		 
	
	$img_name=$_FILES["attach"]["name"];
    $file_name = $img_name;
move_uploaded_file($_FILES["attach"]["tmp_name"], "attachments/" . $file_name); 	
		
$sitename="sitename";
	
		//this subject will be visible only for you
		$sub = "subject here";
	
		$msg = "<table width='100%'  border='0' cellspacing='0' cellpadding='0'>
					  <tr>
						<td height='8'></td>
					  </tr>
					 
					  <tr>
						<td height='8'></td>
					  </tr>
					   
					  
					  
					  <tr>
						<td height='4'></td>
					  </tr>
					</table>";
			
			$msg = stripslashes($msg);
			//echo $msg;
//	echo "<br>";
//		echo exit;
			
			
			
			//include("includes/constants.php");
		
			$SmtpAuth = 1; // 1 For IsSMTP() and 0 For IsMail()	
			
			$mail = new PHPMailer();
			
			//$mail->IsMail();
			if($SmtpAuth == 0)
			{
				$mail->IsMail();
			} 
			else 
			{
				$mail->IsSMTP();
				$mail->Host = "write your hostname";  // specify main and backup server
				$mail->SMTPAuth = true;     // turn on SMTP authentication
				$mail->Username = "write smtp username";  // SMTP username
				$mail->Password = "write password here"; // SMTP password
			}                                      // set mailer to use SMTP
			
			
			
				$mail->From     = $_POST[email];
				$mail->FromName = $_POST[name];
				$mail->AddAddress($_POST[friendemail]);
				$mail->AddAttachment('attachments/'.$_FILES["attach"]["name"]);
/*				$mail->AddCC($resNewsUsers["emailid"]);
				$mail->AddBCC($resNewsUsers["emailid"]);*/
				$mail->IsHTML(true);                                  // set email format to HTML
				$mail->Subject = $sub;
				$mail->Body = $msg;
				$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
				$mail->Send();
			
		$send=1;
/////////////////////////////////////////////////////////////////////////////////////////////
///////            end          /////////
///////////////////////////////////////////////////////////////////////////////////////////

}
?>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <script type="text/javascript" src="http://www.google.com/jsapi">
    </script>
    
  </head>
  <body>
  <form name="form" method="post" enctype="multipart/form-data">
  <center>you can send mails in any languages</center>
  <div id='translControl'>
  <? if($send==1)
  {
  ?>
  successfully sent.
  <?
  }
  ?>
  
     
</div>
<br>name: <input type='textbox' id="name" name="name"/>
<br>email: <input type='textbox' id="email" name="email"/>
<br>
to email: 
<input type='textbox' id="friendemail" name="friendemail"/>
    <br>Title : <input type='textbox' id="transl1" name="transl1"/>
	 <tr align="center">
              <td width="23%">&nbsp;</td>
              <td width="14%" align="left">Attach</td>
              <td width="3%" align="left">&nbsp;</td>
              <td width="51%" align="left">
                  <input type="file" name="attach">			  </td>
              <td width="2%">&nbsp;</td>
			 </tr>					 	 					   		   					   

    <br>Body<br><textarea id="transl2" name="transl2" style="width:600px;height:200px"></textarea>
	
	<input type="submit" name="submit" value="submit"></input>
    <br><div id="errorDiv"></div>
	</form>
  </body>
</html>

see this using smtp we can send mails from localhost. in localhost $SmtpAuth = 1; and inserver put like this $SmtpAuth = 0

For my html mail the only thing I did was alter my headers and there was really no problem. It's not that much different from what you already have. I suppose this is something I could stand to learn a little more about ... once I got it working I just moved on to something else ;)

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Somebody' . "\r\n" ;
$headers .= 'Reply-To: somebody@some_site.com' . "\r\n" ;
$headers .= 'X-Mailer: PHP/' . phpversion();

The one thing I would do different here is to ad your url

$MESSAGE_BODY .= '<img src="http://yourURL.com/images/
bgaccess.jpg" width="800" height="653" />';

I tried your suggestion so that the code now looks like this:

$headers  = 'MIME-Version: 1.0' . "\r\n";
		$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
		$headers .= "From: ".$_POST["email"]. "\r\n" ;
		$headers .= 'Reply-To: '.$_POST["email"] . "\r\n" ;
		$headers .= 'X-Mailer: PHP/' . phpversion();
		
		$MESSAGE_BODY .= '<img src="http://portal.domain.net/images/bgaccess.jpg" width="200" height="300" />';
        $MESSAGE_BODY .= "Name: ".$_POST["name"]."<br>";
        $MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>";
        $MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])."<br>";
        mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");

However this changed the email to Plain Text and I saw actual code instead of HTML. I am using Dreamweaver CS4. Could this have anything to do with it?

<?
ob_start();
extract($_REQUEST);
include("includes/class.phpmailer.php");
include("includes/class.smtp.php");
if($_POST)
{
//echo $attach;exit;
/////////////////////////////////////////////////////////////////////////////////////////////
///////             send email               /////////
///////////////////////////////////////////////////////////////////////////////////////////
		 
	
	$img_name=$_FILES["attach"]["name"];
    $file_name = $img_name;
move_uploaded_file($_FILES["attach"]["tmp_name"], "attachments/" . $file_name); 	
		
$sitename="sitename";
	
		//this subject will be visible only for you
		$sub = "subject here";
	
		$msg = "<table width='100%'  border='0' cellspacing='0' cellpadding='0'>
					  <tr>
						<td height='8'></td>
					  </tr>
					 
					  <tr>
						<td height='8'></td>
					  </tr>
					   
					  
					  
					  <tr>
						<td height='4'></td>
					  </tr>
					</table>";
			
			$msg = stripslashes($msg);
			//echo $msg;
//	echo "<br>";
//		echo exit;
			
			
			
			//include("includes/constants.php");
		
			$SmtpAuth = 1; // 1 For IsSMTP() and 0 For IsMail()	
			
			$mail = new PHPMailer();
			
			//$mail->IsMail();
			if($SmtpAuth == 0)
			{
				$mail->IsMail();
			} 
			else 
			{
				$mail->IsSMTP();
				$mail->Host = "write your hostname";  // specify main and backup server
				$mail->SMTPAuth = true;     // turn on SMTP authentication
				$mail->Username = "write smtp username";  // SMTP username
				$mail->Password = "write password here"; // SMTP password
			}                                      // set mailer to use SMTP
			
			
			
				$mail->From     = $_POST[email];
				$mail->FromName = $_POST[name];
				$mail->AddAddress($_POST[friendemail]);
				$mail->AddAttachment('attachments/'.$_FILES["attach"]["name"]);
/*				$mail->AddCC($resNewsUsers["emailid"]);
				$mail->AddBCC($resNewsUsers["emailid"]);*/
				$mail->IsHTML(true);                                  // set email format to HTML
				$mail->Subject = $sub;
				$mail->Body = $msg;
				$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
				$mail->Send();
			
		$send=1;
/////////////////////////////////////////////////////////////////////////////////////////////
///////            end          /////////
///////////////////////////////////////////////////////////////////////////////////////////

}
?>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <script type="text/javascript" src="http://www.google.com/jsapi">
    </script>
    
  </head>
  <body>
  <form name="form" method="post" enctype="multipart/form-data">
  <center>you can send mails in any languages</center>
  <div id='translControl'>
  <? if($send==1)
  {
  ?>
  successfully sent.
  <?
  }
  ?>
  
     
</div>
<br>name: <input type='textbox' id="name" name="name"/>
<br>email: <input type='textbox' id="email" name="email"/>
<br>
to email: 
<input type='textbox' id="friendemail" name="friendemail"/>
    <br>Title : <input type='textbox' id="transl1" name="transl1"/>
	 <tr align="center">
              <td width="23%">&nbsp;</td>
              <td width="14%" align="left">Attach</td>
              <td width="3%" align="left">&nbsp;</td>
              <td width="51%" align="left">
                  <input type="file" name="attach">			  </td>
              <td width="2%">&nbsp;</td>
			 </tr>					 	 					   		   					   

    <br>Body<br><textarea id="transl2" name="transl2" style="width:600px;height:200px"></textarea>
	
	<input type="submit" name="submit" value="submit"></input>
    <br><div id="errorDiv"></div>
	</form>
  </body>
</html>

see this using smtp we can send mails from localhost. in localhost $SmtpAuth = 1; and inserver put like this $SmtpAuth = 0

This is way over my head... Please forgive me as i'm a beginner...

Are these files that I need to download and save to the includes directory?
When attempting this code I received the following errors when opening the page:

Warning: include(includes/class.phpmailer.php) [function.include]: failed to open stream: No such file or directory in /data/17/1/138/3/1790329/user/1937057/htdocs/Portal/test.php on line 4

Warning: include(includes/class.phpmailer.php) [function.include]: failed to open stream: No such file or directory in /data/17/1/138/3/1790329/user/1937057/htdocs/Portal/test.php on line 4

Warning: include() [function.include]: Failed opening 'includes/class.phpmailer.php' for inclusion (include_path='.:/usr/services/vux/lib/php') in /data/17/1/138/3/1790329/user/1937057/htdocs/Portal/test.php on line 4

Warning: include(includes/class.smtp.php) [function.include]: failed to open stream: No such file or directory in /data/17/1/138/3/1790329/user/1937057/htdocs/Portal/test.php on line 5

Warning: include(includes/class.smtp.php) [function.include]: failed to open stream: No such file or directory in /data/17/1/138/3/1790329/user/1937057/htdocs/Portal/test.php on line 5

Warning: include() [function.include]: Failed opening 'includes/class.smtp.php' for inclusion (include_path='.:/usr/services/vux/lib/php') in /data/17/1/138/3/1790329/user/1937057/htdocs/Portal/test.php on line 5

Also, i'm not sure what to do with this:

$mail->IsSMTP();
				$mail->Host = "write your hostname";  // specify main and backup server
				$mail->SMTPAuth = true;     // turn on SMTP authentication
				$mail->Username = "write smtp username";  // SMTP username
				$mail->Password = "write password here"; // SMTP password

I'm thinking this defines where to send the email? Is there anyway to do a simple "to" line? The email address I would like to send it to is on an exchange server. I'm not sure how to complete this section.

I do however love the code because it solved my next question on how to attach a file. So I would like to try to figure this out...

you include phpmailer class, smtp in your includes folder. it will free download.
and next thing

$mail->IsSMTP();
				$mail->Host = "write your hostname";  // specify main and backup server
				$mail->SMTPAuth = true;     // turn on SMTP authentication
				$mail->Username = "write smtp username";  // SMTP username
				$mail->Password = "write password here"; // SMTP password

hostname:localhost.
username:write smtp username. //website username email
password: password //website password

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.