dongyan9950 2015-12-07 07:09
浏览 66
已采纳

表格在laravel 5中发送三次邮件

I have this weird issue that I have never came across before ever since I started learning laravel.

I have a contact form: contact.blade.php

{!! Form::open(['url' => '/contact-form', 'id' => 'formContact']) !!}
    <div class="col-md-12 col-sm-12 form-group">
        {!! Form::label('full_name', 'Full Name:', ['class' => 'fw_400']) !!}
        {!! Form::text('full_name', null, ['class' => 'form-control input-sm']) !!}
    </div>

    <div class="col-md-6 col-sm-6 form-group">
        {!! Form::label('email', 'Email:', ['class' => 'fw_400']) !!}
        {!! Form::text('email', null, ['class' => 'form-control input-sm']) !!}
    </div>

    <div class="col-md-6 col-sm-6 form-group">
        {!! Form::label('contact', 'Contact Number:', ['class' => 'fw_400']) !!}
        {!! Form::text('contact', null, ['class' => 'form-control input-sm']) !!}
    </div>


    <div class="col-md-12 col-sm-12 form-group">
        {!! Form::label('contact_message', 'Contact Message:', ['class' => 'fw_400']) !!}
        {!! Form::textarea('contact_message', null, ['class' => 'form-control input-sm', 'rows' => 5]) !!}
    </div>

    <div class="col-md-12 col-sm-12 form-group">
        {!! Form::submit('Submit', ['class' => 'btn btn-primary btn-block btnContactOnContactPage']) !!}
    </div>
{!! Form::close() !!}

When I click on the submit button, AJAX call is made.. Here's the ajax handler:

(function() {
    $('#formContact').submit(function(e) {

        $('.btnContactOnContactPage').prop('disabled', true);

        var inputData = $('#formContact').serialize();

        $.ajax({
            url: $('#formContact').attr('action'),
            type: 'POST',
            data: inputData,
            success: function(m) {
                $('.btnContactOnContactPage').prop('disabled', false);

                if (m.status === 'success') {
                    toastr.success(m.msg);

                    setTimeout(function() {
                        window.location.reload();
                    }, 3000)
                }
            },
            error: function(e) {
                if (e.status === 422) {

                    $('.btnContactOnContactPage').prop('disabled', false);

                    var errors = e.responseJSON;

                    var errorsHtml = '<div class="alert alert-danger"><ul>';
                    errorsHtml += '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>';
                    $.each(errors, function(key, value) {
                        errorsHtml += '<li class="text-danger">' + value[0] + '</li>';
                    });
                    errorsHtml += '</ul></div>';
                    $('.errors').html(errorsHtml);
                }
            }
        });

        return false;
    });
})();

And the controller method:

public function postContact(Request $request)
{
    $this->validate($request, [
        'full_name'       => 'required',
        'email'           => 'required|email',
        'contact'         => 'required|regex:/^[0-9+\-]+$/',
        'contact_message' => 'required'
    ]);

    if ($request->ajax()) {
        $prevContactCode = DB::table('contact_messages')->latest()->limit(1)->pluck('cont_code');
        if ($prevContactCode == "" || empty($prevContactCode)) {
            $request['cont_code'] = 'CON-000001';
        }
        else {
            $request['cont_code'] = ++$prevContactCode;
        }

        $contactMessage = ContactMessage::create($request->all());

        $dataToMail = [
            'cont_code'       => $contactMessage->cont_code,
            'full_name'       => $contactMessage->full_name,
            'email'           => $contactMessage->email,
            'contact'         => $contactMessage->contact,
            'contact_message' => $contactMessage->contact_message
        ];

        $mail = Mail::send('emails.contact', $dataToMail, function ($message) use($dataToMail)
        {
            $message->from($dataToMail['email'], $dataToMail['full_name']);
            $message->to('contact@example.com', 'Example Contact')
                    ->subject('Contact Form Details from example.com (Form No.:' . $dataToMail['cont_code'] . ')')
                    ->cc(['ex@example.net', 'exam@hotmail.com'])
                    ->bcc(['examp@gmail.com', 'esmap@example.net']);
        });

        var_dump($mail); // displays **int 5** in chrome console

        dd($mail); // displays **5** in chrome console

        if ($mail) {
            return response(['status' => 'success', 'msg' => 'Contact form details has been successfully submitted.']);
        }
    }

    return response(['status' => 'failed', 'msg' => 'Something went wrong. Please try again later.']);
}

The issue is that, the mail is sent thrice instead of just once. I don't know what is happening here. Earlier [maybe 5 months back], it was working correctly, and now, all of a sudden, it started doing this. I am trying to figure out what is the reason behind this, but to no success. I have also updated the framework using composer update but the issue still exists.

EDIT 1: I was playing trial and error with the Mail facade, and I guess the issue is in bcc method, because I tried no bcc and cc, it worked as expected. Then again I tried with just cc and no bcc, this time also it worked correctly as expected but when added bcc with cc, I received the same mail thrice.

Where I have made the mistake ? Kindly help me out.

  • 写回答

2条回答 默认 最新

  • dtxpz8785 2015-12-07 10:28
    关注

    Some mail drivers, usually Mandrill, sends CC'ed & BCC'ed emails as separate emails instead of treating them as a single email.

    I haven't used mailtrap, but same thing might be happening in your case too.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错