【问题标题】:sending one composed text to emails saved in database using laravel使用 laravel 将撰写的文本发送到保存在数据库中的电子邮件
【发布时间】:2015-11-25 05:44:19
【问题描述】:

我创建了一个表单,提交后它会转到存储在 Laravel 邮件功能中的电子邮件 ID。但我想将相同的电子邮件发送给多个电子邮件 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'));
        }

    }

这是我的观点“联系方式”

{!! 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() !!}

从“显示”方法中,我将两个变量 $data 和 $count 传递给联系方法,但在填写表单并点击提交后出现错误

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

【问题讨论】:

    标签: php email laravel-5


    【解决方案1】:

    原因:您没有在 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');
                    });
    

    【讨论】:

    • 您能否尝试在 Mail:: 函数中发送 input 和其他参数,而不是作为参数发送。我的意思是硬编码它?
    • 硬编码后我只能向一个人发送电子邮件,当我第一次尝试时,我只为一个人尝试过,然后我尝试将邮件发送到阵列硬编码,但再次出现同样的错误。然后我传递了变量,如上所述。
    • mail to array hard-coding 是什么意思。对不起,我不明白.. 你的意思是你试图在 foreach 循环中运行邮件功能?
    • 我已经更新了我的答案。你应该修改它并使用,那将是更好的解决方案;)
    【解决方案2】:

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

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

        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是我们通过邮件功能发送的。当用户将数据放入表单时,由于以下代码行,该数据将保存在 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);
    

    【讨论】:

      猜你喜欢
      • 2022-07-15
      • 2014-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多