在发布和发送电子邮件时格式化PHP

时间:2015-05-04 13:43:47

标签: php email post

我有一个代码块,它接受一个表单,用感谢信息将它发布到同一页面然后继续通过电子邮件发送到我的帐户。

截至目前,它在电子邮件结尾显示如下:

hello world., Name: Hello World, , Email: HelloWorld@fake.com

这意味着它会显示消息,名称,然后是电子邮件。

如何制作它以便在电子邮件中显示类似的内容..

Name: Hello World
Email: HelloWorld@fake.com

Message: hello world...

以下是我正在使用的代码:

<?php
    if (isset($_REQUEST['email']))
    //if "email" is filled out, send email
       {
            //send email
            $name = $_REQUEST['name'] ;
            $email = $_REQUEST['email'] ;
            $subject = $_REQUEST["My Portfolio Website"] ;
            $message = $_REQUEST['message'].", Name: ".$name.", Email: ".$email;
            mail("MyEmail@gmail.com", $subject, $message, "From:" . $email);
            echo "<h1>Thank you for contacting me. I will get back to you ASAP!</h1>";
        }else{
        //if "email" is not filled out, display the form
            echo "<form method='post' action='index.php'>

                <input type='text' input name='name' id='name' class='contacttext' placeholder='Your Name' required>
                <input type='text' input name='email' id='email' class='contacttext' placeholder='Your Email Address' required>
                <textarea name='message' id='message' class='contacttext' placeholder='Your Message' cols='55' rows='5' required></textarea>

                <input type='submit' id='submit' class='submitcontacttext' value='Send'>
                </form>";
        }
?>   

另外,目前主题还没有自行设定,原因是什么?

1 个答案:

答案 0 :(得分:0)

尝试以下方法:

// I don't see 'My Portfolio Website' this being passed into your form
// did you mean to do? $subject = 'My Portfolio Website';
$subject = $_REQUEST["My Portfolio Website"];
// add \n to add line breaks.
// $message = $_REQUEST['message'] . "\nName: " . $name . "\nEmail: ".$email;

/**
 * this will make it easier to re-order the elements or add new ones.
 */
$message = implode("\n", [
    'Name: ' . $name,
    'Email: ' . $email,
    'Message ' . $_REQUEST['message'],
]);