如何在laravel中使用ajax发送电子邮件

时间:2016-07-11 13:34:04

标签: php jquery ajax email

我正在尝试使用lajvel中的ajax发送电子邮件以停止重新加载页面发送电子邮件,它停止正确地重新加载页面但它也停止了发送电子邮件的表单,这是我写的ajax代码:

<script type="text/javascript" >
    $(function(){
        $('#errorMsgs').hide();
        $('#sendmail').submit(function(e){
            e.preventDefault();
            var first_name=$('input[name="first_name"]').val();
            var last_name=$('input[name="last_name"]').val();
            var client =$('input[name="client"]').val();
            var phone =$('input[name="phone"]').val();
            var address =$('input[name="address"]').val();
            var city=$('input[name="city"]').val();
            var telephone=$('input[name="telephone"]').val();
            var fax=$('input[name="fax"]').val();
            var service=$('input[name="service"]').val();
            var first=$('input[name="first"]').val();
            var how=$('input[name="how"]').val();

            var data = new FormData();
            data.append('first_name',first_name);
            data.append('last_name',last_name);
            data.append('client',client);
            data.append('phone',phone);
            data.append('address',address);
            data.append('city',city);
            data.append('telephone',telephone);
            data.append('fax',fax);
            data.append('service',service);
            data.append('first',first);
            data.append('how',how);

            $.ajax({
                url:'postIndex',
                type:'POST',
                data:data,
                processData:false,
                success:function(data){alert('Email sent successfully');},
                error:function(data){
                    $('#errorMsgs').show();
                    $('#errorMsgs').html('');

                    var errors=data.responseJSON;
                    $.each(errors,function(k,v){
                        $('#erroMsgs').append(v+"<br />");
                    });
                }
            });
        });

    });
</script>

这就是我的路线的样子:

Route::controller('request','MailController');

这就是我正在使用的形式:

<form class="row" id="sendmail" action="{{action('MailController@postIndex')}}" method="POST">
                        <div class="col-sm-6">
                            <label>
                                First Name: 
                                <span class="required">
                                    *
                                </span>
                            </label>
                            <input class="form-control" name="first_name" type="text" required>
                        </div><!--End col-sm-6-->
                        <div class="col-sm-6">
                            <label>Last Name:
                                <span class="required">
                                    *
                                </span>
                            </label>
                            <input class="form-control" name="last_name" type="text" required>
                        </div><!--End col-sm-6-->
                        <div class="col-sm-12">
                            <label>
                                Are you a new client?
                                <span class="required">
                                    *
                                </span>
                            </label>
                            <select id="discuss" name="client" class="form-control" required>
                                <option selected value="yes , I am a potential new client">Yes, I am a potential new client.
                                    </option>
                                <option value="no , I am a current existing client">No, I am a current existing client.</option>
                                <option value="I'm neither.">I am neither.</option>
                            </select>
                        </div><!--End col-sm-12-->
                        <div class="spacer-25"></div>
                        <div class="col-sm-6">
                            <label>Phone Number:</label>
                            <input class="form-control" name="phone" type="text" required="">
                        </div>
                        <div class="col-sm-6">
                            <label>Email:
                                <span class="required">
                                    *
                                </span>
                            </label>
                            <input class="form-control" name="email" type="email" required>
                        </div>
                        <div class="clearfix"></div>
                        <div class="col-sm-6">
                            <label>Street Address:
                                <span class="required">
                                    *
                                </span>
                            </label>
                            <input class="form-control" name="address" type="text" required>
                        </div>
                        <div class="col-sm-6">
                            <label>City:</label>
                            <input class="form-control" name="city"  type="text" required>
                        </div>
                        <div class="clearfix"></div>
                        <div class="col-sm-6 col-md-6">
                            <label>Telephone
                                <span class="required">
                                    *
                                </span>
                            </label>
                            <input class="form-control"  name="telephone" type="text" required>
                        </div>
                        <div class="col-sm-6">
                            <label>
                                Fax:
                            </label>
                            <input class="form-control" name="fax" type="text" required>
                        </div>
                        <div class="clearfix"></div>
                        <div class="col-sm-6">
                            <label>Service Type:
                                <span class="required">
                                    *
                                </span>
                            </label>
                            <div class="row">
                                <div class="col-sm-4">
                                    <input checked="" name="service" id="residential" type="radio" value="residental service">
                                    <label for="residential">
                                        Residential
                                    </label>
                                </div><!--End col-sm-6-->
                                <div class="col-sm-5">
                                    <input checked="" name="service" id="ecommere" type="radio" value="commercial service">
                                    <label for="ecommere">
                                        ecommere
                                    </label>
                                </div><!--End col-sm-6-->  
                            </div><!--End Row-->
                        </div><!--End col-sm-6--> 
                        <div class="col-sm-6">
                            <label>Is this your first experience with spanah?:
                                <span class="required">
                                    *
                                </span>
                            </label>
                            <div class="row">
                                <div class="col-sm-3">
                                    <input checked="" name="first" id="yes-first" type="radio" value="yes it's my first experience">
                                    <label for="yes-first">
                                        yes
                                    </label>
                                </div><!--End col-sm-6-->
                                <div class="col-sm-3">
                                    <input checked="" name="first" id="no-first" type="radio" value="no it isn't my first experience">
                                    <label for="no-first">
                                        no
                                    </label>
                                </div><!--End col-sm-6-->  
                            </div><!--End Row-->
                        </div><!--End col-sm-6--> 
                        <div class="spacer-25"></div>
                        <div class="col-sm-12">
                            <label>
                                How can spanah help you?
                            </label>
                            <textarea class="form-control" name="how" rows="5" required></textarea>
                        </div><!--End col-sm-12-->
                        <div class="custom-btn-container">
                            <button class="custom-btn" type="submit" name="submit">
                                Send Message
                            </button>
                        </div>
                        <div class="alert alert-success" id="errorMsgs"></div>
                    </form>

那是postIndex函数:

public function postIndex(){


    if(isset($_POST['submit'])){
    $to = "elbiheiry2@gmail.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $new=$_POST['client'];
    $phone=$_POST['phone'];
    $address=$_POST['address'];
    $city=$_POST['city'];
    $telephone=$_POST['telephone'];
    $fax=$_POST['fax'];
    $service=$_POST['service'];
    $first=$_POST['first'];
    $how=$_POST['how'];
    $subject = "Form submission";
    $subject2 = "Copy of your form submission";
    $message = $first_name . " " . $last_name . " wrote the following:". "\n\n" .$from . "\n\n" . $new .            "\n\n".$phone."\n\n".$address."\n\n".$city."\n\n".$telephone."\n\n".$fax."\n\n".$service."\n\n".$first."\n\n".$how;

    $headers="From:".$from;

    mail($to,$subject,$message,$headers);

    return redirect()->action('MailController@getIndex');
    }
    }

所以有人可以帮我解决这个问题吗?

0 个答案:

没有答案