简体   繁体   中英

How to make Form send simple HTML email and not Plain text

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

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 ???

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\\' .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM