如何使用Mail :: send发送包含多个字段条目的电子邮件

时间:2018-04-07 18:55:56

标签: php laravel email

我目前正在为大学的最后一年项目工作。我目前能够使用'msg'=>发送显示邮件的电子邮件$请求 - >消息。我想知道是否有一种方法可以发送包含多个字段条目的电子邮件,例如:

    'msg' => $request->message, $request->ornamentation

我不知道如何写这个,上面的方法不起作用。谢谢。

我已将我的控制器文件与html一起附加。

ContactMessageController

        <?php

    namespace App\Http\Controllers;

    use Mail;


    use Illuminate\Http\Request;



    class ContactMessageController extends Controller
    {

        public function create(){

            return view('contact');
        }

        public function store(Request $request)

        {

    $this->validate($request,[    

        'name' => 'required',
        'email' => 'required|email',
        'message' => 'required'

        ]);

        Mail::send('emails.contact-message', [

            'msg' => $request->message

        ], function ($mail) use($request) {

            $mail->from($request->email, $request->name);

            $mail->to('flannery-n@ulster.ac.uk')->subject('Contact message');

        });

        return redirect()->back()->with('flash_message', 'Thank you for your message');

        }
    }

Contact.blade.php

<head>

    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

    <style>

        .invalid-feedback {

            display: block;
        }

    </style>
    <title> Contact </title>

</head>

<body>

    <div class="container">

        <h1> Feedback Form </h1>

        <div class="row">       
            <div class="col-md-6">

                @if (Session::has('flash_message'))

                    <div class="alert alert-success">{{ Session::get('flash_message') }}</div>

                @endif

                <form method="post" action="{{route('contact.store')}}">

                    {{ csrf_field() }}

                    <div class="form-group">
                        <label> Full Name: </label>
                        <input type="text" class="form-control" name="name">
                        @if ($errors->has('name'))
                        <small class="form-text invalid-feedback"> {{ $errors->first('name') }}</small>
                        @endif
                    </div>

                    <div class="form-group">
                        <label>  Email: </label>
                        <input type="text" class="form-control" name="email">
                        @if ($errors->has('email'))
                        <small class="form-text invalid-feedback"> {{ $errors->first('email') }}</small>
                        @endif
                    </div>

                    <div class="form-group">
                        <label> Technical Ability: </label>
                        <textarea name="message" class="form-control" placeholder="Enter comments here" style="width: 600px;                                height: 100px;"></textarea> 
                        @if ($errors->has('message'))
                        <small class="form-text invalid-feedback"> {{ $errors->first('message') }}</small>
                        @endif      
                    </div>

                    <div class="form-group">
                        <label> Ornamentation: </label>
                        <textarea name="ornamentation" class="form-control" placeholder="Enter comments here" style="width:                             600px; height: 100px;"></textarea>
                        @if ($errors->has('message'))
                        <small class="form-text invalid-feedback"> {{ $errors->first('message') }}</small>
                        @endif
                    </div>

                    <div class="form-group">
                        <label> Speed: </label>
                        <textarea name="speed" class="form-control" placeholder="Enter comments here" style="width: 600px;                              height: 100px;"></textarea>
                        @if ($errors->has('message'))
                        <small class="form-text invalid-feedback"> {{ $errors->first('message') }}</small>
                        @endif
                    </div>

                    <div class="form-group">
                        <label> Control: </label>
                        <textarea name="control" class="form-control" placeholder="Enter comments here" style="width: 600px;                            height: 100px;"></textarea>
                        @if ($errors->has('message'))
                        <small class="form-text invalid-feedback"> {{ $errors->first('message') }}</small>
                        @endif
                    </div>

                    <div class="form-group">
                        <label> Additional comments: </label>
                        <textarea name="message" class="form-control" placeholder="Enter comments here" style="width: 600px;                            height: 100px;"></textarea>
                        @if ($errors->has('message'))
                        <small class="form-text invalid-feedback"> {{ $errors->first('message') }}</small>
                        @endif
                    </div>


                    <button class="btn btn-primary">Submit</button>


    </form>
    </div>

    </div>
    </div>      

</body>

1 个答案:

答案 0 :(得分:1)

我猜您正在将8/05/07 11:26:11 ERROR Executor: Exception in task 0.0 in stage 2741.0 (TID 2618) java.lang.NoSuchMethodError: com.google.common.cache.CacheBuilder.build(Lcom/google/common/cache/CacheLoader;)Lcom/google/common/cache/LoadingCache; at org.apache.hadoop.io.compress.CodecPool.createCache(CodecPool.java:62) at org.apache.hadoop.io.compress.CodecPool.<clinit>(CodecPool.java:74) at org.apache.parquet.hadoop.CodecFactory$BytesCompressor.<init>(CodecFactory.java:92) at org.apache.parquet.hadoop.CodecFactory.getCompressor(CodecFactory.java:169) at org.apache.parquet.hadoop.ParquetOutputFormat.getRecordWriter(ParquetOutputFormat.java:303) at org.apache.parquet.hadoop.ParquetOutputFormat.getRecordWriter(ParquetOutputFormat.java:262) at org.apache.spark.sql.execution.datasources.parquet.ParquetOutputWriter.<init>(ParquetFileFormat.scala:562) at org.apache.spark.sql.execution.datasources.parquet.ParquetFileFormat$$anon$1.newInstance(ParquetFileFormat.scala:139) at org.apache.spark.sql.execution.datasources.BaseWriterContainer.newOutputWriter(WriterContainer.scala:131) at org.apache.spark.sql.execution.datasources.DefaultWriterContainer.writeRows(WriterContainer.scala:247) at org.apache.spark.sql.execution.datasources.InsertIntoHadoopFsRelationCommand$$anonfun$run$1$$anonfun$apply$mcV$sp$1.apply(InsertIntoHadoopFsRelationCommand.scala:143) at org.apache.spark.sql.execution.datasources.InsertIntoHadoopFsRelationCommand$$anonfun$run$1$$anonfun$apply$mcV$sp$1.apply(InsertIntoHadoopFsRelationCommand.scala:143) at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:70) at org.apache.spark.scheduler.Task.run(Task.scala:86) at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:274) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) 变量内容输出到您的电子邮件模板,执行以下操作:

$msg

因此,在这种情况下,您可以以相同的方式添加其他请求属性。

首先,确保将它们传递给您的模板:

{{ $msg }}

然后,您可以将属性输出到模板,就像使用Mail::send('emails.contact-message', [ 'msg' => $request->message, 'ornamentation' => $request->ornamentation, // And any other attribute you want to pass along ], function ($mail) use ($request) { $mail->from($request->email, $request->name); $mail->to('flannery-n@ulster.ac.uk')->subject('Contact message'); }); 一样:

$msg

为了清楚起见,模板中可用的变量与传递的数组的键具有相同的名称。例如,如果你有一个数组,如:

{{ $ornamentation }}

模板中的变量是:

[
    'foo' => $request->message,
    'bar' => $request->ornamentation,
]

{{ $foo }}