Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davy_yg's avatar
Level 27

Sending email

I am trying to understand the concept of sending email.

contact.blade.php

<form class="form-horizontal" action="" method="POST">
 <div class="form-group">
 <label class="control-label col-sm-2" for="email">Nama:</label>
 <div class="col-sm-4">
     <input type="email" class="form-control" name="nama" id="email">
 </div>
 </div>
 <div class="form-group">
 <label class="control-label col-sm-2" for="pwd">Email:</label>
 <div class="col-sm-4">
     <input type="text" class="form-control" name="email" id="pwd" >
 </div>
 </div>
 <div class="form-group">
 <label class="control-label col-sm-2" for="pwd">Nomor Telepon:</label>
 <div class="col-sm-4">
     <input type="text" class="form-control" name="telp" id="pwd" >
 </div>
 </div>
 <div class="form-group">
 <label class="control-label col-sm-2" for="pwd">Type Proyek:</label>
 <div class="col-sm-4">
     <select class="form-control" name="project">
     <option value="Project1">Project1</option>
     <option value="Project2">Project2</option>
     <option value="Project3">Project3</option>
     <option value="Project4">Project4</option>
     <option value="Project5">Project5</option>
     <option value="Project6">Project6</option>
    </select>
 </div>
 </div>

 <div class="form-group">
 <label class="control-label col-sm-2" for="pwd">Pesan:</label>
 <div class="col-sm-4">
     <textarea name="pesan" class="form-control"></textarea>
 </div>
 </div>

 <div class="form-group">
 <div class="col-sm-offset-2 col-sm-10">
     <button type="submit" class="btn btn-default">Submit</button>
 </div>
 </div>
</form>

FPController.php

 public function sendmail()
    {

    Mail::to($request->user())
        ->send(new OrderShipped($order));

    Session::flash('flash', 'successfully send email');

    return view('contact');
    }

web.php

Route::post('/sendmail', 'FPController@sendmail');

I am not sure how to send the information from contact page to the intended email address?

0 likes
31 replies
davy_yg's avatar
Level 27

I am not sure what to write in the FPController.php in order to send the contact information?

Can you help? forget about the ordershopped. I am not sure how to write the mail in order to send the contact form information.

Sinnbeck's avatar

Think of it like a controller and blade file. The mailable is the "controller". You pass data to the constructor and add it to $this (replace order with whatever data you need)

public function __construct(Order $order)
    {
        $this->order = $order;
    }

And pass it to the blade view

public function build()
    {
        return $this->view('emails.orders.shipped', $this->order);
    }
Sinnbeck's avatar

BTW.. If you have never sent emails using code before, be careful with your html. Emails mostly only support tables and removes your css references. If you use the markdown version of the mailable, laravel will handle this for you (you will loose some design control though)

davy_yg's avatar
Level 27

I cannot understand where to put the mail template? and is it possible to put the form data into my mail template?

Sinnbeck's avatar

As I said it is just a blade file,so it goes in views. The example would be

/views/emails/orders/shipped.blade.php

And you could just pass the form data to the constructor

Mail::to($request->user())
        ->send(new OrderShipped($request->all() ));
davy_yg's avatar
Level 27

Where to write this files?

public function __construct(Order $order)
    {
    $this->order = $order;
    }

public function build()
    {
    return $this->view('emails.orders.shipped', $this->order);
    }

Is this to call the mail template? What file is it and where to look at in laravel?

Sinnbeck's avatar

It goes inside the mailable in app/Mail

php artisan make:mail MyMailName
davy_yg's avatar
Level 27

FPController.php

public function sendmail(request $request)
   {

    $data = Contact::find(1);

    Mail::to($data->contact_email)
        ->send(new ContactMail($request->all()));

    Session::flash('flash', 'successfully send email');

    return view('contact');
}

ok, I created ContactMail:

Mail/ContactMail.php

class ContactMail extends Mailable
{
    use Queueable, SerializesModels;

    /**
    * Create a new message instance.
    *
    * @return void
    */

public function __construct($request->all())
    {
    //
    $this->name = $request->name;
    $this->email = $request->email;
    $this->phone = $request->phone;
    $this->project = $request->project;
    $this->pesan = $request->pesan;


    }

