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`
Related
I'm developing a small script locally, which allows users to send an email from the post posted by another user. A simple contact form that then sends an email from user to user.
I have configured my .env with the corresponding Mailtrap sample data.
My problem is that every email sent is sent to Mailtrap and not to the user's email.
public function html_email(Request $request)
{
$request->validate([
'message_to' => 'required|string|max:255',
]);
$data = array('name' => Auth::user()->name);
Mail::send('mail', $data, function($message) {
$message
->to(request('email_to'))
->subject('Someone is interested in your ad!');
$message->from(Auth::user()->email, Auth::user()->name);
});
return back()
->with('success', __('app.email_successfully_sent'));
}
What am I doing wrong?
Ok, but setting a real email in .env when in a post a user has to contact another user, where is the email addressed? To the email set by default to .env? I want it to be sent to the email address of the user who posted the post.
I took an example from the Laravel 5.1 documentation for Mail and replaced it with my send and receiver email ids.
Mail::raw works in the controller and if I use Mail::send in tinker it works. However, if I use Mail::send in the controller it doesn't work.
Everything is set up as described on the Laravel 5.1 mail page. https://laravel.com/docs/5.1/mail#sending-mail . I have also cleared app cache, config cache and view cache.
public function sendEmailReminder(Request $request, $id)
{
$user = User::findOrFail($id);
Mail::send('emails.reminder', ['user' => $user], function ($m) use ($user) {
$m->from('hello#app.com', 'Your Application');
$m->to($user->email, $user->name)->subject('Your Reminder!');
});
}
The error could be from the email you are trying to send from, in order to send the email successfully this email hello#app.com has to be a valid email address and the one register as your MAIL_USERNAME
It was a permissions issue for the storage/frameworks directory. Once I changed the permissions it worked fine..
I am using vue.js as my front end and Lumen as my api service. Now I need to send emails from lumen. This is what i did for this.
.env file
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=********#gmail.com
MAIL_PASSWORD=**********
MAIL_FROM_ADDRESS=******#gmail.com
MAIL_FROM_NAME=Sample Email App
MAIL_ENCRYPTION=tls
and then edited the file bootstrap\app.php and uncommented the following lines.
$app->register(App\Providers\AppServiceProvider::class);
$app->withFacades();
Dotenv::load(__DIR__.'/../');
$app->withEloquent();
In the controller, I have used the following code
use Illuminate\Support\Facades\Mail;
private function sendActivationEmail( $email = null ){
$email_sent = false;
if( $email != null ){
// send email
Mail::raw('Raw string email', function($msg) {
$msg->to(['tismon#gmail.com']); $msg->from(['x#x.com']);
});
}
return $email_sent;
}
Unfortunately, this is not working. Can anyone tell me where I went wrong ?
Register MailServiceProvider in bootstrap/app.php file
$app->register(\Illuminate\Mail\MailServiceProvider::class);
$app->configure('mail');
try adding below code to AppServiceProvider.php within register function
$this->app->singleton('mailer', function ($app) {
$app->configure('services');
return $app->loadComponent('mail', 'Illuminate\Mail\MailServiceProvider', 'mailer');
});
if not working After the above configuration, try Mail send function
Mail::send('user.emails.registration' , $data, function($msg) use ($to,$from,$subject)
{
$msg->to($to)->from($from)->subject($subject);
});
I'm trying to send a post request to my Laravel app so that I could create a User without the UI.
I've tried sending a post request via cURL:
curl --data "name=test&password=password120918&email=app#test.com" http://localhost:8080/register
This didn't work.
This is a fresh install of Laravel 5.4
I can't find anything to do with the RegisterController in the routes/web.php file.
What would the url to register a user be for Laravel 5.4? (I'm pretty sure it works the same way as 5.3)
Thank you.
I know that in laravel there is a default way to do things, But if your just looking to create a user from post request and send it back as a response you can do it yourself.
in your routes.php
Route::post("/users", "UsersController#store");
Then create a UsersController.php and add the method:
public function store(Request $request){
//You should add validation before creating the user.
$user = App\User::create([
"email" => $request->email,
"name" => $request->name,
"password" => bcrypt($request->password)
]);
if(!$user){
return response(["error" => "Your error here"], 400);
}
return response(["user" => $user], 200);
}
Then try it our with postman or curl command like
curl -X POST -F 'name=Testing User' -F 'password=pass1234' -F 'email=testing#gmail.com' http://localhost:8080/users
php artisan route:list
Will show you all of the registered routes for your application. When using Laravel's built in auth, routes are registered without them actually being in your routes file.
By default Laravel adds a POST route for /register to
App\Http\Controllers\Auth\RegisterController#register
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'))