sending email with html content

A

Anonymous

Guest
Hi guys,

I am trying to use html code as content of the email i sending out by using php but why the email my friends received are all in text format ???

what should i do to make my html code works in an email... i have created a html file and want to send it out as newsletter.. what i did is copy all the html code from the file and paste it as value for $contents but this doesn't seem to work.,,

please advise what i suppose to do,, is it i missed some header ??


Code:
// connect to db code.. 

$to = "{$row['email']}\r\n";
//$from= "From: $name < $email >\r\n";
$from= "From: testing while< myemail >\r\n";
$subject ="$subject\r\n";

if($mailtype != "html")
			{
	$MailText = stripslashes($MailText);
			}

$contents ="$MailText";

mail($to,$subject,$contents,$from);
 
Like setting your From address, you need to set Content-type to text/html.

So, you would do (Example):
Code:
<?php

// Check if the form was submitted
if(isset($_POST['submit'])){

// Get the subject and message from the form
$subject = $_POST['subject'];
$contents = $_POST['contents'];

// Get date/time
$date = date("y-m-d H:i:s");

// Making the user email address (to send to)
$to = $_POST['name'] . " <" . $_POST['email'] . ">";

// Creating header information
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";	
$headers .= "Reply-To: admin <you@yoursite.com>\r\n"; 
$headers .= "X-Mailer: PHP/" . phpversion();
$headers .= "From: admin <you@yoursite.com>\r\n";

// html message
$message = '
<html>
<head>
   <title>' . $subject . '</title>
</head>
<body>
   <table width="100%">
      <tr>
         <td>' . $contents . '</td>
      </tr>
   </table>
</body>
</html>';

// Send email
$mail = mail($to, $subject . ' - ' . $date, $message, $headers);

// Prints a message depending on the sent email status
$mail ? echo 'Email sent !' : echo 'Error sending email !';

}

?>

Gesf
 
Hi guys,

now it can shows the html page after i added the headers (after refer to complex email part from php.net)..

i have another problems now.. i am wondering why when i viewed the email, the html page displayed on my mailbox looks different from the online version ???
problem i encountered :
- some part of the page colors are different from the online page.,. for instance, a cell suppose to be yellow color but it displayed green in my mailbox
- some of the textfield contain unwanted values such as \"submit \", \"login\" and etc.. the textfields value should be empty but how come there are values with slashes there ??

when i add stripslashes function for my content, i would not able to view the html page (only text displayed),, but if i dun add this function, i am not able to view the proper html page..

please advise what i suppose to do,, thanks.

Code:
$from= "From: testing while< myemail >\r\n";
$subject ="$subject\r\n";

if($mailtype != "html")
			{
	$contents = stripslashes($MailText);
			}
else 
			{
$contents ="$MailText";
$from .= "MIME-Version: 1.0\r\n";
$from .= "Content-type: text/html; charset=iso-8859-1\r\n";
			}

$contents.="
============================================
Please visit http://testing.com to unsubscribe from the mailing list. Thank you. 
============================================";

mail($to,$subject,$contents,$from);
if(mail)
			{
	echo ' email is sent to :';
	echo "<br>".$row['email']."<br>";
			}
		}
 
Most email apps aren't designed to do fancy html so that might be the problem. Try using CSS and include it inline into your email.
 
Yes, use CSS and with the full url to an (online) style-sheet, images... whatever!
 
Back
Top