使用Mail.php / mime.php的PHP电子邮件表单系统

时间:2018-05-31 21:36:24

标签: php html

我正在尝试为我的管理员创建一个表单,以便他们能够在我的网站上的管理面板中发送电子邮件。

我使用PHP mail.php和mime.php发送电子邮件。我目前在下面有一个基本的PHP脚本和HTML。我目前使用<textarea>作为邮件部分,但是因为Mail.php/mine.php需要HTML,每当管理员进入新行时,如何添加<br>代码?

PHP:

$SQL = $odb -> prepare("SELECT `name` FROM `staff_names` WHERE `username` = :username");
    $SQL -> execute(array(':username' => $_SESSION['username']));
    $name = $SQL -> fetchColumn(0);

if (isset($_POST['send-email'])){

        if ($user -> isAdmin($odb)){

           include('Mail.php');
           include('Mail/mime.php');

           // Constructing the email
           $sender = "$name <".strtolower($_SESSION['username'].")@example.com>";// Your name and email address
            $recipient = strip_tags(trim($_POST['email'])); // The Recipients name and email address
            $subject = strip_tags(trim($_POST['subject']));// Subject for the email
            $text = $_POST['message'];// Text version of the email
            $html = $_POST['message'];// HTML version of the email
            $crlf = "\r\n";
            $headers = array('From' => $sender, 'Return-Path' => $sender, 'Subject' => $subject);

            // Creating the Mime message
            $mime = new Mail_mime($crlf);

            // Setting the body of the email
            $mime->setTXTBody($text);
            $mime->setHTMLBody($html);

            $body = $mime->get();
            $headers = $mime->headers($headers);

            // Sending the email
            $mail =& Mail::factory('mail');
            $mail->send($recipient, $headers, $body);

            $notify = success("Email sent to {$_POST['email']} successfully.");
        }
    }       

HTML:

<form class="form-horizontal push-10-t" method="post">
                                <div class="form-group row">
                                    <div class="col-sm-12">
                                        <div class="form-material">
                                            <label for="email">Email</label>
                                            <input class="form-control" type="email" id="email" name="email" placeholder="Enter the recipient email">

                                        </div>
                                    </div>
                                </div> 
                                <div class="form-group row">
                                    <div class="col-sm-12">
                                        <div class="form-material">
                                            <label for="subject">Subject</label>
                                            <input class="form-control" type="text" id="subject" name="subject" placeholder="Enter subject here">

                                        </div>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <div class="col-sm-12">
                                        <div class="form-material">
                                            <label for="message">Message</label>
                                            <textarea class="form-control" type="textarea" rows="6" id="message" placeholder="Please type your message here" name="message"></textarea>
                                        </div>
                                    </div>
                                </div>  
                                <div class="form-group row">
                                    <div class="col-sm-9">
                                        <button name="send-email" value="do" class="btn btn-sm btn-primary" type="submit">Send</button>
                                    </div>
                                </div>
                            </form>

1 个答案:

答案 0 :(得分:0)

有一个PHP函数可以将换行符转换为<br>:查看nl2br()