0

我目前正在构建一个联系表单,并且想知道是否有可能维护文本区域的换行符,我的电子邮件采用 html 样式,因此它们在我的电子邮件程序中看起来更好。

我阅读了一些关于 nl2br 命令的文章,但我不确定将代码放在哪里,因为我并不是真正的 PHP 怪胎。

这是代码:

<?php
                    $name = $_POST['name'];
                    $email = $_POST['email'];
                    $message = $_POST['message'];
                    $from = 'From: info@epicconcepts.nl'; 
                    $to = 'info@epicconcepts.nl'; 
                    $subject = 'Contact formulier bericht';
                    $human = $_POST['human']; 

                    $headers = "From: info@epicconcepts.nl\r\n";
                    $headers .= "Reply-To: ". strip_tags($_POST['email']) . "\r\n";
                    $headers .= "CC: quincynorbert@gmail.com\r\n";
                    $headers .= 'MIME-Version: 1.0' . "\r\n";
                    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

                    $body = '<!DOCTYPE html>
                                <html>
                                <head>
                                <style type="text/css">
                                    body{background: #231f20;
                                         width:670px;
                                         height:100%;}
                                    #header-mail{background:#110f10;
                                                 width:650px;
                                                 height:70px;}
                                    #content{background:#231f20;
                                             width:630px;
                                             height:100%;
                                             padding: 20px;
                                             word-wrap:break-word;}
                                    .mail-text{font-family:arial;
                                                color:#fff;
                                                font-size:10pt;
                                                line-height:130%;}
                                    .bold{font-family:arial;
                                          color:#fff;
                                          font-size:10pt;
                                          font-weight:bold;}
                                </style>
                                </head>
                                <body>
                                <img src="http://test.epicconcepts.nl/images/bg-mailer.png" alt="Website Change Request" />
                                <div id="content"><p class="bold">'.$name.'<br><br>'.$email.'</p><p class="mail-text">
                                '.$message.'</p>
                                </div>
                                <a href="http://www.epicconcepts.nl">
                                <img src="http://test.epicconcepts.nl/images/mailer-footer.png" alt="Website Change Request" />
                                </a>
                                </body>
                                </html>';

                    if ($_POST['submit']) {
                        if ($name != '' && $email != '') {
                            if ($human == '4') {                 
                                if (mail ($to, $subject, $body, $headers)) { 
                                echo '<p class="correct-message">Your message has been sent!</p>';
                            } else { 
                                echo '<p class="correct-message">Something went wrong, go back and try again!</p>'; 
                            } 
                        } else if ($_POST['submit'] && $human != '4') {
                            echo '<p class="correct-message">You answered the anti-spam question incorrectly!</p>';
                        }
                        } else {
                            echo '<p class="correct-message">You need to fill in all required fields!</p>';
                        }
                    }
                ?>
                <form method="post" action="contact.php">

                    <label>Naam</label>
                    <input name="name" placeholder="Type hier">

                    <label>Email</label>
                    <input name="email" type="email" placeholder="Type hier">

                    <label>Bericht</label>
                    <textarea name="message" placeholder="Type hier"></textarea>

                    <label>Hoeveel is 2+2?</label>
                    <input name="human" placeholder="Type hier">

                    <input id="submit" name="submit" type="submit" value="Verzend">

                </form>
4

1 回答 1

1

你可以像这样用 nl2br 包裹 $body :

if (mail ($to, $subject, nl2br($body), $headers)) { 
于 2013-02-12T20:24:09.050 回答