4

Ok, I've asked this question before but I didn't get a clear answer and this time I will also explain it better.

I've got an contact form that when submitted, the submission email received is in plain text which is NOT what I want. I want the submission to have at least some style and to be neater (using my own HTML below). I've tried to get this to work before but with no success, and now need outside assistance.

Here's the process php form:

 <?php if (!isset($_SESSION)) session_start();

 if(!$_POST) exit;

if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");


$address = "email@domain.com";
$bcc = "email@domain.com";

    $twitter_active     = 0;
    $twitter_user       = ""; 
    $consumer_key       = "";
    $consumer_secret    = "";
    $token              = "";
    $secret             = "";


$name    = $_POST['name'];
$email  = $_POST['email'];
$phone  = $_POST['phone'];
$dayin  = $_POST['dayin'];
$dayout = $_POST['dayout'];
$comments = $_POST['comments'];

if (isset($_POST['verify'])) :
    $posted_verify   = $_POST['verify'];
    $posted_verify   = md5($posted_verify);
else :
    $posted_verify = '';
endif;

// Important Variables
$session_verify = $_SESSION['verify'];

if (empty($session_verify)) $session_verify = $_COOKIE['verify'];

$error = '';

    if(trim($name) == '') {
        $error .= '<li>Your name is required.</li>';
    }

    if(trim($email) == '') {
        $error .= '<li>Your e-mail address is required.</li>';
    } elseif(!isEmail($email)) {
        $error .= '<li>You have entered an invalid e-mail address.</li>';
    }

    if(trim($phone) == '') {
        $error .= '<li>Your phone number is required.</li>';
    } elseif(!is_numeric($phone)) {
        $error .= '<li>Your phone number can only contain digits (no 
spaces).</li>';
    }

    if(trim($comments) == '') {
        $error .= '<li>You must enter a message to send.</li>';
    }

    if($session_verify != $posted_verify) {
        $error .= '<li>The verification code you entered is incorrect.
</li>';
    }

    if($error != '') {
        echo '<div class="error_message">Attention! Please correct the 
errors below and try again.';
        echo '<ul class="error_messages">' . $error . '</ul>';
        echo '</div>';

    } else {

    if(get_magic_quotes_gpc()) { $comments = stripslashes($comments); }

     $e_subject = 'Website Enquiry';

     $msg = '<html><body>
     <strong>WEBSITE ENQUIRY FROM:</strong><br>
     www.domainname.com<br>
     -----------------------------------------------------------<br><br>
     <strong>Name: </strong>
     '.$_POST['name'].' <br>
     <strong>Email: </strong>
     '.$_POST['email'].' <br>
     <strong>Contact Number: </strong>
     '.$_POST['phone'].' <br>
     <strong>Day in: </strong>
     '.$_POST['dayin'].' <br>
     <strong>Day out: </strong>
     '.$_POST['dayout'].'<br><br>
     <strong>Notes / Comments: </strong><br>
     '.$_POST['comments'].'
     </body></html>';

    if($twitter_active == 1) {

        $twitter_msg = $name . " - " . $comments . ". You can contact " . 
$name . " via email, " . $email ." or via phone " . $phone . ".";
        twittermessage($twitter_user, $twitter_msg, $consumer_key, 
$consumer_secret, $token, $secret);

    }

    $msg = wordwrap( $msg, 70 );

    $headers = "From: $email\r\nBCC:{$bcc}\r\n" . PHP_EOL;
    $headers .= "Reply-To: $email" . PHP_EOL;
    $headers .= "MIME-Version: 1.0" . PHP_EOL;
    $headers .= "Content-type: text/html; charset=utf-8" . PHP_EOL;
    $headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;

    if(mail($address, $e_subject, $msg, $headers)) {

     echo "<fieldset>";
     echo "<div id='success_page'>";
     echo "<img src='success.png' align='absmiddle' style='padding-right:5px;' 
/><strong>Email Sent Successfully.</strong>";
     echo "</div>";
     echo "</fieldset>";

     } else {

     echo 'ERROR!'; // Dont Edit.

     }

}


function twittermessage($user, $message, $consumer_key, $consumer_secret, $token, 
$secret) { // Twitter Direct Message function, do not edit.

require_once('twitter/EpiCurl.php');
require_once('twitter/EpiOAuth.php');
require_once('twitter/EpiTwitter.php');

$Twitter = new EpiTwitter($consumer_key, $consumer_secret);
$Twitter->setToken($token, $secret);

$direct_message = $Twitter->post_direct_messagesNew( array('user' => $user, 'text' 
    => $message) );
$tweet_info = $direct_message->responseText;

 }
?>

Take note of this code in the form above:

 $msg = '<html><body>
     <strong>WEBSITE ENQUIRY FROM:</strong><br>
     www.domainname.com<br>
     -----------------------------------------------------------<br><br>
     <strong>Name: </strong>
     '.$_POST['name'].' <br>
     <strong>Email: </strong>
     '.$_POST['email'].' <br>
     <strong>Contact Number: </strong>
     '.$_POST['phone'].' <br>
     <strong>Day in: </strong>
     '.$_POST['dayin'].' <br>
     <strong>Day out: </strong>
     '.$_POST['dayout'].'<br><br>
     <strong>Notes / Comments: </strong><br>
     '.$_POST['comments'].'
     </body></html>';

I would like to replace this code above (which produces a plain text email) with this STYLED HTML:

