为什么我的PHP联系表格HTML发送的电子邮件不是格式化的?

时间:2021-07-22 22:22:46

I am trying to make my own custom contact form. While testing it to make improvements, I found out the emails aren't coming in HTML format. I am not sure why this happens, but I figured out I must be doing something wrong, since my email service provider receives HTML formatted emails from other sources. I think the HTML code inside the message body variable is correct as well, so it must be something related to the headers variable or the way I set it.

我正在尝试制作自己的自定义联系表单。在测试它以进行改进时,我发现电子邮件不是HTML格式。我不确定为什么会这样,但我发现我一定做错了,因为我的电子邮件服务提供商从其他来源接收HTML格式的电子邮件。我认为消息体变量中的HTML代码也是正确的,因此它必须是与headers变量或我设置它的方式相关的东西。

EDIT: Unlike someone suggested, using style tags isn't my problem since I am not using Gmail (that doesn't accept them), but Outlook (that does accept them).

编辑:不像有人建议,使用样式标签不是我的问题,因为我不使用Gmail(不接受它们),而是Outlook(确实接受它们)。

if ($_POST['Submit']) {
   if (empty($_POST['Phone'])) {
      $content = "<html>
          <head></head>
          <body>
            <div class='boldtext'>From:</div>
            $name
            <br/>
            <div class='boldtext'>E-mail:</div>
            $email
            <br/><br/>
            $message
           <style>
             .boldtext {
                font-weight: bold;
          }
          </style>
          </body>
        </html>";
   }

Above is part of my PHP code for the form action. The remaining part is some regex validations for each input and echos as response to those. The submissions are sent to an Outlook email address, which I know is compatible with the style tag in the head or body. What am I doing wrong?

上面是我的表单操作的PHP代码的一部分。剩下的部分是对每个输入和回声的一些正则表达式验证作为对它们的响应。提交的内容将发送到Outlook电子邮件地址,我知道该地址与头部或正文中的样式标记兼容。我究竟做错了什么?

Sample of email literally received by client:

客户字面上收到的电子邮件样本:

<html>
<head></head>
<body>
  <div class='boldtext'>From:</div>
  John Doe
  <br/>
  <div class='boldtext'>E-mail:</div>
  email@email.com
  <br/><br/>
  This is a message.
<style>
.boldtext {
font-weight: bold;
}
</style>
</body>
</html>

2 个解决方案

#1


1  

I found out the reason the emails sent were not HTML formatted. It was not HTML or CSS related, The error was in the mail(); function:

我发现发送的电子邮件不是HTML格式的原因。它不是HTML或CSS相关的,错误发生在mail();功能:

The parameter $from interfered with $headers and did not let the HTML format work.

参数$ from干扰了$ headers并且没有让HTML格式工作。

mail($to, $subject, $message, $from, $headers);

mail($ to,$ subject,$ message,$ from,$ headers);

mail($to, $subject, $message, $headers);

邮件($ to,$ subject,$ message,$ headers);

Everything works correctly now and the CSS structure depends on the email client compatibility.

现在一切正常,CSS结构取决于电子邮件客户端的兼容性。

#2


0  

To best of my knowledge, you cannot add styles in a email like you have provided, but you can provide inline styles only to add styles in your emails. Do something like this example code,

据我所知,您不能像您提供的那样在电子邮件中添加样式,但您只能在电子邮件中添加样式。做类似这个示例代码的事情,

<div style="font-weight:bold;">From:</div>
$name
<br/>
<div style="font-weight:bold;">E-mail:</div>
$email
<br/><br/>
<p style="font-weight:100;">$message</p>

edit: your code is this;

编辑:你的代码是这样的;

 if ($_POST['Submit']) {
   if (empty($_POST['Phone'])) {
      $content = "<html>
          <head></head>
          <body>
            <div class='boldtext'>From:</div>
            $name
            <br/>
            <div class='boldtext'>E-mail:</div>
            $email
            <br/><br/>
            $message
           <style>
             .boldtext {
                font-weight: bold;
          }
          </style>
          </body>
        </html>";
   }
}

you need to do like this,

你需要这样做,

 if ($_POST['Submit']) {
   if (empty($_POST['Phone'])) {
      $content = '<html>
          <head></head>
          <body>
            <div style="font-weight:bold;">From:</div>
            '.$name.'
            <br/>
            <div style="font-weight:bold;">E-mail:</div>
            '.$email.'
            <br/><br/>
            <p style="font-weight:100;">'.$message.'</p>
          </body>
        </html>';
   }
}

#1


1  

I found out the reason the emails sent were not HTML formatted. It was not HTML or CSS related, The error was in the mail(); function:

我发现发送的电子邮件不是HTML格式的原因。它不是HTML或CSS相关的,错误发生在mail();功能:

The parameter $from interfered with $headers and did not let the HTML format work.

参数$ from干扰了$ headers并且没有让HTML格式工作。

mail($to, $subject, $message, $from, $headers);

mail($ to,$ subject,$ message,$ from,$ headers);

mail($to, $subject, $message, $headers);

邮件($ to,$ subject,$ message,$ headers);

Everything works correctly now and the CSS structure depends on the email client compatibility.

现在一切正常,CSS结构取决于电子邮件客户端的兼容性。

#2


0  

To best of my knowledge, you cannot add styles in a email like you have provided, but you can provide inline styles only to add styles in your emails. Do something like this example code,

据我所知,您不能像您提供的那样在电子邮件中添加样式,但您只能在电子邮件中添加样式。做类似这个示例代码的事情,

<div style="font-weight:bold;">From:</div>
$name
<br/>
<div style="font-weight:bold;">E-mail:</div>
$email
<br/><br/>
<p style="font-weight:100;">$message</p>

edit: your code is this;

编辑:你的代码是这样的;

 if ($_POST['Submit']) {
   if (empty($_POST['Phone'])) {
      $content = "<html>
          <head></head>
          <body>
            <div class='boldtext'>From:</div>
            $name
            <br/>
            <div class='boldtext'>E-mail:</div>
            $email
            <br/><br/>
            $message
           <style>
             .boldtext {
                font-weight: bold;
          }
          </style>
          </body>
        </html>";
   }
}

you need to do like this,

你需要这样做,

 if ($_POST['Submit']) {
   if (empty($_POST['Phone'])) {
      $content = '<html>
          <head></head>
          <body>
            <div style="font-weight:bold;">From:</div>
            '.$name.'
            <br/>
            <div style="font-weight:bold;">E-mail:</div>
            '.$email.'
            <br/><br/>
            <p style="font-weight:100;">'.$message.'</p>
          </body>
        </html>';
   }
}