This is the problem:
The name associated with the email shows up as "Example"
In config/mail.php
set from
property as:
'from' => ['address' => '[email protected]', 'name' => 'Firstname Lastname']
Here, address should be the one that you want to display in from email and name should be the one what you want to display in from name.
P.S. This will be a default email setting for each email you send.
If you need to use the Name as a variable through code, you can also call the function from() as follows (copying from Brad Ahrens answer below which I think is good to mention here):
return $this
->from($address = '[email protected]', $name = 'Sender name')
->subject('Here is my subject')
->view('emails.view');
You can use
Mail::send('emails.welcome', $data, function($message)
{
$message->from('[email protected]', 'Laravel');
$message->to('[email protected]')->cc('[email protected]');
});
Reference - https://laravel.com/docs/5.0/mail
In the case of google SMTP, the from address won't change even if you give this in the mail class. This is due to google mail's policy, and not a Laravel issue. Thought I will share it here.
A better way would be to add the variable names and values in the .env
file.
Example:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=password
MAIL_ENCRYPTION=tls
MAIL_FROM_NAME="My Name"
[email protected]
Notice the last two lines. Those will correlate with the from name and from email fields within the Email that is sent.
For anyone who is using Laravel 5.8 and landed on this question, give this a shot, it worked for me:
Within the build function of the mail itself (not the view, but the mail):
public function build()
{
return $this
->from($address = '[email protected]', $name = 'Sender name')
->subject('Here is my subject')
->view('emails.welcome');
}
Happy coding :)
If you want global 'from name' and 'from email',
Create these 2 keys in .env file
MAIL_FROM_NAME="global from name"
MAIL_FROM_ADDRESS[email protected]
And remove 'from' on the controller. or PHP code if you declare manually. now it access from name and from email.
config\mail.php
'from' => [
'address' => env('MAIL_FROM_ADDRESS', '[email protected]'),
'name' => env('MAIL_FROM_NAME', 'write name if not found in env'),
],
ON my controller.
$conUsBody = '';
$conUsBody .= '<h2 class="text-center">Hello Admin,</h2>
<b><p> '.trim($request->name).' Want some assesment</p></b>
<p>Here are the details:</p>
<p>Name: '.trim($request->name).'</p>
<p>Email: '.trim($request->email).'</p>
<p>Subject: '.trim($request->subject).'</p>';
$contactContent = array('contactusbody' => $conUsBody);
Mail::send(['html' => 'emails.mail'], $contactContent,
function($message) use ($mailData)
{
$message->to('[email protected]', 'Admin')->subject($mailData['subject']);
$message->attach($mailData['attachfilepath']);
});
return back()->with('success', 'Thanks for contacting us!');
}
My blade template.
<body>
{!! $contactusbody !!}
</body>
I think that you have an error in your fragment of code. You have
from(config('app.senders.info'), 'My Full Name')
so config('app.senders.info') returns array.
Method from should have two arguments: first is string contains address and second is string with name of sender. So you should change this to
from(config('app.senders.info.address'), config('app.senders.info.name'))
© 2022 - 2024 — McMap. All rights reserved.
->from('[email protected]', 'Awesome Name')
– Edgaredgard