1

Hello The Genious peoples.... I am trying to send a html mail using php everything in my php script looks to be correct but when I send the mail I recieve only plain text. This is the link url i am using to test the script http://www.mailme.netne.net you may check it here..

These are the codes in script:

<?php 
require 'ErrorHandler.inc.php';  
//.....set up a boundary to seperate the message..........  
$boundary = '======'.md5(mt_rand(4,time())).'======';  
$headers=array();  
$headers[]='MIME-Version:1.0';  
$headers[]='Content-type:multipart/alternative;boundary="'.$boundary.'"'; 
$headers[]='From: '.$_POST['from'];  
$msg_body = 'This a is Multipart Message in MIME Format'."\n";  
$msg_body .= '--'.$boundary."\n";  
$msg_body .= 'Content-Type:text/html; charset="iso-8859-1"'."\n"; 
$msg_body .= 'Content-Transfer-Encoding:7bit'."\n\n";  
$msg_body .=$_POST['message'] ."\n"; 
$msg_body .= '--'.$boundary."\n"; 
$msg_body .= 'Content-Type:text/plain; charset="iso-8859-1"'."\n";
$msg_body .= 'Content-Transfer-Encoding:7bit'."\n\n";  
$msg_body .=strip_tags($_POST['message']) . "\n";  
$msg_body .= '--'.$boundary.'--'."\n";  
//======================send n test return value=======================
$success  = mail($_POST['to'],$_POST['sub'],$msg_body,implode("\r\n",$headers)) ;
if($success)  
{ echo "<strong>Your mail sent </strong>";}    
else     
{echo "<strong>Error Sending your mail..please try again later</strong>";}  
echo " with following details:<br /><br />";  
echo "<strong>From : </strong><em>" .$_POST['from']."<br />";  
echo "<strong>To&nbsp;&nbsp;: </strong><em>".$_POST['to'].  "<br/>";  
echo "<strong>Subject : </strong><em>".$_POST['sub']."<br />";  
echo "<strong>Message : </strong><em>".$msg_body."<br />";  
 ?>      

please help me ..I am trying since last 6 days..

4
  • You might want to use swiftmailer.org instead of writing your own. Commented Sep 6, 2012 at 16:28
  • 1
    It's not an answer to your problem, but you really should think again about a script that sends user-entered text to a user-entered email address. As it is, it's wide open to abuse by spammers. Commented Sep 6, 2012 at 16:30
  • There are many libraries available for constructing emails for you, like PHPMailer. You'd be better off using one of them instead of trying to concat an email together ad-hoc. Commented Sep 6, 2012 at 16:35
  • try phpmailer its very cool and easy ,effective Commented Sep 6, 2012 at 16:35

1 Answer 1

1

You're adding header data to your message body:

$msg_body .= 'Content-Type:text/html; charset="iso-8859-1"'."\n"; 
$msg_body .= 'Content-Transfer-Encoding:7bit'."\n\n"; 

But you're not actually adding it to your headers like so:

$headers[] = 'Content-Type:text/html; charset="iso-8859-1"';
$headers[] = 'Content-Transfer-Encoding:7bit';

Try this:

<?php 
require 'ErrorHandler.inc.php'; 

//.....set up a boundary to seperate the message..........  
$boundary = '======'.md5(mt_rand(4,time())).'======';  

$headers    =   array();  
$headers[]  =   'MIME-Version:1.0';  
$headers[]  =   'Content-type:multipart/alternative;boundary="'.$boundary.'"'; 
$headers[]  =   'From: '.$_POST['from']; 
$headers[]  =   'Content-Type:text/html; charset="iso-8859-1"';
$headers[]  =   'Content-Transfer-Encoding:7bit';

$msg_body = 'This a is Multipart Message in MIME Format'."\n";  
$msg_body .= '--'.$boundary."\n";  
$msg_body .= 'Content-Type:text/html; charset="iso-8859-1"'."\n"; 
$msg_body .= 'Content-Transfer-Encoding:7bit'."\n\n";  
$msg_body .=$_POST['message'] ."\n"; 
$msg_body .= '--'.$boundary."\n"; 
$msg_body .= 'Content-Type:text/plain; charset="iso-8859-1"'."\n";
$msg_body .= 'Content-Transfer-Encoding:7bit'."\n\n";  
$msg_body .=strip_tags($_POST['message']) . "\n";  
$msg_body .= '--'.$boundary.'--'."\n";  


//======================send n test return value=======================
$success  = mail($_POST['to'],$_POST['sub'],$msg_body,implode("\r\n",$headers)) ;
if($success){ 
    echo "<strong>Your mail sent </strong>";
}    
else     
{
    echo "<strong>Error Sending your mail..please try again later</strong>";    
}  

echo " with following details:<br /><br />";  
echo "<strong>From : </strong><em>" .$_POST['from']."<br />";  
echo "<strong>To&nbsp;&nbsp;: </strong><em>".$_POST['to'].  "<br/>";  
echo "<strong>Subject : </strong><em>".$_POST['sub']."<br />";  
echo "<strong>Message : </strong><em>".$msg_body."<br />";
?> 
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for taking INterst, but what will this do.. this way we are just adding the content -type declaration twice once in header and again in msg_body.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.