    /**
    * Build the message.
    *
    * @return $this
    */

public function build()
    {
    return $this->view('mail.contact', $this->name, $this->email, $this->phone, $this->project,         $this->pesan);
     }
}

Is this the correct way to do it?

views/mail/contact.blade.php

<html>
<body>

<h3>Pesan Baru untuk Anda</h3>

<p>
Name    : {{ name }}
Email   : {{ email }}
Phone   : {{ phone }}
Project : {{ project }}
Pesan   : {{ pesan }}


</p>

</body>
</html>
Fajar's avatar

@davy_yg

1. php artisan make:mail ContactMail --markdown=emails.contact

2.  open file ContactMail and write this
public function __construct(Order $order)
{ 
    $this->order  = $order;
}
public function build()
    {
        return $this->from('email from', 'and anything youre project')
                    ->markdown('emails.contact')
                    ->with('order', $this->order);
    }
3. in youre contact.blade.php
get data with {{ $order->name }}

4.in youre controller

$to = Mail::to($request->get('email'))->send(new ContactMail($order));

hope this can help you
davy_yg's avatar
Level 27

What is orders?

public function sendmail(request $request)
  {

    // $request->name;
    // $request->email;
    // $request->phone;
    // $request->project;
    // $request->pesan;


    $data = Contact::find(1);

    Mail::to($data->contact_email)
        ->send(new ContactMail($request->all()));

    Session::flash('flash', 'successfully send email');

    return view('contact');
}

I thought it should $request->all() since these are all the fields from the contact forms.

Sinnbeck's avatar

I belive orders are just an example. Yours should be fine (expect for the formatting of the email). Does it not work?

davy_yg's avatar
Level 27

What should I write in: views/mail/contact.blade.php ?

<html>
<body>

<h3>Pesan Baru untuk Anda</h3>

<p>
Name    : {{ name }}
Email   : {{ email }}
Phone   : {{ phone }}
Project : {{ project }}
Pesan   : {{ pesan }}

</p>

</body>
</html>

In order to test do I have to put my website on the web server? Is it possible to test the contact form from localhost using xampp as the server?

davy_yg's avatar
Level 27

I already sign up mailtrap and get my first email of mailtrap and already set the config in .env

After I click submit the contact form I get:

419 Page Expired

Any clue why?

davy_yg's avatar
Level 27

syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting ')'

I get an error in my Mail\ContactMail.php

    public function __construct($request->all())
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Oh yeah missed that. Should be something like

public function __construct($data)
    {
    //
    $this->name = $data['name'];
    $this->email = $data['email'];
davy_yg's avatar
Level 27

Argument 2 passed to Illuminate\Mail\Mailable::view() must be of the type array, string given, called in D:\xampp72\htdocs\aws_admin\app\Mail\ContactMail.php on line 39

line 39: return $this->view('mail.contact', $this->nama, $this->email, $this->phone, $this->project, $this->pesan);

Sinnbeck's avatar

Please read about passing data to a view.

Hint, it need to be an array. This is just a normal view

davy_yg's avatar
Level 27

Connection could not be established with host smtp.mailtrap.io [The requested address is not valid in its context. #10049]

All my MAIL_*=

does not use '(quote)

Any clue why?

Sinnbeck's avatar

Strange. Did you set the port?

Also you can try turning off your firewall (just while testing)

Fajar's avatar

@davy_yg Settingan .env untuk email ganti pakai log ajah dulu biar enak tesnya

Untuk yang tdi saya buat itu contoh

Fajar's avatar

@davy_yg so with the driver log all messages whether they succeed or not will be recorded on the ground ... well if we use the log we can see the message was sent or not

change this

MAIL_DRIVER=smtp

to
MAIL_DRIVER=log

if it's successful just try another driver

this my setting driver mail

AIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=25
MAIL_USERNAME=get a username at mailtrap.io
MAIL_PASSWORD=get a password at mailtrap.io
MAIL_ENCRYPTION=ssl
davy_yg's avatar
Level 27

I finally ask someone to help me out. I eventually use gmail. I am going to close this forum. Thanks all.

Next

Please or to participate in this conversation.