【问题标题】:html tags displayed after mail sent发送邮件后显示的html标签
【发布时间】:2018-10-29 10:27:31
【问题描述】:

美好的一天,我在联系页面上遇到了问题,当我填写详细信息并提交表单时,它会被处理并将捕获的数据发送到指定的电子邮件,但是当页面加载时它会显示页面html标签。

This is what shows when the form has submitted

在电子邮件方面,我收到的电子邮件不是 HTML 样式的,而是正常的

This is what is received as email

下面是我处理表单数据并发送电子邮件的 php 脚本

<?php

include 'inc/config.php';

$msg = "";
  
  if(isset($_POST['submit']))
  {

  	$name = addslashes($_POST['name']);
  	$email = addslashes($_POST['email']);
  	$subject = addslashes($_POST['subject']);
  	$message = addslashes($_POST['message']);
	  
	  if($name != "" && $email != "" && $subject != "" && $message != "")
	  {

	  		require 'inc/mailer/PHPMailerAutoload.php';
	  		header('Content-Type: application/json');
	  		
	  		$mail = new PHPMailer;
	  		
	  		$mail->setFrom('no-reply@kazforextraders.com', 'Kaz Forex Traders');
	  		
	  		$mail->addAddress($email);   
	  		
	  		$mail->addReplyTo('no-reply@kazforextraders.com', 'No Reply');
	  		
	  		$mail->isHTML(true);   
	  		
	  		$mail->Subject = $subject;
	  		
	  		$mail->Body    = file_get_contents('inc/contact.php');
	  		
	  		$mail->Body    = str_replace('$name', $name, $mail->Body);
	  		
	  		$mail->Body    = str_replace('$email', $email, $mail->Body);
	  		
	  		$mail->Body    = str_replace('$subject', $subject, $mail->Body);
	  		
	  		$mail->Body    = str_replace('$message', $message, $mail->Body);
	  		
	  		if(!$mail->send()) {
	  		    
	  		    $msg = 'Something Went Wrong. Plese Try Again! <br>Mailer Error: '.$mail->ErrorInfo.'';
	  		    
	  		} else {
	  		    
	  		    $msg = 'Message Sent Successfully!';
	  		    
	  		}	  	

	  } 

	  else

	  {

	  	$msg = 'Please Fill out all fields!';

	  }

	}

?>

下面是“inc/contact.php”文件。它是应该发送的文件(样式为 HTML),但我以简单文本的形式接收它。

<!DOCTYPE html>
<!--[if IE 8 ]><html class="no-js oldie ie8" lang="en"> <![endif]-->
<!--[if IE 9 ]><html class="no-js oldie ie9" lang="en"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--><html class="no-js" lang="en"> <!--<![endif]-->
<head>

   <!--- basic page needs
   ================================================== -->
   <meta charset="utf-8">
	<title>Kaz Forex Traders</title>
	<meta name="author" content="Eminential Designs">

   <!-- mobile specific metas
   ================================================== -->
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

 	<!-- CSS
   ================================================== -->
   <link rel="stylesheet" href="https://www.kazforextraders.com/css/base.css"> 
   <link rel="stylesheet" href="https://www.kazforextraders.com/css/vendor.css"> 
   <link rel="stylesheet" href="https://www.kazforextraders.com/css/main.css">    

   <!-- script
   ================================================== -->
	<script src="https://www.kazforextraders.com/js/modernizr.js"></script>
	<script src="https://www.kazforextraders.com/js/pace.min.js"></script>
    <style>
	.section-intro .with-bottom-line::after {
    display: block;
    background-color: white;
    width: 120px;
    height: 3px;
    content: '\0020';
    position: absolute;
    left: 50%;
    bottom: 0;
    margin-left: -60px;
	}
	.section-intro .with-bottom-line:hover::after {
    display: block;
    background-color: white;
    width: 150px;
    height: 3px;
    content: '\0020';
    position: absolute;
    left: 50%;
    bottom: 0;
    margin-left: -70px;
    -webkit-transition: all 0.5s ease-in-out;
    -moz-transition: all .5s ease-in-out;
    -ms-transition: all .5s ease-in-out;
    -o-transition: all .5s ease-in-out;
    transition: all .5s ease-in-out;
	}
	</style>

   <!-- favicons
	================================================== -->
	<link rel="icon" type="image/png" href="https://www.kazforextraders.com/favicon.png">

