【问题标题】:HTML PHP FormattingHTML PHP 格式化
【发布时间】:2014-09-14 05:21:34
【问题描述】:

我想格式化从 php webform 发送的电子邮件

此刻它们看起来像这样


姓名:贾斯汀 B

电子邮件:example@mail.com

电话:0123456789

IP:(发件人ip)

评论:你好


我希望文本更大,但如果我将 <h2> 之类的内容放在 $email_message 部分,它只会输出为文本而不是 html。

网络上的文章似乎都没有帮助/工作

<?php
if(isset($_POST['email'])) {
     
 
    $email_to = "example@mail.com";
   
         
    function died($error) {
        // your error code can go here
   
        echo "Please fix errors below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please fix these errors and resubmit form.<br /><br />";
        die();
    }
     
    // validation expected data exists
    if(!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['email']) ||
                !isset($_POST['subject'])||
                !isset($_POST['phone'])||
        !isset($_POST['comments']))
               
               
                {
        died('We are sorry, but there appears to be a problem with the form you submitted.');      
    }
     
    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // required
        $phone = $_POST['phone']; // required
        $subject = $_POST['subject'] ;
    $comments = $_POST['comments']; // required
   
       
    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
   
     
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }
     $ip = $_SERVER['REMOTE_ADDR'];
 
    $email_message .= "Name: ".clean_string($first_name);
    $email_message .= " ".clean_string($last_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
        $email_message .= "Phone: ".clean_string($phone)."\n";
        $email_message .= "IP: ".clean_string($ip)."\n";
        $email_message .= "\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";
       
 
     
// create email headers
$headers .= "Content-Type: text/html";
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $subject, $email_message, $headers);  
 
function IsInjected($str)
{
    $injections = array('(\n+)',
           '(\r+)',
           '(\t+)',
           '(%0A+)',
           '(%0D+)',
           '(%08+)',
           '(%09+)'
           );
               
    $inject = join('|', $injections);
    $inject = "/$inject/i";
     
    if(preg_match($inject,$str))
    {
      return true;
    }
    else
    {
      return false;
    }
}
 
if(IsInjected($visitor_email))
{
    echo "Bad email value!";
    exit;
}
 
 
 
 
function spamcheck($field) {
  // Sanitize e-mail address
  $field=filter_var($field, FILTER_SANITIZE_EMAIL);
  // Validate e-mail address
  if(filter_var($field, FILTER_VALIDATE_EMAIL)) {
    return TRUE;
  } else {
    return FALSE;
  }
}
 
 
 
?>
 
 
<!-- place your own success html below -->
 
<head>
<meta charset="utf-8">
<!--Declares that this is a HTML document!-->
<title>Piccadilly CFS</title>
<link rel="shortcut icon" href="../images/favico.ico" />
<link rel="stylesheet" type="text/css" href="../css/styles.css">
</head>
<body>
<div id='content'>
<IMG class="header" src="../images/header.png" alt="site header">
</div>
       
 
<nav class="nav">
<hr class="hr"/>
<p class="navtext"><a href="../index.html">Home</a>
&nbsp; <a href="about_us.html">About Us</a>
&nbsp; <a href="../appliances.html">Appliances</a>
&nbsp; <a href="../photos.html">Photos</a>
&nbsp; <a href="../news.html">News</a>
&nbsp; <a href="../contact_us.html">Contact Us</a>
</p>
<hr class="hr"/>
</nav>
<br><br>
<p class= "pagetitle">Form Sent!</p>
       
<p class="ptext4">
<section class="section">
Thank you for contacting us. We will be in touch with you very soon.
</section>
 <div class="hr1">
<hr class="hr1">
</div>
 
<footer>
    <div class="footer">
    <p class="ftext">
    <a href="credits.html">Credits </a> <br>
   &copy; Copyright Justin Bussell 2014
    </p>
</div>
<?php
}
die();
?>

【问题讨论】:

  • 请不要破坏您的帖子。通过在 Stack Exchange 网络上发帖,您已在 CC BY-SA 4.0 license 下授予 Stack Exchange 分发该内容的不可撤销的权利(即无论您未来的选择如何)。根据 Stack Exchange 政策,帖子的非破坏版本是分发的版本,因此,任何破坏行为都将被撤销。如果您想了解更多关于删除帖子的信息,请参阅:How does deleting work?

标签: php forms email


【解决方案1】:

您正在覆盖标题:

$headers .= "Content-Type: text/html";
$headers = 'From: '.$email_from."\r\n".          <--- discards content type
           'Reply-To: '.$email_from."\r\n" .     
           'X-Mailer: PHP/' . phpversion();

试试:

$headers = "Content-Type: text/html"."\r\n";
$headers .= 'From: '.$email_from."\r\n".
            'Reply-To: '.$email_from."\r\n" .
            'X-Mailer: PHP/' . phpversion();

【讨论】:

    【解决方案2】:

    尝试在 email_message 周围使用 css 样式作为测试,看看是否有效。我很确定这会奏效。如果你把消息放在一边,它不会起作用。

    同时尝试在标题部分告诉 html 消息它是 html 消息。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-06
      • 1970-01-01
      • 1970-01-01
      • 2012-02-18
      • 2011-12-09
      • 1970-01-01
      相关资源
      最近更新 更多