smartrahat smartrahat - 11 days ago 9
PHP Question

How to change header information of mail in Laravel?

I am creating a simple contact us form using Laravel 5.1

public function sendMessage(){
$data = [
'name' => Input::get('name'),
'email' => Input::get('email'),
'subject' => Input::get('subject'),
'body' => Input::get('body')
];
Mail::send('emails.contact',$data, function($message) use($data){
$message->from($data['email'], $data['name']);
$message->to('smartrahat@gmail.com','Mohammed');
$message->subject($data['subject']);
});
Session::flash('success_message','Mail sent successfully!');
return redirect('contact');
}


Everything is working fine but the sender email address is not the one it get from the contact page. The email is sent from the address which I configure in
.env


I want to have the email from the sender email address which he filled up contact form. Or, you can say I want to change the header information (only
from
, I can change other information).

Answer

Well Laravel will send the mail through the given smtp server. I guess the smtp server (e.g. google doing this) will not let you change your from address to another address then the account belongs to.

If you want to reply to this address directly in your email programm you can add $message->replyTo($data['email'], $data['name']);.

Comments