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/
Related
I am trying to send email using Laravel via yahoo small business but I am getting This error please help me.
This is what I am getting an error(Image link)
My .env file
MAIL_DRIVER=smtp
MAIL_HOST=smtp.bizmail.yahoo.com
MAIL_PORT=465
MAIL_USERNAME=****#yahoo.com
MAIL_PASSWORD=*******
MAIL_ENCRYPTION=null
My Send email Code
public function index()
{
$this->sendMail('mails.subscribe', '******#****.**', '******#**.**',
'Testing', 'Test');
}
public function sendMail($bladeName, $from, $to, $subject, $body)
{
Mail::send(['html' => $bladeName], ['user' => $body], function
($message) use ($to, $subject, $from) {
$message->to($to)->subject($subject);
$message->from($from, 'Test');
});
}
Try to change the smtp port to : 587
Sorry for my other answer I overlooked your code.
your mail function is wrong that's why you are not connecting
try :
Mail::send('emails.welcome', ['key' => 'value'], function($message)
{
$message->to('foo#example.com', 'John Smith')->subject('Welcome!');
});
One more :
Mail::send('emails.welcome', $data, function($message)
{
$message->from('us#example.com', 'Laravel');
$message->to('foo#example.com')->cc('bar#example.com');
$message->attach($pathToFile);
});
I want to send activation email/code to their emails during signup procedure but I am unable to find suitable answer that help me to complete my work.
This is my controller method where i am saving user data into my database using sentinel.
public function postRegister(Request $request)
{
$user = Sentinel::registerAndActivate($request->all());
return redirect('/');
}
Here i want to sent activation email/code to their email when user signup.
Your any help would be highly appreciated!
public function postRegister(Request $request)
{
$user = Sentinel::register($request->all());
$activation = Activation::create($user);
$this->sendEmail($user, $activation->code);
return redirect('/');
}
private function sendEmail($user,$code)
{
Mail::send('emails.activation',[
'user' => $user,
'code' => $code
], function($message) use ($user){
$message->to($user->email);
$message->subject("Hello $user->first_name,
activate your account.");
});
}
Use this code
use MAIL namespace in your controller moreover don't forget to create email.verify blade in your application path resources\views\email and create smtp detail in .env file.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=username
MAIL_PASSWORD=password
MAIL_FROM_ADDRESS=your_email#address.com
MAIL_FROM_NAME=YOURNAME
MAIL_ENCRYPTION=null
public function postRegister(Request $request)
{
$verification_code = 111111; // create random number & save it to db ;
$user = Sentinel::registerAndActivate($request->all());
return redirect('/');
$subject = "Please verify your email address.";
Mail::send('email.verify', ['name' => $user->name, 'verification_code' => $verification_code],
function ($mail) use ($user, $subject) {
$mail->from(getenv('FROM_EMAIL_ADDRESS'), "YOUR APPLICATION NAME");
$mail->to($user->email, $user->name);
$mail->subject($subject);
});
}
this is your email.verify blade :
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
</head>
<body>
<div>
Hi {{ $name }},
<br>
Thank you for creating an account with us.
<br>
Your verification code : <b>{{$verification_code}}</b>
<br/>
</div>
</body>
</html>
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)
I'm trying to send mail with Laravel 5.2 and I'm trying to do it with function Mail::send
Mail::send('reminder', ['user' => $user], function ($m) use ($user) {
$m->from('hello#app.com', 'Your Application');
$m->to($user->email, $user->name)->subject('Your Reminder!');
});
This is what i have in views/reminder.blade.php
Hello <?php echo $user->name; ?> this is your mail
Now when I try to use the method for this, it gives me the following error:
InvalidArgumentException in FileViewFinder.php line 137: View [reminder] not found.
Can someone please explain to me why this happens and how can I fix this problem?
Thank you.
In your example, you're specifying that the blade template you want to use is called reminder and it needs to be in the root of the resources/views directory.
Mail::send('reminder', ['user' => $user], function ($m) use ($user) {
$m->from('hello#app.com', 'Your Application');
$m->to($user->email, $user->name)->subject('Your Reminder!');
});
It's likely that your view is in another directory, people often put their email templates in resources/views/mail or similar so they are separated from other types of view. In this case, you would use Laravel's dot notation to specify a directory:
Mail::send('mail.reminder', ['user' => $user], function ($m) use ($user) {
$m->from('hello#app.com', 'Your Application');
$m->to($user->email, $user->name)->subject('Your Reminder!');
});
I would assume that your view isn't in the root of the resources/views directory so would either need to be placed there, or you should use dot notation to specify which directory the view is in.
I have been able to get my Iron.io push queue subscription to work great, but not with mail queues for some reason. In the code below I am pushing a job onto the queue with an email address and username in the $data array. For reasons unknown to me, the email address and username are not getting passed to the Mail function. The job just sits there, failing to send.
FooController.php
<?php
$data = array(
'email_address' => Input::get('email'),
'username' => Input::get('username'),
);
Queue::push('SendEmail#reserve', $data);
routes.php
<?php
// iron.io push queue path
Route::post('queue/push', function()
{
return Queue::marshal();
});
class SendEmail
{
public function reserve($job, $data)
{
Mail::send('emails.reserve', $data, function($message)
{
$message->to($data['email_address'], $data['username'])->subject($data['username'] . ', welcome to RockedIn!');
});
$job->delete();
}
}
?>
You need to pass $data to the closure.
public function reserve($job, $data)
{
Mail::send('emails.reserve', $data, function($message) use ($data)
{
$message->to($data['email_address'], $data['username'])->subject($data['username'] . ', welcome to RockedIn!');
});
$job->delete();
}