Laravel Mail queues with Iron.io - php

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();
}

Related

laravel: mailgun sending double emails

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/

email in laravel not working

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)

Send massive emails in laravel

Goodnight
Porblem 1.-
I need to send more than 1000 emails for each event created, and for this I use queue (as Laravel's documentation says), but when sending the emails I have to wait until all the emails are sent to return to the view of control Panel
this is my "store" function in NewsEvents.php controller that sends the emails
public function store(Request $request)
{
$attributes = request()->validate(News::$rules, News::$messages);
$news = $this->createEntry(News::class, $attributes);
//queue for sending emails
$this->dispatch(new Nevent($news));
return redirect_to_resource();
}
the "handle" function of job "Nevent.php"
public function handle()
{
//
$users=User::where('tipo_user','user')->get();
foreach($users as $user)
{
$user->notify(new EventCreated($this->news));
echo 'enviado correo';
Informe::create([
'event_id' => $this->news->id,
'total' => '1',
'tipo' => 'invitacion',
'dst_id' => $user->id,
'estado' => 'correcto',
]);
}
}
What could be the problem?
problem 2.-
How could I send an email for every minute?
since when sending all emails my server responded with this message:
Domain mu.edu.fi has exceeded the max emails per hour (100/100 (100%)) allowed. Message will be reattempted later
If u are using Redis server for managing jobs, Laravel provides a simple
API for Rate Limiting API's
Redis::throttle('your job id')->allow(10)->every(60)->then(function () {
// Job logic...
}, function () {
return $this->release(10);
});
Hope this helps.

Debug why a Queue failed on Laravel 5.2

I'm trying to dispatch Welcome Email on Laravel 5.2 but the job is failing and I have no idea how to figure out why.
Here is what I have so far:
public function test(Request $request) {
$email = $request->get('email');
$subject = $this->word('emails.welcome.subject');
// Mail::send # I use this to Send it synchronously
// $this->mailQueue->queue # I use this to Queue
('emails.welcome', ['domain' => 'blablabla', 'name' => 'Marco Aurélio Deleu', 'date' => Carbon::now(),
'total' => 100, 'pretext' => '', 'score' => '', 'surtext' => '', 'password' => 'blabla', 'shop' => ''],
function ($message) use ($email, $subject) {
$message->to($email);
$message->subject($subject);
}, true);
return 'queued';
}
I noticed that Jobs can easily fail when any kind of exception is thrown, so I figured that I could use Mail::send to find out which exception was being thrown / what was the problem and then use the $this->mailQueue->queue() when I had it sorted out.
I managed to use this strategy to find out that I can't use function calls inside the closure, but now I just don't know what else to do in order to find where the problem lies. The synchronous method of sending the email directly works just fine and the email arrives, but the job will fail at a queue.
The last piece of evidence that I have is that the problem is in the $data array, because if I use it without any $data, the job will be processed and the email arrives.
...
// If I use it like this, it will work perfectly.
$this->mailQueue->queue
('emails.welcome', [ /* empty array */ ], function ($message) use ($email, $subject) {
$message->to($email);
$message->subject($subject);
}, true);
Here is the Queue Listener:
// --> This is the job failing with $data array <--
[06:34 PM]-[root#local]-[/project/]-[git sign-up-with-email]
# php artisan queue:listen --timeout=30 --tries=1
[2016-06-28 21:34:29] Failed: mailer#handleQueuedMessage
^C
// --> This is the job working with empty $data array <--
[06:35 PM]-[root#local]-[/project/]-[git sign-up-with-email]
# php artisan queue:listen --timeout=30 --tries=1
[2016-06-28 21:44:31] Processed: mailer#handleQueuedMessage

email sends whole html page(instead of email body) in laravel 5.1

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

Categories