</head>

<body id="top">

	<!-- main content wrap
   ================================================== -->
   <div id="main-content-wrap">


		<!-- main content wrap
   	================================================== -->
   	<section id="intro">

		   <!-- <div class="shadow-overlay"></div> -->
		   
		   <div class="row intro-content">
		   	<div class="col-twelve">

		   		<h3 class="animate-intro" id="message"></h3>
			  		
					<div class="col-1-4 tab-full mob-full">
                    <p></p>
                    </div>
                    <div class="col-1-2 tab-full mob-full" style="padding-bottom: 13.5rem">	
                    	<img src="https://www.kazforextraders.com/images/logo.png" alt="" class="animate-intro" style="max-width: 250px;">
                    </div>	

			  	</div> <!-- /twelve --> 		   			
		   </div> <!-- /row -->   

		</section> <!-- /intro -->
		<section id="about">

			<div class="row section-intro group animate-this">	
	   		<div class="col-twelve with-bottom-line">

	   			<h2 class="">New Message</h2>

	   		</div>   

	   			<p class="lead" style="text-align: left;">From: $name<br></p>	
	   			<p class="lead" style="text-align: left">Email: $email</p>
	   			<p class="lead" style="text-align: left">Subject: $subject</p>
	   			<p class="lead" style="text-align: left">Message: $message</p>			
	   			<p class="lead" style="text-align: left">This is an automated message, do not reply to it.</p>		
	   			<p class="lead" style="text-align: left">Regards,<br>Kaz Forex Traders</p>	

	   	</div>
			
		</section>	
   
   </div> <!-- /main-content-wrap -->


   <!-- footer
	================================================== -->
	<footer id="main-footer">
	   	
	   <div class="footer-bottom" style="padding-top: 3rem "> 

	   	<div class="copyright">
		     	<span>© Copyright Kaz Forex Traders 2018.</span> 
		     	<span>Developed by <a href="#">Eminential Designs</a></span>	         	
		   </div>  		
   	</div>
	   	
   </footer>   

   <!-- Java Script
   ================================================== --> 
   <script src="https://www.kazforextraders.com/js/jquery-2.1.3.min.js"></script>
   <script src="https://www.kazforextraders.com/js/plugins.js"></script>
   <script src="https://www.kazforextraders.com/js/main.js"></script>

</body>

</html>

请让我知道我能做些什么来解决这个问题,如果有任何帮助,将不胜感激。

【问题讨论】:

    标签: php html css email phpmailer


    【解决方案1】:

    对于收到的邮件样式的问题,包括gmail在内的大多数邮件客户端,我相信不会关注&lt;head&gt;部分的css链接,他们更喜欢邮件的内联样式。同样,JavaScript 将被剥离,因此您不能依赖 JS 来设置或格式化您的 html。一般来说,当谈到以 HTML 格式发送电子邮件时,支持它的技术就像现代网络浏览器所做的那样,根本不存在。

    尝试查看this Q&A 中的一些提示和技巧,以获取一些有助于完成所需设计的建议。

    为什么当表单提交时你会看到你的页面是 HTML,这通常是因为 Content-Type 标头设置了浏览器应该如何解释这个响应。尝试检查 chrome 中调试工具中的网络选项卡,并在提交表单后查看请求以查看服务器作为响应标头返回的内容。另外,我在您的 php 脚本中看到您将标头 Content-Type 设置为 json,如果此 php 脚本正在设置该标头,那么在同一个请求中包含您的 HTML,它可能是罪魁祸首。

    Here 是一个链接,它非常详细地解释了网络选项卡以及如何使用它。此外,请检查设置以保留日志,使其在页面刷新或表单提交之间持续存在。

    【讨论】:

      猜你喜欢
      • 2016-07-16
      • 2020-08-29
      • 2020-02-20
      • 2020-06-12
      • 2018-11-10
      • 2016-04-05
      • 2019-03-22
      • 2012-01-06
      • 2020-04-03
      相关资源
      最近更新 更多