How to change From Name in Laravel Mail Notification - laravel

This is the problem:
The name associated with the email shows up as "Example"

In config/mail.php
set from property as:
'from' => ['address' => 'someemail#example.com', '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 = 'noreply#example.com', $name = 'Sender name')
->subject('Here is my subject')
->view('emails.view');

You can use
Mail::send('emails.welcome', $data, function($message)
{
$message->from('us#example.com', 'Laravel');
$message->to('foo#example.com')->cc('bar#example.com');
});
Reference - https://laravel.com/docs/5.0/mail

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
MAIL_USERNAME=example#example.com
MAIL_PASSWORD=password
MAIL_ENCRYPTION=tls
MAIL_FROM_NAME="My Name"
MAIL_FROM_ADDRESS=support#example.com
Notice the last two lines. Those will correlate with the from name and from email fields within the Email that is sent.

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.

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 = 'noreply#example.com', $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=support#example.com
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', 'info#example.com'),
'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('my.personal.email#example.com', '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'))

Related

Validation of an email address in Laravel

I have created an edit form in my Laravel application form which accepts an email address. I have used Laravel validation on the server-side. It is validating the email address correctly when I pass a clearly invalid value like 'xxxxxx'.
But the problem is when I send an email address with only a top level domain and no dot like 'xxxxxx#yyyyyy', it accepts it as a valid email address.
How can I validate the email address to ensure it's using a proper domain?
With Laravel 7: you can use
'email' => 'email:rfc,dns'
You can simply do:
$validator = Validator::make($request->all(), [
'Email'=>'required|email'
]);
Try this:
$this->validate($request, [
'email' => 'required|regex:/(.+)#(.+)\.(.+)/i',
]);
It is not a Laravel issue. That is technically a valid email address.
Notice that if you tell the browser to validate the email address, it will also pass.
But you can use package EmailValidator for validating email addresses.
At first, also check these: https://laravel.com/docs/6.x/validation#rule-email
Or,
use the checkdnsrr function.
<?php
$email = 'email#gmail.com';
list($username, $domain) = explode('#', $email);
if (checkdnsrr($domain, 'MX')) {
echo "verified";
}
else {
echo "failed";
}
Laravel email validation and unique email insert database use for code:
'email_address' => 'required|email|unique:customers,email_address'

Add var in auth

I use stard auth from Laravel but I want to send var to auth's view. Actually I want to send title of website and keywords. In other controllers I can do that
return view('my.view')->with('title', 'My funny title');
How I can do that in Login, Register...
perhaps you should do something like this.
in your controller( you will find this controller in AuthenticatesUsers Traits located in Illuminate\Foundation\Auth folder.
$title= "my page title";
return view('my.view', compact('title'));
and in view, just use {{ $title }} where ever you cant to call that text. this should work.
Add this on baseController/Controller __construct() function
by this you will share the variable to every blade file.
$siteTitle = 'SiteTitle';
View::share($siteTitle);
Maybe try with this syntax as in documentation
return view('my.view', ['title' => 'My funny title']);
or
$data = [
'title' => 'My funny title',
...
];
return view('my.view', $data);
I remember having the similar issues while ago, though i cant remember how exactly i worked it out.

Issue with laravel and mailgun

Have a nice time,
I am wondering if there is anyone has the correct steps for using Mailgun with laravel 5.4
Many thanks and best regards,
These are my steps that i follow.
first open .env file and bellow code:
MAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=uremail#gmail.com
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=tls
create new account in mailgun.com SignUp if you don't have before.
After registeration active your mailgun account and click on Domails
and click on Add New Domail button. then you can see bellow screen.
After add name you can copy domain name and API Key.
Now you have to open services.php and add mailgun configration this
way :
on config/services.php
'mailgun' => array(
'domain' => 'youremail.com',
'secret' => 'key-11796c09e58-056a9e975c96dd334da0dd',
),
Now we are ready to send mail for test so first create test route
for email sending.
app/Http/routes.php define route: Route::get('mail', 'HomeController#mail');
Ok, now add mail function in HomeController.php file so add this way
public function mail()
{
$user = User::find(1)->toArray();
Mail::send('emails.mailEvent', $user, function($message) use ($user) {
$message->to($user->email);
$message->subject('Mailgun Testing');
});
dd('Mail Send Successfully');
}
At last create email template file for send mail so let's create mailEvent.blade.php file in emials folder.
resources/views/emails/mailEvent.blade.php`

Undefined variable inside Laravel view when using Mail

While trying to send verification email using Laravel 5.2, I get an error:
Undefined variable: confirmation_code (View:
C:\xampp\htdocs\laravel\resources\views\email\verify.blade.php)
My code looks like this:
Controller.php:
public function postSignup(Request $request){
$this->validate($request,[
'email'=>'required|unique:users|email',
'name'=>'required|max:50|min:3',
'password'=>'required|min:6',
'con-password'=>'required|same:password',
]);
$confirmation_code=['code'=>str_random(20)];
$name = $request->input('name');
Mail::send('email.verify',$confirmation_code,function($message)
use($request,$name){
$message->to($request->input('email'),$name)
->subject('Verify Your Email Address');
});
User::create([
'email'=>$request->input('email'),
'name'=>$request->input('name'),
'password'=>bcrypt($request->input('password'))
]);
return redirect()->back()->with('info','Congratulation you have been successfully registered.Please check your email for verification');
}
Mail.verify.blade.php:
<h2>Verify Your Email Address</h2>
<div>
Thanks for creating an account with the verification demo app.
Please follow the link below to verify your email address
{{ URL::to('register/verify/'.$confirmation_code) }}.<br/>
</div>
</body>
Try this:
Mail::send('email.verify', compact('confirmation_code'), function ($message) use($request, $name) {
$message->to($request->input('email'),$name)
->subject('Verify Your Email Address');
});
The reason why it fails is that Laravel views accept an associative array as their data, so that it can turn them into variables using keys as variables names and match them to their corresponding values.
What compact does is turn your variable into an associative array, with the name of the variable as its key (sort of the opposite of what the Laravel view will do).

Laravel 4 Mail function not working properly

I'm currently working on a web application which requires users to verify before they are able to use their account.
I'm using Cartalyst's Sentry to register the users, and sending the email using the built in Mail function, but whenever I register I get the following error:
Argument 1 passed to Illuminate\Mail\Mailer::__construct() must be an instance of
Illuminate\View\Environment, instance of Illuminate\View\Factory given,
called in
/var/www/vendor/laravel/framework/src/Illuminate/Mail/MailServiceProvider.php
on line 34 and defined
I can't figure out what causes this.
At the top of my code I included "use Mail" otherwise I would get another error:
Class '\Services\Account\Mail' not found
Code
// Create the user
$user = $this->sentry->register(array(
'email' => e($input['email']),
'password' => e($input['password'])
));
$activationCode = $user->getActivationCode();
$data = array(
'activation_code' => $activationCode,
'email' => e($input['email']),
'company_name' => e($input['partnerable_name'])
);
// Email the activation code to the user
Mail::send('emails.auth.activate', $data, function($message) use ($input)
{
$message->to(e($input['email']), e($input['partnerable_name']))
->subject('Activate your account');
});
Anybody got an idea what the solution for this error is?
Thanks in advance,
Kibo
Remove /bootstrap/compiled.php I think it will work for you.
You need to remove this from your Mail::send call. The function should be the third parameter so I'm not sure what you're trying to do here -- the $input['email'] field will already be available within the function due to your "use ($input)"
$email = e($input['email']

Resources