简体   繁体   English

使用php发送电子邮件

[英]Sending an email using php

Given a standard html form such as the one below; 给定标准的html格式,例如以下格式;

<form method="post" action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>>
                            <div class="row half">
                                <div class="6u"><input type="text" placeholder="Name" name="name" /></div>
                                <div class="6u"><input type="text" placeholder="Email" name="email" /></div>
                            </div>
                            <div class="row half">
                                <div class="12u"><textarea name="message" placeholder="Message" name="message"></textarea></div>
                            </div>
                            <div class="row">
                                <div class="12u">
                                    <ul class="actions">
                                        <li><input type="submit" class="button" value="Send Message" name="submit" /></li>
                                        <li><input type="reset" class="button alt" value="Clear Form" name="clear"/></li>
                                    </ul>
                                </div>
                            </div>
                        </form>

And the following php 和以下PHP

if(isset($_POST['submit'])){
    $to = "enquiries@appcloudkent.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $name = $_POST['name'];
    $subject = "Website Enquiry";
    $message = $name . " wrote the following:" . "\n\n" . $_POST['message'];
    $headers = "From:" . $from;
    $headers2 = "From:" . $to;

    if(isValidString($from) && isValidString($name) && isValidString($message)){
        mail($to,$subject,$message,$headers);
    }
}

It is possible to send an email, however, this approach causes a couple of problems. 可以发送电子邮件,但是,这种方法会导致很多问题。 Because my form is at the bottom of the page, when send is clicked the page is redirected back and the focus goes back to the top of the page. 因为我的表单位于页面的底部,所以单击“发送”后,页面将被重定向回,并且焦点将返回页面的顶部。

What is the correct way to provide adequate user feedback to let the user know the email was sent, is it possible to navigate back to the page and automatically scroll to the bottom - allowing me to change the send button to green or something? 提供足够的用户反馈以使用户知道电子邮件已发送的正确方法是什么?是否可以导航回页面并自动滚动至底部-允许我将发送按钮更改为绿色?

Failing that, is there a better approach to doing this? 如果不这样做,是否有更好的方法来做到这一点?

Thanks 谢谢

Add an anchor link before your form. 在表单之前添加锚链接。 <a id="anchorName"></a>

Post your form to the anchor. 将您的表格发布到锚点。

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>#anchorName">

You could submit the form using an XMLHttpRequest, and cancel the default action of pressing submit. 您可以使用XMLHttpRequest提交表单,并取消按Submit的默认操作。 jQuery's .post() and event.preventDefault() methods are particularly good at that. jQuery的.post()和event.preventDefault()方法尤其擅长于此。

The form action should point to the page itself (like it does in your example) so you can display error messages from validation (like you don't). 表单动作应该指向页面本身(就像您的示例中那样),因此您可以显示来自验证的错误消息(就像您没有那样)。

If the form is not at the top of your page you can add an anchor: 如果表单不在页面顶部,则可以添加锚点:

<h2><a name="theForm">The form</a></h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>#theForm">

If the form is processed properly (=mail sent), it is good practice to redirect to another page, showing a success message. 如果正确处理了表单(=已发送邮件),则好的做法是重定向到另一个页面,显示成功消息。 So pressing F5 doesn't submit the form again (and send another mail). 因此,按F5不会再次提交表单(并发送另一封邮件)。

if (mail($to,$subject,$message,$headers)) {
    // redirect to page showing success message and exit script
} else {
    // redirect to page showing error message and exit script
}

I usually redirect to the same page again, but attach an param ( $_SERVER["PHP_SELF"].'?mail_success=1' ) and then inside the template I decide whether to show the success message, error message or the form: 我通常会再次重定向到同一页面,但是要附加一个参数( $_SERVER["PHP_SELF"].'?mail_success=1' ),然后在模板中决定是否显示成功消息,错误消息或以下形式:

if (isset($_REQUEST['mail_success'])) {
    // show success message
} elseif (isset($_REQUEST['mail_error'])) {
    // show error message, technical issues, try again bla bla
} else {
    // show the form (including validation errors if necessary)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM