1

I am creating a simple contact us from in Laravel.

I set up .env with my Gmail account. I can send email from Laravel with my email address, no problem with this.

But, I want to send email from sender address who is sending the message form contact us form.

Here is my code in controller:

public function sendMessage(Request $request)
{
    $this->validate($request,[
        'email'=>'required|email',
        'subject'=>'required',
        'body'=>'required|max:150'
    ]);
    $body = $request['body'];
    Mail::send('emails.support',['body' => $body], function($message){
        $email = Input::get('email');
        $subject = Input::get('subject');
        $message->sender($email);
        $message->to('smartrahat@gmail.com','Mohammed');
        $message->subject($subject);
    });
    return redirect('contactUs');
}

Though, I am getting email address form contact us form, the email always sent form my email account which I configured in .env

smartrahat
  • 5,381
  • 6
  • 47
  • 68
  • `->from($email)`, not `->sender($email)`. Refer to the documentation: http://laravel.com/docs/5.1/mail#sending-mail. I've never used `sender()` before, and I know it exists as a function, but `from()` has never had an issue from my experience. – Tim Lewis Nov 18 '15 at 20:09
  • using `from()` or `sender()` working same for me. – smartrahat Nov 18 '15 at 20:15
  • I think this solves your issue: https://stackoverflow.com/a/56965347/2311074 – Adam Jul 10 '19 at 08:41
  • Possible duplicate of [multiple mail configurations](https://stackoverflow.com/questions/26546824/multiple-mail-configurations) – Adam Jul 10 '19 at 08:41

3 Answers3

6

I would have done some things different. I guess $body is the email-text? You should put this in a array and add it as a parameter in Mail::send (or directly put $request->all() as a parameter.

Also, inside of the closure of Mail, i don`t thinks its very nice to put logic there (like $email=Input::get). It does not look right if you ask me.

Didn`t test this code, but this should work:

public function sendMessage(Request $request)
{
  $this->validate($request,[
    'email'     => 'required|email',
    'subject'   => 'required',
    'body'      => 'required|max:150'
  ]);

  $data = [
            'email'   => $request->input('email'), 
            'subject' => $request->input('subject'),
            'body'    => $request->input('body')
        ];

  Mail::send('emails.support', $data, function($message) use ($data)
  {
    $message->from($data['email']);
    $message->to('smartrahat@gmail.com','Mohammed');
    $message->subject($data['subject']);
  });

  return redirect('contactUs');
}

Because you add $data as as 'use', you can access this data inside the closure. The $data send as a parameter, can be used in the email blade. In this example, you can access the data like this: $email , $subject , $data, in blade these will output the values.


But you can also do it like this:

Mail::send('emails.support', $request->all(), function($message) use ($data)

Maurice
  • 1,342
  • 11
  • 21
  • Everything is fine, mail is sending too. But the problem is I can't change the sender address. whatever I write here: `$message->form('user@domain.com','name')`, the mail is always sending from the address which is configured in `.env` – smartrahat Nov 24 '15 at 14:14
  • Is it using the address from the `mail.php` config file? The 'Global "From" Address – Maurice Nov 24 '15 at 19:04
  • Not exactly the `mail.php` it's using the `username` from `.env` – smartrahat Nov 24 '15 at 19:10
  • I`m not sure, but i think 'from' gets overwritten because of this setting in mail.php. You can try setting it to null, check if that makes a difference. – Maurice Nov 24 '15 at 19:12
  • I already tried that. Someone told me `gmail` is responsible for this. I am using `smtp` server of google. Gmail is converting the `from` to the authentic email address for security reason. I may send email in name of Obama. But the funny thing is I still can send email with any email address by `Raw PHP Code`. Just wanted to do it in Laravel way. – smartrahat Nov 24 '15 at 20:33
1
public function reset_password(Request $request)
{
    $user=User::whereEmail($request->email)->first();
    if(count($user) == 1)
    {
        $data = array('name'=>"Virat Gandhi");
        $subject=$request->email;
        mail::send(['emails'=>'reset_set'],$data,
        function($message)use($subject)
        {
            $message->from('your@gmail.com','kumar');
            $message->to($subject);
            $message->subject($subject);
        });  
        echo "Basic Email Sent. Check your inbox.";
    }
}
Robert
  • 7,394
  • 40
  • 45
  • 64
kumaresan
  • 11
  • 1
  • 1
    Welcome to stackoverflow. Please explain your code a bit, so that people can learn from it instead of just copying & pasting something from the web. In particular, I'd be curious about the hard-coded "Virat Gandhi" in there, or the "kumar". – Robert Sep 05 '18 at 17:02
-1
foreach ($emails as $value1) 
{
    $to[]=implode(',',$value1['0']);
}
$subject=$request->input('sub');
$mailsend=Mail::send('email.subscribe',$mail_content,
function($message)use($to,$subject)
{
    $message->from('ganarone@ganar.io','Ganar');
    $message->to($to);
    $message->subject($subject);
});  
mastisa
  • 1,875
  • 3
  • 21
  • 39