I'm building a project with Laravel version 7.28 and I'm trying to send notification to my users. I'm based on https://www.itsolutionstuff.com/post/laravel-7-send-email-exampleexample.html
.env file
MAIL_MAILER=smtp
MAIL_HOST=mail.google.com
MAIL_PORT=587
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=ssl
MAIL_FROM_ADDRESS=
MAIL_FROM_NAME="lorem ipsum"
web.php
Route::get('/send-notification', 'MailController#sendNotification');
MailController.php
<?php
namespace App\Http\Controllers;
use App\Mail\Test;
use Illuminate\Support\Facades\Mail;
class MailController extends Controller
{
public function sendNotification()
{
$details = [
'title' => 'title',
'body' => 'body'
];
Mail::to('info#londonistinvestments.com')->send(new Test($details));
echo 'email has sent';
}
}
Under app folder, I have a directory named Mail and under it I have Test.php. I created it with "php artisan make:mail MyTestMail" command on terminal.
Here is Test.php file
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class Test extends Mailable
{
use Queueable, SerializesModels;
public $details;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($details)
{
$this->details = $details;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->subject('Notification - New Lead')->view('email.test');
}
}
Finally test view that is under resources/views/email
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Notification</title>
</head>
<body>
<h1>lorem isppsum</h1>
<p>thank you</p>
</body>
</html>
I'm using Xampp, PHP version is 7.2.34. I'm able to send emails on local but when I upload to my shared hosting that uses PHP version 7.3 I get error. Which is;
I didn't understand the problem. I also tried to connect mail.gmail.com on ports 25, 465 and 587 with ssl and tls. What sould I do?
The shared hosting provider is likely blocking those ports.
I had a similar situation with trying to email (integration with mailgun but still using those ports) from shared hosting (A2 Hosting) and after going through their support was informed that those ports are not available and it would not work.
I ended up having to switch to VPS to solve my problem.
Related
I'm new in Laravel and working with Laravel 6. For customer contact.
curently when i submit contact form and sending email perfectly by given email id demo#gmail.com But, need to extend it with one following features:
1):Customer will get an email just after contact.
Does anyone have an idea ? please help me thanks.
Controller
public function store(Request $request)
{
$contactemail = new ContactEmail;
$contactemail->name = $request->name;
$contactemail->email = $request->email;
$contactemail->contact = $request->contact;
$contactemail->subject = $request->subject;
$contactemail->message = $request->message;
$contactemail->save();
Mail::to(config('wall_master_furishing.mail_to'),$contactemail->email)->send(new EnquiryEmail($contactemail));
return back()->with('success', 'We Will Contact You Soon')
->with('path', $contactemail);
}
Mailable class
directory
app/Mail/EnquiryEmail.php
class EnquiryEmail extends Mailable
{
use Queueable, SerializesModels;
public $enquiry;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct(ContactEmail $enquiry)
{
$this->enquiry = $enquiry;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->view('email.contactmail');
}
html view
app/resources/views/emails/contactmail.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>WallMaster enquiry Us Mail</title>
</head>
<body>
<p>Name : {{$enquiry->name}}</p>
<p>email : {{$enquiry->email}}</p>
<p>Phone : {{ $enquiry->contact}}</p>
<p>Subject : {{$enquiry->subject}}</p>
<p>Message : {{ $enquiry->message}}</p>
</body>
.env
MAIL_TO=demo#gmail.com
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=d6d151d01264b5
MAIL_PASSWORD=4c5c45ae453ae2
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=demo#gmail.com
MAIL_FROM_NAME="${APP_NAME}"**
if u meant after contact is after redirected, then use laravel queue
I am trying to send an auto-reply to the newly registered user and I want to send them an image rather than the text but when I try to send the image the mail does send but the image received is broken. I studied many answers from StackOverflow as well tried many other solutions from the internet but nothing seems to be working.
My mail class:
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class autoMail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->subject("Subject")
->view('emails.autoemail');
}
}
Here is my function which is calling this mail class.
Mail::to($request->email)->send(new autoMail());
And last but not least my view which I am sending as an email.
<!DOCTYPE html>
<html>
<head>
<title>Auto Email</title>
</head>
<body>
<img src="{{ asset('img/email.png') }}">
</body>
</html>
You can manually pass your data to the view via the with() method.
class autoMail extends Mailable
{
use Queueable, SerializesModels;
public function build()
{
$image = env('APP_URL')."/img/email.png";
return $this->subject("Subject")
->view('emails.autoemail')
->with(['image' => $image]);
}
}
Once the data has been passed to the with() method, it will automatically be available in your view, so you may access it like you would access any other data in your Blade templates:
<!DOCTYPE html>
<html>
<head>
<title>Auto Email</title>
</head>
<body>
<img src="{{ $image }}">
</body>
</html>
Laravel Nullable is not working, i try everything but it still not working. Please Look My Code..
Controller :-
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Pop;
class PopController extends Controller
{
public function index(){
return view('test2');
}
public function create(Request $request){
$formvalidtion = $request->validate([
'usercode' => ['nullable', 'required'],
]);
return "<h1>SUCCESS</h1>";
}
}
Model :-
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Pop extends Model
{
protected $timestamps = false;
protected $fillable = ['usercode'];
}
Migration :-
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePopsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('pops', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('usercode')->nullable();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('pops');
}
}
Route :-
route::get('test', 'PopController#index');
route::post('testcheck', 'PopController#create')->name('uc');
View :-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Nullable Test</title>
</head>
<body>
<h1>Test Nullable</h1>
<form action="{{ route('uc') }}" method="POST">
#csrf
<input type="number" name="usercode" id="" placeholder="Enter User Code">
#error('usercode')
{{ $message }}
#enderror
<br>
<input type="submit" >
</form>
</body>
</html>
I spend 1 hour to find solution of but not get. some of youtube video in mentioned that datatype of field must be int and nullable . this also not working is there any solution available?
It's not right to use nullable and required validations rules together:
required: The field under validation must be present in the input data and not empty.
nullable: The field under validation may be null.
present: The field under validation must be present in the input data but can be empty.
filled: The field under validation must not be empty when it is present.
prohibited: The field under validation must be empty or not present.
sometimes: The field under validation will only be validated if it is present.
exclude: The field under validation will be excluded from the request data returned by the validate and validated methods.
You may use nullable and present together:
$formvalidtion = $request->validate([
'usercode' => ['nullable', 'present'],
]);
You don't specify what you mean with "not working" (i.e. what you expect, but what happens instead), but I'm assuming you mean that null values are not accepted by the validation.
That would be because the required rule expects a value to be non-empty. So your nullable rule probably passes, but required fails.
If you want a value to be present, but not necessarily non-empty, you can use the present rule.
In my app I have a users table and a profiles table. When a user goes to their dashboard, they should be able to click a link to view their profile page. Here's the link:
link to your profile page
However, I am getting the error: Route [profiles.show] not defined.
I'm a novice and am not clear on how to link a signed up user with his/her profile page. All users should have a profile page on sign up.
I'd appreciate some guidance! Here is what I have so far:
The link to profile page
link to your profile page
ProfilesController.php
namespace App\Http\Controllers;
use App\Profile;
use Illuminate\Http\Request;
class ProfilesController extends Controller
{
public function show($id)
{
$profile = Profile::find($id);
return view('profiles.show', compact('profile'));
}
}
Profile.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Profile extends Model
{
public function user()
{
return $this->belongsTo('User');
}
}
routes/web.php
Route::get('pages/profiles', 'ProfilesController#show');
profiles.blade.php
This is just a very simple page for now.
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>{{ $user->user_id }}</h1>
<p>{{ $user->about_me }}</p>
</body>
</html>
Solution
I found an easy solution and I wanted to post it here to help others who might be struggling with creating a user profile page. The below assumes you already have a users table in your database and now you want to create a profiles table and connect user ID to their profile page.
Adding Laravel User Profiles
This is the video which help me.
Create table
php artisan make:migration create_profiles_table
This creates a migration file:
2019_09_22_213316_create_profiles_table
Open migration file and add extra columns you need:
$table->integer('user_id')->unsigned()->nullable();
$table->string('about_me')->nullable();
Migrate these to database
php artisan migrate
Now we have our database sorted, we need to create a controller to control how our php functions.
ProfilesController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProfilesController extends Controller
{
public function show($user_id)
{
$user = User::find(1);
$user_profile = Profile::info($user_id)->first();
return view('profiles.show', compact('profile', 'user'));
}
public function profile()
{
return $this->hasOne('Profile');
}
}
routes/web.php
Route::get('dashboard/profile', 'ProfilesController#show');
Profile.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Profile extends Model
{
public function user()
{
return $this->belongsTo('User');
}
}
Add this to User.php
public function profile()
{
return $this->hasOne('Profile');
}
profile.blade.php
Create any design you want. If you want to pull in users name, include {{ Auth::user()->name }}
try this
Route::get('pages/profiles/{id}', 'ProfilesController#show')->name('profiles.show');
Because in link you use a name route but in web.php there is no name route called profiles.show.so it's show error.And In Route You Need To pass the ID.
At Blade File :
link to your profile page
Change Route Style like below :
Route::get('pages/profiles/{id}', 'ProfilesController#show')->name('profiles.show');
At Profile Model
public function user()
{
return $this->belongsTo(User::class);
}
In Profiles.blade.php
<body>
<h1>{{ $profile->id }}</h1>
<p>{{ $profile->about_me }}</p>
</body>
You Passed User information through "profile" parameter. So you have to write this name in blade file.
Note Route Function won't work if you don't mentioned any name for this Route name. Either you have to use URL.
Your route is wrong, take a look at named routes. If you don't use Route::resource(), you have to manually name your routes and specify when you are expecting a parameter (in this case the profile ID).
Route::get('pages/profiles/{id}', 'ProfilesController#show')->name('profiles.show');
link to your profile page
Route model binding is probably the way to go in this case.
Route::get('pages/profiles/{profile}', 'ProfilesController#show')->name('profiles.show');
namespace App\Http\Controllers;
use App\Profile;
use Illuminate\Http\Request;
class ProfilesController extends Controller
{
public function show(Profile $profile)
{
return view('profiles.show', compact('profile'));
}
}
am trying to develop a web application with the help of LARAVEL framework and am successfully installed the Laravel in my laptop.
I want to make a basic controller and a view program. and routing . is there any error in my program and reply to this question please.
My Controller,view, routes files are described in below
NewController.php
<?php
class New_Controller extends BaseController {
public function action_index()()
{
return View::make('hai');
}
}
hai.php
Laravel Basics
<body>
<h1>Jishad is Developing Laravel 4</h1>
</body>
</html>
Routes.php
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/
Route::get('/', function()
{
return View::make('hai');
});
You may try something like following. Declare the route (It'll call index method from NewController when you access home page):
Route::get('/', 'NewController#index');
Now create your NewController like this:
// NewController.php
class NewController extends BaseController {
// You may keep this line in your BaseController
// so you don't need to use it in every controller
protected $layout = 'layouts.master';
public function index()
{
// Make the view and pass a $name variable to the view with
// Jishad as it's value and then set the $view to the layout
$view = View::make('hai')->with('name', 'Jishad');
$this->layout->content = $view;
}
}
Now Since you are new to this framework so I would suggest to use controller layout instead of blade layout but you may find everything about layout/templating here. To make it working you need to create the master layout in app/views/layouts folder like this:
// app/views/layouts/master.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Simple Web Page</title>
</head>
<body>
<div><?php echo $content; ?></div>
</body>
</html>
Also need to create the hai view in app/views folder like:
// hai.php
<h1>Welcome TO Laravel</h1>
<p><?php echo $name ?> is developing learning Laravel</p>
You need to read more about Laravel, check the Laravel - 4 documentation and read some articles/books. Also You used action_index but it was used in Laravel - 3, just use index.
1.Your routing is wrong, if you want to point your route to a controller do this:
Route::get('/', 'NewController#action_index');
2.If the name of your controller is NewController then your class should also be that:
class NewController extends BaseController {
public function action_index()
{
return View::make('hai');
}
}
3.Also the public function action_index()() should be public function action_index().