0

我正在一个网站上工作,我想在其中格式化收到的电子邮件的内容。

此刻,它像这样排成一行,只显示值。

Mike abc@gmail.com 9870000000 纽约 Hello World

我在 Laravel 中用于电子邮件的控制器是:

Class PostingMessageController extends Controller
{
    public function create() 
    {
        return view('posting');
    }

   public function store(Request $request)
   {

/*
    dd($request->all());

  */   
      $this->validate($request, [
      'name' => 'required',
      'email' => 'required|email',
      'number' => 'required',
      'city' => 'required',
      'post' => 'required'
      ]);

       Mail::send('emails.posting-message', [
        'msg'=> $request->name . "\r\n"
        . $request->email . "\r\n"
        . $request->number . "\r\n"
        . $request->city . "\r\n"
        . $request->post . "\r\n"


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

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

           $mail->to('abc@hellworld.com')->subject('Contact Message');
       });

       return redirect()->back()->with('flash_message', 'thank you, your posting info has been sent to our team. we will reach out as soon as we can to provide next steps!');

   } 
}   


问题陈述:

我想知道我需要在控制器中进行哪些更改,以便所有内容都显示在不同的行中,如下所示,左侧的字段名称和右侧的值。目前,我只看到上面提到的值。

Name        Mike

Email       abc@gmail.com

Number      9870000000

City        New York

Post        Hello World 
4

3 回答 3

2

如果您想修改您的电子邮件输出,您可能正在寻找可以进入刀片并设计它然后发送它的东西,这就是我的做法

在我的路线中:routes/Web.php

Route::get('contact-us', 'Main@showContactUs')->name('route_name');
Route::post('contact-us', 'Main@doContactUs')->name('route_name');

在我的控制器中:app/Http/Controllers/Main.php

use Illuminate\Support\Facades\Mail;
use App\Mail\SendMailable;

class Main extends Controller
{
    public function showContactUs()
    {
        $data = [];
        return View::make('pages.contactUs',$data);
    }
    public function doContactUs(Request $r)
    {
     $fullname = $r->get('fullName');
     $phone    = $r->get('phone');
     $email    = $r->get('email');
     $description = $r->get('message');
     Mail::to('RECEIVER_EMAIL_ADDRESS')->send(new SendMailable($fullname, $phone, $email, $description));
      if (Mail::failures())
      {
        $message1 = " Something Went Wrong.";
      }
      else
      {
        $message2 = " Message Sent Successfully.";
      }
    return redirect()->route('route_name'])->with([
            'warning' => $message1,
            'success' => $message2
        ]);
    }
}

然后创建这个:app\Mail\SendMailable.php

<?php
namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class SendMailable extends Mailable
{
    use Queueable, SerializesModels;
    public $fullname,$phone,$email,$description;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($fullname, $phone, $email,$description)
    {
        $this->fullname = $fullname;
        $this->phone = $phone;
        $this->email = $email;
        $this->description = $description;
    }
    /**
     * Build the message. THIS WILL FORMAT YOUR OUTPUT
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('email.layoutOne')->subject('Contact Us Subject');
    }
}

最后在 Blade 中:resources/email/layoutOne.blade.php 你可以这样做

<div>
    <p>Fullname  : {{ $fullname }}</p>
    <p>Phone No. : {{ $phone }}</p>
    <p>Email Address : {{ $email }}</p>
    <p>Description : {{ $description }}</p>
    <hr>
    <p>Thank you for your Query. We'll get back to you within 24 Hours. </p>
</div>

希望这可以帮助。

于 2018-08-26T04:13:27.960 回答
0

为什么不将数据作为数组传递并在视图中格式化?

$data = array(
    'name'=> $request->name,
    'email'=> $request->email,
    'number'=> $request->number,
    'city'=> $request->city,
    'post'=> $request->post
);

Mail::send('emails.posting-message', $data, function($mail) use($request) {
    $mail->from($request->email, $request->name);
    $mail->to('letsruckify@ruckify.com', 'Name')->subject('Contact Message');
});

您可以以 $name 或 $email 的形式访问变量,并在刀片视图中实现一些表,就像@ViperTecPro 的答案中的表一样

这是您如何做到这一点的一个示例……具体的 html 由您决定。

<div>
    <p>Name  : {{ $name }}</p>
    <p>Email Address :    {{ $email }}</p>
</div>
于 2018-08-26T04:51:21.310 回答
0
Mail::send('emails.posting-message', [
    'msg'=> "Name\t" . $request->name . "\r\n"
    . "Email\t" . $request->email . "\r\n"
    . "Number\t" . $request->number . "\r\n"
    . "City\t" . $request->city . "\r\n"
    . "Post\t" . $request->post . "\r\n"


   ]
于 2018-08-26T03:38:27.613 回答