通过Laravel Mailable将阵列数据发送到电子邮件

时间:2019-10-28 20:12:42

标签: php arrays laravel forms

对于create.blade.php(我的表单),我有以下几行

<form action="/contact" method="POST">
    <div class="form-control">

<fieldset>
    <legend><span class="number">1</span> Personal Information</legend>


        <input type="text" name="name" placeholder="Name *" value="{{old('name') }}">
        <div>{{ $errors->first('name') }} </div>

        <input type="text" name="email" placeholder="Phone *" value="{{old("email") }}">
        <div>{{ $errors->first('email') }} </div>

        <input type="text" name="phone" placeholder="Phone *" value="{{old('phone') }}">
        <div>{{ $errors->first('phone') }} </div>   
        <label for="message">Message</label>
        <textarea type="text" name="message" id="message" cols="30" rows="10">{{old('message') }} 
      </textarea>
        <div>{{ $errors->first('message') }} </div>

<?php
$countries = array('India' => 'India',
        'Pakistan' => 'Pakistan',
        'Bosnia' => 'Bosnia',
        'Andorra' => 'Andora',
        'Haiti' => 'Haiti',
        'Australia' => 'Australia',
        'Maldives' => 'Maldives'
        );
?>

<label for="message">Country</label>

<select name="country">
    @foreach ($countries as $country)
        <option value="{{ $country }}" @if(old('country') == $country) selected="selected" @endif>{{ 
$country }}</option>
    @endforeach
</select>
@csrf
    <button type="submit" class="btn btn-primary">Send message</button>
</div>
</form>

对于我的contact-form.blade.php(可邮寄),我有以下内容。这应该在我收到的电子邮件中显示提交的数据。问题是我在输入字段(姓名,电子邮件号码)中获取了所有数据,但没有获得所选国家/地区的数据。问题很可能在于以下几行。

 @component('mail::message')

#Requested Information

<strong>Name</strong> {{ $data['name'] }}
<strong>Email</strong> {{ $data['email'] }}

<strong>Message</strong> {{ $data['message'] }}

<strong>Country</strong> {{ $data['$country'] }}

@endcomponent

3 个答案:

答案 0 :(得分:0)

正确的是$data['country']而不是$data['$country']

答案 1 :(得分:0)

我认为您正在寻找以下代码

 $data=array(
        'Name' => $request->name,
        'email' => $request->email,
        'country'=>$request->country,
        'phone'=> $rwquest->phone,
    );

    Mail::send('here your email filename', $data, function($message)  {
        $message->subject('');   //subject of email
        $message->from();   // sender email address
        $message->to();   //receiver email
    });

答案 2 :(得分:0)

我插入了以下行

$countries = $_POST['country'];

现在看起来像这样

<select>
    $countries = $_POST['country'];

@foreach ($countries as $country)
<option value="{{ $country }}" name="country"

@if(old('country') == $country) selected="selected" 
@endif>

{{ $country }}</option>
@endforeach
</select>

也是我的邮件中的以下行

Country {{ $data['country'] }}

我得到了错误的未定义索引:国家