Thanks all for the responses. Yes I was trying to use gmail to send emails and it is true that they override whatever value you have set with default values. It can be changed in gmail but you will have to register each individual email you want to use as a sender and this is not dynamic so not worth trying. However thank you for the help.
@snapey , thanks for the advice on the auth()->user() so I can just call ('id', id) instead?
and @aurawindsurfing , I did use the mailable class. I pass in the data like so however the user was queried on the StartConversation class and not passed in as data attribute.
Perhaps there is a better way to do this but I call a sendMail() function in my controller like this
public function sendMail(Request $request)
{
$data = $request->validate([
'id' => 'required|integer'
]);
$question = Question::where('id', $request->id)->first();
$engagement = Engagement::where('id', $question->engagement_id)->first();
$client = CLient::where('id', $engagement->client_id)->first();
Mail::to($client->email)->send(new StartConversation(['question' => $question, 'engagement' => $engagement, 'client' => $client]));
$question->email_sent = true;
$question->save();
return response()->json([ 'question' => $question, 'message' => 'Email Was Sent']);
}
which calls the StartConversation mailable class
/**
* The demo object instance.
*
* @var Client
*/
public $client;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($client)
{
$this->client = $client;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
$account = Account::first();
$sender = User::where('id', auth()->user()->id)->first();
$email = $sender->email;
$name = $sender->name;
return $this->from($email, $name)
->replyTo($email, $name)
->bcc($email, $name)
->subject('Pending Questions From '. $account->business_name)
->view('email')
->with([
'phoneNumber' => $account->phone_number,
'faxNumber' => $account->fax_number,
'accountName' => $account->business_name,
'accountEmail' => $account->email,
'userEmail' => $email
]);
}
}