duanjianao0592 2018-04-02 04:18
浏览 43
已采纳

如何从HTML表单发送邮件? [重复]

This question already has an answer here:

I made a form and my task is to sending mail to the mail server.I tried with mailto HTML attributes.But failed.Then start with php.It was not working also.So I badly need help.

<form method="post" action="form-to-email.html">
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label for="exampleInputName">Your Name (required)</label>
                                        <input type="text" class="form-control"  name="name" placeholder="Enter Your Name">

                                    </div>
                                    <div class="form-group">
                                        <label for="exampleInputEmail1">Your Email (required)</label>
                                        <input type="Email" class="form-control" name="email" placeholder="Enter Your Email">

                                    </div>
                                    <div class="form-group">
                                        <label for="exampleInputTextarea">Your Message</label>
                                        <textarea class="form-control" placeholder="Enter Your Message" rows="4" name="message"></textarea>
                                    </div>

                                    <button type="submit" value="Send Email" class="btn btn-default">Send</button>

                                    </div>
                                </form>

And this is my php code.

<?php
if(isset($_POST['name'])&&isset($_POST['email'])){
$name = $_POST['name'];
  $visitor_email = $_POST['email'];
  $message = $_POST['message'];
    $email_from = 'dbgreen71@heidianderson.com.au';
    $email_subject = "New Form submission";
    $email_body = "You have received a new message from the user $name.
".    
                            "Here is the message:
 $message".    
$to = "dbgreen71@heidianderson.com.au";
  $headers = "From: $email_from 
";
  $headers .= "Reply-To: $visitor_email 
";
  mail($to,$email_subject,$email_body,$headers);
}
?>

Someone please help me.I really don't find any way to do this task.

</div>
  • 写回答

1条回答 默认 最新

  • duanaigua4033 2018-04-02 04:27
    关注

    Try this and tell if you get any error:

    <?php
        $visitor_email = $_POST['email'];
        $message = $_POST['message'];
        $email_subject = "New Form submission";
        $to = "dbgreen71@heidianderson.com.au";
        $subject = $email_subject;
        $email_from = 'dbgreen71@heidianderson.com.au';
        $header = "From:dbgreen71@heidianderson.com.au 
    ";
        $header .= "MIME-Version: 1.0
    ";
        $header .= "Content-type: text/html
    ";
    
        $retval = mail ($to,$subject,$message,$header);
    
        if( $retval == true ) {
        echo "Message sent successfully...";
        }else {
        echo "Message could not be sent...";
        $errorMessage = error_get_last()['message'];
        echo $errorMessage;
        }
    ?>
    

    Also I have added error handling code which will help you in near future if you get any errors related to mail. Another thing, if you need headers then and then only use it, or else it is of no usage. You can even send emails without headers.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?