I want to send an email to the user who has just entered his email address.
Like this,
$email = $request->email;
Mail::send('emails.info', $data, function ($message) {
$message->from('myemail#gmail.com', 'My Email');
$message->to($email)->subject('The subject');
});
But this returns an error:
Undefined variable: email
Where is the problem?
You need to reference email within the closure use use
$email = $request->email;
Mail::send('emails.info', $data, function ($message) use ($email) {
$message->from('myemail#gmail.com', 'My Email');
$message->to($email)->subject('The subject');
});
Related
I try mailgun and it works to send an email, but the problem is when I run the function, it sent double email.
the function is like this:
public function sendEmail($id){
try{
$user= Users::find($id);
$data = array('username'=>$user->name, 'email'=>$user->email);
Mail::send('emails.hapstics', $data, function ($message) use($data){
$message->to($data['email'], $data['username'])->subject('Test Subject');
});
return redirect('leads');
}
catch(Exception $e){}
}
.env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=*hidden
MAIL_PASSWORD=*hidden
MAIL_FROM_NAME=*hidden
MAILGUN_DOMAIN=*hidden
MAILGUN_SECRET=*hidden
You can use this pattern for sending email.
$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!');
});
You can send mail using Mailable as laravel docs https://laravel.com/docs/5.6/mail#generating-mailables
tutorial https://appdividend.com/2018/03/05/send-email-in-laravel-tutorial/
I am trying to use Mail function in Laravel. Heres the code
public function basic_email(){
$data = array('name'=>"Virat Gandhi");
Mail::send(['text'=>'mail'], $data, function($message) {
$message->to('shanipasrooria#gmail.com', 'Tutorials Point')->subject
('Laravel Basic Testing Mail');
$message->from('m.usman5991#gmail.com','Virat Gandhi');
});
echo "Basic Email Sent. Check your inbox.";
}
I have made changes in .env file. Set everything, Heres my route.
Route::get('sendbasicemail','MailController#basic_email');
I get the following Error.
InvalidArgumentException in FileViewFinder.php line 137:
View [mail] not found.
You can try this code
Mail::send([], [], function ($message) {
$message->to('shanipasrooria#gmail.com', 'Tutorials Point')
->subject('subject')
->setBody('some body', 'text/html');
});
you can try this
$html = '<h1>Hi, welcome Virat!</h1>';
Mail::send([], [], function ($message) use ($html) {
$message->to('shanipasrooria#gmail.com', 'Tutorials Point')
->subject('Laravel Basic Testing Mail')
->from('m.usman5991#gmail.com','Virat Gandhi')
->setBody($html, 'text/html'); //html body
or
->setBody('Hi, welcome Virat!'); //for text body
});
Mail::send(['text'=>'mail']< here the mail should be a valid view file.
According to API Documentation, the Mailer Class should receive a String, Array Or MailableContract, Those reference a view. So you need to pass a valid view in the send method.
void send(string|array|MailableContract $view, array $data = [], Closure|string $callback = null)
My case is that a user's unique email can have many accounts .
Means that one email is associated with many accounts on unique username's.
My current code works for mail, I want to send email on their username, Means that user will enter his username then the email associate with that username will get the password reset link
Current working code for direct email :
public function postEmail(Request $request)
{
$validator = Validator::make(Input::get(),
[
'email' => 'required|email'
]
);
if ($validator->fails()) {
return redirect()
->back()
->withErrors($validator->errors())
->with('message', 'Please fix your form fields.')
->with('form', 'recover')
->withInput(Input::except('password'));
}
$response = $this->passwords->sendResetLink($request->only('email'), function($message)
{
$message->subject('Password Reminder');
});
switch ($response)
{
case PasswordBroker::RESET_LINK_SENT:
return redirect()
->back()
->with('message', 'A verification email was sent to your email inbox.')
->with('form', 'recover')
->withInput(Input::except('password'));
case PasswordBroker::INVALID_USER:
dd('true');
}
}
I have added the following line :
$usernameToEmail = App\User::where('name','=', Input::get());
And then i passed $usernameToEmail->email to
$response = $this->passwords->sendResetLink($usernameToEmail->email,
function($message)
{
$message->subject('Password Reminder');
});
Which throws the following error :
Type error: Argument 1 passed to Illuminate\Auth\Passwords\PasswordBroker::sendResetLink() must be of the type array, string given
This is happening because the PasswordBroker is trying to retrieve a use by the credentials that you give and it will make a DB query to get a user. So if your username is unique you could do it like this:
$this->passwords->sendResetLink(
['username' => $username],
function($message){...}
);
This will not work you need to have unique email and username.
as token is generated against a unique users row in table and used when resetting.
I'm making a contact form. If someone fills in the form, the data (text,subject) should be sent to my mail. Everything works, but when I fill in form and send the email, it sends the whole HTML page (not just the text from the form).
Here's how it looks like
Here's my controller where I send the mail
public function sendEmail(Request $request)
{
$data["username"] = $request->username;
$data["email"] = $request->email;
$data["subject"] = $request->subject;
$data["mail_text"] = $request->mail_text;
Mail::send('contact', ["data" => $data], function ($mail) use ($data){
$mail->to("leribobo#gmail.com")->from($data["email"])->subject($data["subject"]);
});
return redirect()->back();
}
as I guess it sends 'contact' page, which is first parameter in method
Mail::send('contact' ....
You are actually sending the contact.blade.php file. This is not how we should send a mail.
Mail::send('emails.contact', $data, function ($message) {
//
$message->from('us#example.com', 'Laravel');
$message->to('foo#example.com')->cc('bar#example.com');
});
You've to make a file in emails.contact.blade.php and pass $data to the blade file as showed above.
Check here: https://laravel.com/docs/5.1/mail#sending-mail
When I'm trying to send an email using the swiftmailer in laravel I get the folowing error: "Missing argument 2 for UsersController::{closure}()".
My code is below:
Mail::send('emails.default', array('key' => Config::get('settings.WELCOMEMAIL')), function($message, $mail, $subject = 'Welcome!')
{
$message->to($mail)->subject($subject);
});
It's really weird though. The $mail variable contains a valid email address and I'm not using the UsersController at all in this script.
Thanks in advance
You must pass only the $message to the closure. Any additional variable must be passed down with the use keyword:
Mail::send('emails.default', array('key' => Config::get('settings.WELCOMEMAIL')), function($message) use($mail, $subject)
{
$subject = empty($subject) ? 'Welcome!' : $subject;
$message->to($mail)->subject($subject);
});