使用laravel将一个组合文本发送到保存在数据库中的电子邮件

时间:2015-11-25 05:44:20

标签: php email laravel-5

我创建了一个表单,在提交后会转到电子邮件ID,该表单存储在Laravels邮件功能中。但是我想将同一封电子邮件发送给多个人,他们的email-id存储在数据库中。这是我的代码:

路线:

Route::get('contactform', 'ClientsController@display1');
Route::post('contactform', 'ClientsController@contact');

我的控制器:

public function display(){
    $data = Input::get('agree');
    $count = count ($data);
    if($count){
        return $this->contact( $data,$count);
    }
    else{
        echo "Select At least one email-id.";
        return view('clientinfo.backtoprev');
    }

}

    public function display1(){

        return view('contactform');
    }

    public function contact($data,$count){
        $input = Input::only('name', 'email', 'msg');

        $validator = Validator::make($input,
            array(
                'name' => 'required',
                'email' => 'required|email',
                'msg' => 'required',
            )
        );

        if ($validator->fails())
        {
            return Redirect::to('contact')->with('errors', $validator->messages());
        } else { 


            Mail::send('contactemail', $input, function($message) use($data)
            {
                $message->from('your@email.address', 'Your Name');

                $message->to( $data);
            });

            return view('clientinfo.display', compact('data','count'));
        }

    }

这是我的观点" Contactform"

{!! Form::open(array('action' => 'ClientsController@contact','method' => 'post','name'=>'f1' , 'id'=>'form_id'))!!}  
    @if(Session::has('errors'))
        <div class="alert alert-warning">
            @foreach(Session::get('errors')->all() as $error_message)
                <p>{{ $error_message }}</p>
            @endforeach
        </div>
    @endif
       //My fields, name & message
{!! Form::close() !!}

来自&#34;显示&#34;方法我将两个变量$ data和$ count传递给联系方法,但填写表单并点击后提交错误来自

缺少App \ Http \ Controllers \ ClientsController :: contact()的参数1

2 个答案:

答案 0 :(得分:0)

原因:您未在Mail::函数中传递两个参数

你应该怎么做:

Mail::中你应该传递这样的两个参数

Mail::send('contactemail', $input, function($message) use($data, $input)

更新:您应该尝试使用比尝试触发电子邮件更好的内容。

Mail::send([], array('UserName' => $UserName, 'Email' => $Email), function($message) use ($UserName, $Email) {
                    $MailBody = 'Hi'.$UserName.'<br>Your HTML Content';
                    $message->setBody($MailBody, 'text/html');
                    $message->to($Email);
                    $message->subject('Subject of Email');
                });

答案 1 :(得分:0)

以下是对我的问题有效的更正。

我使用了一个控制器而不是两个:

    public function showForm(Request $request )
    {
        //Get Content From The Form
        $name = $request->input('name');
        $email = Input::get('agree');
        $message = $request->input('message');

        //Make a Data Array
        $data = array(
            'name' => $name,
            'email' => $email,
            'message' => $message
        );

        //Convert the view into a string
        $emailView = View::make('contactemail')->with('data', $data);
        $contents = (string) $emailView;

        //Store the content on a file with .blad.php extension in the view/email folder
        $myfile = fopen("../resources/views/emails/email.blade.php", "w") or die("Unable to open file!");
        fwrite($myfile, $contents);
        fclose($myfile);

        //Use the create file as view for Mail function and send the email
        Mail::send('emails.email', $data, function($message) use ($data) {
            $message->to( $data['email'], 'Engage')->from('stifan@xyz.com')->subject('A Very Warm Welcome');
        });
//        return view();
    }

路线:

Route::post('contactform', 'ClientsController@showForm');
Route::get('/', 'ClientsController@profile');

视图 contactemail 包含要发送的数据,以及我们通过邮件功能发送的视图电子邮件。当用户在表单中放入数据时,由于这些代码行,该数据将保存在 email.blade.php 中:

//Convert the view into a string
        $emailView = View::make('contactemail')->with('data', $data);
        $contents = (string) $emailView;

        //Store the content on a file with .blad.php extension in the view/email folder
        $myfile = fopen("../resources/views/emails/email.blade.php", "w") or die("Unable to open file!");
        fwrite($myfile, $contents);
        fclose($myfile);