<table width="550" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td height="78" colspan="2" align="center" valign="middle" bgcolor="#F0F5FB"><span 
style="font-family:Georgia, 'Times New Roman', Times, serif; font-size: 24px; 
font-style:italic; color:#0099CC; ">WEBSITE ENQUIRY FROM:</span><br />
  <span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;  
color:#333333">www.domainname.com</span></td>
</tr>
<tr>
<td width="177" height="166" valign="middle"><span style="font-family:Arial, 
Helvetica, sans-serif; font-size:14px; color:#333333; font-weight:bold;">Name:<br />
Country<br />
Contact Number<br />
Email<br />
No. Guests<br />
Day in<br />
Day Out<br />
Comments
</span></td>
<td width="373" valign="middle"><span style="font-family:Arial, Helvetica, sans-serif; 
font-size:14px; color:#333333; font-weight:bold;">'.$_POST['name'].'<br />
'.$_POST['country'].'<br />
'.$_POST['tel'].'<br />
'.$_POST['email'].'<br />
'.$_POST['guests'].'<br />
'.$_POST['day in'].'<br />
'.$_POST['day out'].'<br />
'.$_POST['comments'].'<br />


</span></td>
</tr>
</table>

On a step by step basis, what do need to do to get this to work. I've tried replacing the plain text code with my STYLED html and it doesn't work. What else do I need to do?

Appreciate your responses!

EDIT

Please check here: Here's what the HTML email should look like: http://www.testing123.co.za/styled.jpg ..... Here's how it turns out instead: http://www.testing123.co.za/not-correct.jpg

Reflex84
  • 313
  • 2
  • 6
  • 16
  • you can try using smarty templates – Ajeet Sinha Jan 25 '12 at 11:19
  • Nope, need to use it with this code, thanks. – Reflex84 Jan 25 '12 at 11:20
  • 2
    does it arrive as plain text? are u sure that client mail doesn't strip html tags? – Elen Jan 25 '12 at 11:21
  • You might need to MIME encode it. Can't remember exactly how it's done since it's a long time since I did PHP mailing of HTML. – GordonM Jan 25 '12 at 11:30
  • Its definitely not by email client because my other form uses the STYLED html and it looks perfect on my email client. So I don't think my email client has anything to do with it? Unless I didn't understand correct? – Reflex84 Jan 25 '12 at 12:16
  • Here's what the HTML email should look like: http://www.testing123.co.za/styled.jpg ..... Here's how it turns out instead: http://www.testing123.co.za/not-correct.jpg – Reflex84 Jan 25 '12 at 12:24

3 Answers3

4

Our (working) code:

$text = "";
$text .= "<html>\n";
$text .= "<body style=\"font-family:Arial; \">\n";
$text .= "<b>more text</b>";
. . .
$text .= "</body>\n";
$text .= "</html>\n";

$headers = 'From: ' . $frm . "\r\n";
$headers .= 'To: ' . $mmail_to . "\r\n";
$headers .= 'Bcc: ' . $mmail_bcc . "\r\n";  
$headers .= 'Return-Path: ' . $frm . "\r\n";
$headers .= 'MIME-Version: 1.0' ."\r\n";
$headers .= 'Content-Type: text/HTML; charset=ISO-8859-1' . "\r\n";
$headers .= 'Content-Transfer-Encoding: 8bit'. "\n\r\n";
$headers .= $text . "\r\n";

if (!mail('', $strSubject, '', $headers)) {
. . . 

maybe you need to add the 'Content-Transfer-Encoding' or maybe the $msg should be part of the $headers ???

Atara
  • 3,523
  • 6
  • 37
  • 56
  • Ok, Its now 100%! All I did was replaced my header $headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL; to per your header above $headers .= 'Content-Transfer-Encoding: 8bit'. "\n\r\n" . PHP_EOL; ... and now it comes out perfectly! Thanks dude! – Reflex84 Jan 25 '12 at 12:37
  • What about if I want add subject - $headers .= 'subject: ' . $title . "\r\n"; -which $title is name of the text box so the user can type the email subject. Is that correct ? – Mohammed Moustafa Jan 17 '16 at 04:30
  • 1
    see my latest code for PHP 5.4.43 - http://stackoverflow.com/questions/30887610/error-with-php-mail-multiple-or-malformed-newlines-found-in-additional-header/31671606#31671606 The 2nd parameter to mail() function is the email subject. – Atara Jan 18 '16 at 09:31
1

I've tried replacing the plain text code with my STYLED html and it doesn't work

Please check your 'styled HTML'. I think problem is single quote font-family: 'Times New Roman', maybe you can change with font-family:\'Times News Roman\'.

Zul
  • 3,627
  • 3
  • 21
  • 35
  • Hi, Ok This worked! BUT ... now the email submission (I remember this happening to me before) looks totally different to my STYLED html above. The email has a orange background color and the cell widths are very squashed and narrow. – Reflex84 Jan 25 '12 at 12:14
  • Oh and its still arriving in plain text – Reflex84 Jan 25 '12 at 12:17
  • Here's what the HTML email should look like: http://www.testing123.co.za/styled.jpg ..... Here's how it turns out instead: http://www.testing123.co.za/not-correct.jpg – Reflex84 Jan 25 '12 at 12:24
  • 1
    Hi, Thanks for your help, though Atara mentioned the Content-Transfer-Encoding header which was what corrected everything. See my comment to Atara – Reflex84 Jan 25 '12 at 12:41
-1

use charset=iso-8859-1 in the headers

$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

to send a html email you have to use the above charset, here and also here you can see most of them using the charset

Manigandan Arjunan
  • 2,260
  • 1
  • 25
  • 42