【问题标题】:Adding HTML into email body from PHP mailer script [duplicate]从 PHP 邮件程序脚本将 HTML 添加到电子邮件正文中[重复]
【发布时间】:2017-09-08 04:31:20
【问题描述】:

我正在尝试使用我在以下位置找到的现有 AJAX PHP 表单邮件程序找到一种在电子邮件正文中包含 HTML 代码的方法:http://blog.teamtreehouse.com/create-ajax-contact-form

当我将自己的任何 HTML 编码添加到电子邮件正文中时,它只会显示标签。

这是来自http://blog.teamtreehouse.com/create-ajax-contact-form的原始编码

<?php
// My modifications to mailer script from:
// http://blog.teamtreehouse.com/create-ajax-contact-form
// Added input sanitizing to prevent injection

// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get the form fields and remove whitespace.
    $name = strip_tags(trim($_POST["name"]));
            $name = str_replace(array("\r","\n"),array(" "," "),$name);
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
    $message = trim($_POST["message"]);

    // Check that data was sent to the mailer.
    if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        // Set a 400 (bad request) response code and exit.
        http_response_code(400);
        echo "Oops! There was a problem with your submission. Please complete the form and try again.";
        exit;
    }

    // Set the recipient email address.
    // FIXME: Update this to your desired email address.
    $recipient = "support@minutemenapparel.com";

    // Set the email subject.
    $subject = "New contact from $name";

    // Build the email content.
    $email_content = "Name: $name\n";
    $email_content .= "Email: $email\n\n";
    $email_content .= "Message:\n$message\n";

    // Build the email headers.
    $email_headers = "From: $name <$email>";

    // Send the email.
    if (mail($recipient, $subject, $email_content, $email_headers)) {
        // Set a 200 (okay) response code.
        http_response_code(200);
        echo "Thank You! Your message has been sent.";
    } else {
        // Set a 500 (internal server error) response code.
        http_response_code(500);
        echo "Oops! Something went wrong and we couldn't send your message.";
    }

} else {
    // Not a POST request, set a 403 (forbidden) response code.
    http_response_code(403);
    echo "There was a problem with your submission, please try again.";
}

?>

【问题讨论】:

    标签: php html css ajax email


    【解决方案1】:

    修改标题

    $headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
    $headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
    $headers .= "CC: susan@example.com\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
    

    然后在您的消息中,包含普通的 HTML 标记

    <html>
        <body>
            Message
        </body>
    </html>
    

    【讨论】:

      【解决方案2】:

      您可以尝试使用以下示例,然后处理您的脚本。我发布的那个是我在项目中使用的。

          $to  = 'aidan@example.com' . ', '; // note the comma
          $to .= 'wez@example.com';
      
          // subject
          $subject = 'Adding html to php mail sender';
      
         // message
         $message = '
        <html>
        <head>
        <title>html code with php</title>
        </head>
        <body>
        <p>Here are the things to do</p>
        <table>
        <tr>
        <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
        </tr>
        <tr>
        <td>xyz</td><td>3rd</td><td>September</td><td>2017</td>
        </tr>
        <tr>
        <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
        </tr>
        </table>
        </body>
        </html>';
      
       // To send HTML mail, the Content-type header must be set
      $headers  = 'MIME-Version: 1.0' . "\r\n";
      $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
      
      // Additional headers
      $headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . 
      "\r\n";
      $headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
      $headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
      
      // Mail it
      mail($to, $subject, $message, $headers);
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-04
      • 1970-01-01
      • 1970-01-01
      • 2016-06-10
      • 1970-01-01
      • 2014-04-24
      • 1970-01-01
      • 2013-07-30
      相关资源
      最近更新 更多