如何使表单发送简单的HTML电子邮件而不是纯文本

时间:2012-01-25 11:12:10

标签: php html css forms email

好的,我之前已经问过这个问题,但我没有得到明确的答案,这次我也会更好地解释。

我有一份联系表格,提交后,收到的提交电子邮件是纯文本,这不是我想要的。我希望提交至少有一些风格和整洁(使用我自己的HTML下面)。我之前尝试过这项工作,但没有成功,现在需要外界协助。

这是php表单的过程:

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

 }
?>

请注意以上表格中的此代码:

 $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>';

我想用这个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>

在逐步的基础上,需要做些什么才能使其发挥作用。我已经尝试用我的STYLED html替换纯文本代码,但它不起作用。我还需要做什么?

感谢您的回复!

修改

请在这里查看: 以下是HTML电子邮件的外观:http://www.testing123.co.za/styled.jpg .....以下是它的结果:http://www.testing123.co.za/not-correct.jpg

3 个答案:

答案 0 :(得分:4)

我们的(工作)代码:

$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)) {
. . . 

也许你需要添加'Content-Transfer-Encoding' 或者$ msg应该是$ headers ???

的一部分

答案 1 :(得分:1)

  

我尝试用我的STYLED html替换纯文本代码,但它不起作用

请检查您的'样式HTML'。我认为问题是单引号font-family: 'Times New Roman',也许您可​​以使用font-family:\'Times News Roman\'进行更改。

答案 2 :(得分:-1)

在标题

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

要发送html电子邮件,您必须使用上面的字符集,here以及here您可以使用字符集看到大部分