【问题标题】:Need to use bold tags in php需要在php中使用粗体标签
【发布时间】:2012-05-06 22:16:28
【问题描述】:

我已完成此表单,并且在提交表单时工作正常信息通过电子邮件发送到电子邮件 ID,但所有内容在交付时都以纯文本显示我希望当表单信息到达电子邮件 ID 时它应该以粗体显示字段名称。

<?php 
$errors = '';
$myemail = 'abc@email.com';//<-----Put Your email address here.


if(
empty ($_POST['fullname']) ||
empty ($_POST['martialstatus']) ||
empty ($_POST['dateofbirth']) ||
empty ($_POST['email']) ||
empty ($_POST['telephone']) ||
empty ($_POST['cell']) ||
empty ($_POST['graduation']) ||
empty ($_POST['yearatt']) ||
empty ($_POST['department']) ||
empty ($_POST['program']) ||
empty ($_POST['permanentaddress']) ||
empty ($_POST['currentemp']) ||
empty ($_POST['designation']) ||
empty ($_POST['selfemp']) ||
empty ($_POST['officeemail']) ||
empty ($_POST['officetele']) ||
empty ($_POST['portfolio']) ||
empty ($_POST['membership']))

{
    $errors .= "\n Error: all fields are required";
}

$fullname = $_POST['fullname'];
$martialstatus = $_POST['martialstatus'];
$dateofbirth = $_POST['dateofbirth'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$cell = $_POST['cell'];
$graduation = $_POST['graduation'];
$yearatt = $_POST['yearatt'];
$department = $_POST['department'];
$program = $_POST['program'];
$permanentaddress = $_POST['permanentaddress'];
$currentemp = $_POST['currentemp'];
$designation = $_POST['designation'];
$selfemp = $_POST['selfemp'];
$officeemail = $_POST['officeemail'];
$officetele = $_POST['officetele'];
$portfolio = $_POST['portfolio']; 
$membership = $_POST['membership'];



if( empty($errors))
{
    $to = $myemail; 
    $email_subject = "Membership Information: $fullname";
    $email_body = "You have received a new message. ".
    " Here are the details:\n  Personal Information \n\n 
    Name:                      $fullname \n 
    Martial Status:            $martialstatus \n 
    Date of Birth:             $dateofbirth \n 
    Email:                     $email \n 
    Telephone:                 $telephone \n 
    Cell:                      $cell \n 
    Year of Graduation:        $graduation \n 
    Years Attended:             $yearatt \n
    Department:                $department \n 
    Program Attended:          $program \n 
    Permanent Address:         $permanentaddress \n\n 
    Career Information \n\n 
    Currently Employeed with:  $currentemp \n 
    Designation:               $designation \n 
    Self Employeed:            $selfemp \n 
    Office Email:              $officeemail \n 
    Office Telephone:          $officetele \n 
    Portfolio:                 $portfolio \n\n 
    MemberShip \n\n 
    Type of MemberShip:        $membership \n\n "; 

    $headers = "From: $myemail\n"; 
    $headers .= "Reply-To: $email";

    mail($to,$email_subject,$email_body,$headers);
    //redirect to the 'thank you' page
    header('Location: contact-form-thank-you.html');
} 
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
    <title>Contact form handler</title>
</head>

<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>


</body>
</html>

【问题讨论】:

  • 这与 PHP 无关。您正在尝试发送 HTML 电子邮件,而不是“在 PHP 中使用粗体”。
  • 您对 $_POST 的使用是一个直接且广泛的安全漏洞。您必须过滤这些变量的内容。
  • 我在这里看不到任何问题。

标签: php html css


【解决方案1】:

粗体文本可以用 HTML 邮件完成,设置内容类型标题:

$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html;\r\n";

使用&lt;strong&gt;标签:

<strong>Name:</strong>                      $fullname <br />

不要忘记\n 不会在 HTML 电子邮件中显示新行,您需要使用 &lt;br /&gt;

【讨论】:

    【解决方案2】:

    确保以 HTML 格式发送电子邮件并将相应的行更改为

    Email:                     <b>$email</b> \n
    

    【讨论】:

    • 不推荐使用,改用
    • &lt;b&gt;&lt;strong&gt; 不一样。如果我们谈论的语义网站可以通过屏幕阅读器大声朗读,我同意,其中&lt;strong&gt; 部分被大声朗读,而&lt;b&gt; 没有任何区别。不过,在 html 电子邮件中,我更喜欢仍然使用原始的粗体标记,因为我测试过的任何电子邮件软件都会以粗体呈现它。 &lt;strong&gt; 并非如此。
    【解决方案3】:

    请务必添加标题,如 Mail page documentation 中指定的那样

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

    然后使用标记来撰写您的电子邮件

    【讨论】:

      【解决方案4】:

      您需要添加 Content-type 标头,并将 HTML 标记添加到您发送的电子邮件中。所以:

      $headers = "From: $myemail\n"; 
      $headers .= "Reply-To: $email\n";
      $headers .= "Content-type: text/html";
      

      另外,您需要将\n 转换为 HTML 换行符,所以:

      $email_body = nl2br($email_body);
      mail($to,$email_subject,$email_body,$headers);
      

      【讨论】:

        【解决方案5】:

        您的标头必须始终以 `\r\n" 结尾

        这是错误的

        $headers = "From: $myemail\n"; 
        $headers .= "Reply-To: $email";
        

        应该是这样的

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

        您需要在邮件中添加 HTML 标签

        例子

         Name:                      <strong>$fullname</strong><br />
        

        【讨论】:

          【解决方案6】:

          我冒昧地重写了很多次,主要是因为重复太多,而且存在巨大的安全问题。

          人们也可以对此进行一些进一步的改进,但这留给读者作为练习。

          <?php
          $errors = '';
          $myemail = 'abc@email.com';//<-----Put Your email address here.
          
          $fields = array(
              'fullname' => 'Name',
              'martialstatus' => 'Martial Status',
              'dateofbirth' => 'Date of Birth',
              'email' => 'Email',
              'telephone' => 'Telephone',
              'cell' => 'Cell',
              'graduation' => 'Year of Graduation',
              'yearatt' => 'Years Attended',
              'department' => 'Department',
              'program' => 'Program Attended',
              'permanentaddress' => 'Permanent Address',
              'currentemp' => 'Currently Employeed with',
              'designation' => 'Designation',
              'selfemp' => 'Self Employeed',
              'officeemail' => 'Office Email',
              'officetele' => 'Office Telephone',
              'portfolio' => 'Portfolio',
              'membership' => 'Type of MemberShip',
          );
          
          // Don't allow HTML in form.
          foreach ($_POST as $key => $value) {
              $_POST[$key] = strip_tags($value);
          }
          
          $missing_fields = array_diff_assoc($fields, $_POST);
          
          if (count($missing_fields) > 0) {
              $errors .= "\n Error: all fields are required";
          }
          
          if (empty($errors)) {
              $to = $myemail;
              $email_subject = 'Membership Information: ' . $_POST['fullname'];
              $email_body = 'You have received a new message. Here are the details:<br />  Personal Information <br/ ><br/ >';
              foreach (array('fullname', 'martialstatus', 'dateofbirth', 'email', 'telephone', 'cell', 'graduation', 'yearatt', 'department', 'program', 'permanentaddress') as $key) {
                $email_body .= $fields[$key] . ': <strong>' . $_POST[$key] . "</strong><br />";
              }
          
              $email_body .= "Career Information <br /><br />";
          
              foreach (array('currentemp', 'designation', 'selfemp', 'officeemail', 'officetele', 'portfolio') as $key) {
                $email_body .= $fields[$key] . ': <strong>' . $_POST[$key] . "</strong><br />";
              }
          
              $email_body .= "MemberShip <br /><br />";
          
              foreach (array('membership') as $key) {
                $email_body .= $fields[$key] . ': <strong>' . $_POST[$key] . "</strong><br />";
              }
          
              $headers = "From: $myemail\r\n";
              $headers .= "Reply-To: $myemail\r\n";
              $headers .= "MIME-Version: 1.0\r\n";
              $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
          
              mail($to,$email_subject,$email_body,$headers);
              //redirect to the 'thank you' page
              header('Location: contact-form-thank-you.html');
          }
          ?>
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
          <html>
          <head>
              <title>Contact form handler</title>
          </head>
          
          <body>
          <!-- This page is displayed only if there is some error -->
          <?php
          echo nl2br($errors);
          ?>
          
          
          </body>
          </html>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-08-03
            • 2023-03-06
            • 2016-08-10
            • 2020-12-23
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多