【问题标题】:Changing php form to submit a styled email (HTML email)更改 php 表单以提交样式电子邮件(HTML 电子邮件)
【发布时间】:2012-01-12 02:04:03
【问题描述】:

当我提交表单时,我会收到纯文本格式的电子邮件,而不是我想要的格式。我希望它至少对正文有颜色并安排电子邮件以使其更整洁。

在contact.php 表单上...这是我目前拥有的:

     $msg  = "You have been contacted by $name with regards to Accommodation. 
They passed verification and their message is as follows." . PHP_EOL . PHP_EOL;
     $msg .= "$comments" . PHP_EOL . PHP_EOL;
     $msg .= "You can contact $name via email, $email or via phone $phone." . 
PHP_EOL . PHP_EOL;
     $msg .= "We want to stay from the $dayin to the $dayout" . PHP_EOL . 
PHP_EOL;

为什么这个表单使用 $msg?我见过其他形式,他们使用 $message。 我可以在上面的表单中添加 CSS 样式吗? 我也看到了:

        $headers = "From: $email" . PHP_EOL;
    $headers .= "Reply-To: $email" . PHP_EOL;
    $headers .= "MIME-Version: 1.0" . PHP_EOL;
    $headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
    $headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;

我需要将“plain”更改为“HTML”吗? 任何建议或示例都会很棒!


编辑:

好的,现在我的标题是:

    $headers = "From: $email" . PHP_EOL;
    $headers .= "Reply-To: $email" . PHP_EOL;
    $headers .= "MIME-Version: 1.0"  . "\r\n" . PHP_EOL;
    $headers .= "Content-type: text/html;charset=iso-8859-1" . "\r\n" . 
 PHP_EOL;
    $headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;

每个标头都需要 PHP_EOL 吗?

我的 $msg 看起来像:

$msg  = '<style type="text/css">
<!--
.style {
font-family: Arial;
font-size: 12px;
color: #4D241E;
}
body {
background-image: url();
background-color: #F1EAE4;
 }
.style1 {font-size: 14px}
-->
</style>
<p>&nbsp;</p>
<table width="420" border="0" align="center" cellpadding="0" cellspacing="5">
<tr>
<td><table width="100%"  border="0" cellpadding="8" cellspacing="0" bgcolor="#E7D3AF" 
class="style">
  <tr>
    <td colspan="2" valign="top"><div align="center"><strong><span 
class="style1">Submission / Enquiry from your website</span><br>

&nbsp;........................................................................
</strong><br>
&nbsp;</div></td>
    </tr>
  <tr>
    <td width="32%" valign="top"><div align="left"><strong>Date Submitted</strong>
 </div></td>
    <td width="68%" valign="top">'. date("F j, Y, g:i a") .'</td>
  </tr>
  <tr>
    <td valign="top"><div align="left"><strong>Name</strong></div></td>
    <td valign="top">'.$_POST['name'].'</td>
  </tr>
  <tr>
    <td valign="top"><div align="left"><strong>Email</strong></div></td>
    <td valign="top">'.$_POST['email'].'</td>
  </tr>
  <tr>
    <td valign="top"><div align="left"><strong>Phone</strong></div></td>
    <td valign="top">'.$_POST['phone'].'</td>
  </tr>
  <tr>
    <td valign="top"><div align="left"><strong>Day in</strong></div></td>
    <td valign="top">'.$_POST['dayin'].'</td>
  </tr>
  <tr>
    <td valign="top"><div align="left"><strong>Day out</strong></div></td>
    <td valign="top">'.$_POST['dayout'].'</td>
  </tr>
  <tr>
    <td valign="top"><div align="left"><strong>Comments</strong></div></td>
    <td valign="top">'.$_POST['comments'].'</td>
  </tr>
</table></td>
</tr>
 </table>
';

编辑: 我的新 HTML 电子邮件设计:

<table width="600" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="12" height="14"></td>
<td width="140"></td>
<td width="432"></td>
<td width="16"></td>
</tr>
<tr>
<td height="71"></td>
<td colspan="2" align="center" valign="middle" bgcolor="#F5F5F5"><span style="font-
family:Arial; font-size:18px; color:#333333;"><strong>Submission from</strong>
</span><br />
<span style="font-family:Arial; font-size:16px; color:#f9930f;">
<em>Your website</em></span></td>
<td></td>
 </tr>
<tr>
<td height="16"></td>
<td></td>
<td></td>
<td></td>
 </tr>
<tr>
<td height="30"></td>
<td valign="middle"><span style="font-family:Arial; font-size:12px; color:#333333;">
<strong>Name:</strong></span></td>
<td valign="middle"><span style="font-family:Arial; font-size:12px; 
color:#333333;">'.$_POST['name'].'</span></td>
<td></td>
</tr>
<tr>
<td height="30"></td>
<td valign="middle"><span style="font-family:Arial; font-size:12px; color:#333333;">
<strong>Email:</strong></span></td>
<td valign="middle"><span style="font-family:Arial; font-size:12px; 
color:#333333;">'.$_POST['email'].'</span></td>
<td></td>
</tr>
<tr>
<td height="30"></td>
<td valign="middle"><span style="font-family:Arial; font-size:12px; color:#333333;">
<strong>Phone:</strong></span></td>
<td valign="middle"><span style="font-family:Arial; font-size:12px; 
color:#333333;">'.$_POST['phone'].'</span></td>
<td></td>
</tr>
 <tr>
<td height="30"></td>
<td valign="middle"><span style="font-family:Arial; font-size:12px; color:#333333;">
<strong>Day in:</strong></span></td>
<td valign="middle"><span style="font-family:Arial; font-size:12px; 
color:#333333;">'.$_POST['dayin'].'</span></td>
<td></td>
</tr>
<tr>
<td height="30"></td>
<td valign="middle"><span style="font-family:Arial; font-size:12px; color:#333333;">
<strong>Day out:</strong></span></td>
<td valign="middle"><span style="font-family:Arial; font-size:12px;
color:#333333;">'.$_POST['dayout'].'</span></td>
<td></td>
 </tr>
<tr>
<td height="30"></td>
<td valign="middle"><span style="font-family:Arial; font-size:12px; color:#333333;">
<strong>Comments:</strong></span></td>
<td>&nbsp;</td>
<td></td>
</tr>
<tr>
<td height="190"></td>
<td colspan="2" valign="top"><span style="font-family:Arial; font-size:12px; 
color:#333333;">'.$_POST['comments'].'</span></td>
<td></td>
</tr>
 <tr>
<td height="20"></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td></td>
</tr>
</table>

现在当我在我的 php 中添加这个表单并提交我的表单时......我收到的提交看起来不像上面的表格布局。

【问题讨论】:

    标签: php html css email


    【解决方案1】:

    您可以使用以下标题。这些将允许您将普通 HTML 放入您的消息中。

    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    
    // More headers
    $headers .= 'From: <example@example.com>' . "\r\n";
    $headers .= 'Cc: example two@example.com' . "\r\n";
    

    【讨论】:

    • 嗨 Spacebeers,感谢您的帮助。好的,我更改了我的标题以允许 HTML,然后我为我的电子邮件输出添加了一个基本的 HTML 布局,但由于某种原因没有颜色或 CSS 不起作用。这是为什么?请在上面的问题中查看我所做的编辑,谢谢。
    • 我可能错了(目前无法对其进行测试),但您最好对 HTML 电子邮件使用内联样式。特别适用于 Outlook。尝试向 td 添加内联样式,看看是否有帮助。这很痛苦,但这就是你的前景。
    • 对于 Outlook,您也会遇到背景图像。
    • 事实上,为了让你以后头疼,这里有一篇关于电子邮件中 ​​CSS 的好文章 - css-tricks.com/133-using-css-in-html-emails-the-real-story 如果你认为跨浏览器测试不好,请等到你完成了跨电子邮件程序测试。 .
    • 内联样式对我来说很好,我不想使用背景图像,可能只是背景颜色。我阅读了 CSSTRICKS LINK,它当然有一些很好的提示。我现在的问题是,当我将设计的 HTML 布局放在我的 php 表单中时(在我的问题中的我的编辑中查看它),当我提交表单时它不会像那样出现,几乎就像它没有使用来自内联样式的 CSS。
    【解决方案2】:

    是的,你将不得不使用

    Content-type:text/html;charset=iso-8859-1\r\n
    


    提供 HTML 和文本格式的电子邮件可能是个好主意,因为并非所有电子邮件应用程序都支持 HTML,而且许多其他应用程序对它的支持很差。

    为此,请改用此标头。边界是电子邮件中的 HTML 和文本版本之间的区别,下面的行会生成一个适合此目的的唯一代码

    $boundary = md5(date('r', time())); 
    $headers .= "\r\nContent-Type: multipart/alternative; boundary=$boundary."\"";
    


    在电子邮件的文本版本发布此代码之前,请注意请求处的“--”。您在整个电子邮件中使用相同的边界。

    --<?php echo $boundary; ?> 
    


    在 HTML 版本之前再次发布相同的代码

    你可以在这里使用一个很好的模板 http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php

    【讨论】:

      猜你喜欢
      • 2018-11-05
      • 1970-01-01
      • 2013-09-16
      • 2016-04-25
      • 2012-03-04
      • 2015-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多