i have a form in laravel which sends the data to email and database, i am trying to store the image from form in public folder, then the following error happened
Unable to open file for reading [/hermes/walnaweb08a/b1307/as.book/TEIA/storage/app/public/1562913994bannernew.jpg]
the error is occured in the following code for swiftmailer
private function getReadHandle()
{
if (!isset($this->reader)) {
$pointer = #fopen($this->path, 'rb');
if (!$pointer) {
throw new Swift_IoException('Unable to open file for reading ['.$this->path.']');
}
$this->reader = $pointer;
if (0 != $this->offset) {
$this->getReadStreamSeekableStatus();
$this->seekReadStreamToPosition($this->offset);
}
}
return $this->reader;
}
this is my sendmail.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use App\Register;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class SendEmail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* #return void
*/
public $sub;
public $msg;
public $phot;
public $rec;
public $sig;
public function __construct($subject,$message,$photo,$recipt,$sign)
{
$this->sub = $subject;
$this->msg = $message;
$this->phot = $photo;
$this->rec = $recipt;
$this->sig = $sign;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
$b_subject = $this->sub;
$b_message = $this->msg;
$reg = Register::where('id',$b_message)->first();
$b_phot = $this->phot;
$b_phot = $this->phot;
$recipt = $this->rec;
$sign = $this->sig;
return $this->view('mail.sendmail', compact("reg","b_phot","sign","recipt"))->subject($b_subject);
}
}
the image is stored in the folder and data is sent to database, but the problem is this error occurs and the data is not sent to mail. can anyone please tell what coul be the problem
Related
I'm trying to catch Vonage API errors in my Laravel job file so that I can determine whether to deduct credits from a user's account.
My job consists of a function called attemptSend. When I log the before and after, I can see that the message attempt, first and second logs are all logging, but not the message failed despite there being an exception thrown and logged by Laravel.
What am I missing here to catch this?
Here's my job file:
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Log;
use App\Notifications\Messages\SendMessageViaVonageEngine;
use App\Models\User;
use App\Models\Message;
use App\Models\Sanctum\PersonalAccessToken;
use Carbon\Carbon;
class ProcessMessage implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* The number of times the job may be attempted.
*
* #var int
*/
public $tries = 1;
/**
* The message id
*/
protected $messageId;
/**
* The user id
*/
protected $userId;
/**
* The API key's UUID.
*/
protected $apiKeyUuid;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($messageId, $userId, $apiKeyUuid)
{
$this->messageId = $messageId;
$this->userId = $userId;
$this->apiKeyUuid = $apiKeyUuid;
}
/**
* Get API key
*/
public function getApiKey()
{
return PersonalAccessToken::where('tokenable_id', $this->userId)
->where('uuid', $this->apiKeyUuid)
->withSum('credit_transactions AS credit_balance', 'delta')
->first();
}
/**
* Get the message
*/
public function getMessage()
{
return Message::where('id', $this->messageId)
->where('user_id', $this->userId)
->first();
}
/**
* Update message status
*/
public function updateMessageStatus($message, $status = 'sending', $reason = null)
{
$message->status = $status;
// set reason
if ($reason) {
$message->reason = $reason;
}
// mark the sent_at date
if (!$message->sent_at && $status == 'sending') {
$message->sent_at = Carbon::now();
}
$message->save();
}
/**
* Attempt to send
*/
public function attemptSend($message)
{
Log::debug('MESSAGE:ATTEMPT');
try {
Log::debug('MESSAGE:SUCCESS-FIRST');
$sent = Notification::route('vonage', $message->send_to)
->notify(new SendMessageViaVonageEngine($message));
Log::debug('MESSAGE:SUCCESS-SECOND', [
'sent' => $sent
]);
} catch (\Exception $e) {
Log::debug('MESSAGE:FAILED');
throw new \Exception($e->getMessage());
}
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$apiKey = $this->getApiKey();
$message = $this->getMessage();
if (! $message) {
throw new \Exception("Message can't be found, was it deleted? Unable to send message.");
return;
}
if (! $apiKey) {
$this->updateMessageStatus($message, 'dead', "API key not found");
throw new \Exception("API key can't be found, was it deleted? Unable to send message.");
return;
}
if (! $apiKey->credit_balance || $apiKey->credit_balance <= 0) {
$this->updateMessageStatus($message, 'dead', "Insufficient credits");
throw new \Exception("API key: $apiKey->uuid has run out of credits. Unable to send message.");
return;
}
$this->updateMessageStatus($message, 'sending', "Attempting to send");
$this->attemptSend($message);
}
}
Here is the piece of code. My idea is that each email comes out with one of the values/entry from one of my tables.
public function __construct($siniestro)
{
$this->siniestro = $siniestro;
$this->subject = {{ $siniestro->siniestro }};
}
[from this place I want to get my subject][1]
[1]: https://i.stack.imgur.com/D0PNO.png
this is all the code of my mailable
class ContactanosMailable extends Mailable
{
use Queueable, SerializesModels;
$this->subject = $siniestro->siniestro;
public $siniestro;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($siniestro)
{
$this->siniestro = $siniestro;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->view('emails.contactanos');
}
}
If I can add something else just let me know, and I will.
I hope this can reach, it's getting complicated
I would do it like that :
class ContactanosMailable extends Mailable
{
use Queueable, SerializesModels;
public $siniestro;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($siniestro)
{
$this->siniestro = $siniestro;
// set the subject property
$this->subject = $siniestro->siniestro;
// or use the subject method
$this->subject($siniestro->siniestro);
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->view('emails.contactanos');
}
}
using the subject() method looks cleaner but both works
public function build(){
$from_email = "test#mail.com";
$subject = "Account Request";
return $this->from($from_email)->subject($subject)->view('emails.contactanos')
;
}
And also please check the code below Laravel Cron Job the compiler is not go to the if condition after sending mail. Its not deleting or update the status when email fails. Please see the code. What should I do for that. After sending mail, if mail fails means i.e Mail::fails function its not to do any think. I want to change the status if mails fails and delete the record if mail send successfully. Please help me
<?php
namespace App\Console\Commands;
use App\Mail\SendEmail;
use App\Models\CronjobSetting;
use App\Models\ProcessingEmail;
use App\Models\SmtpServer;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Mail;
class SendEmailsBasedonDate extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'sendemailviadate:cron';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return int
*/
public function handle()
{
$cronJob = CronjobSetting::whereDate('created_at', Carbon::today())->first();
if ($cronJob->email_group_type == 1) {
$processingEmails = ProcessingEmail::all();
foreach ($processingEmails as $processingEmail) {
$smtpServer = SmtpServer::where('id', $processingEmail->smtp_id)->first();
Config::set('mail.mailers.smtp.host', $smtpServer->hostname);
Config::set('mail.mailers.smtp.port', $smtpServer->port);
Config::set('mail.mailers.smtp.username', $smtpServer->username);
Config::set('mail.mailers.smtp.password', $smtpServer->password);
$email = new SendEmail($processingEmail);
Mail::to($processingEmail->recipient_email)->send($email);
if (Mail::failures()) {
ProcessingEmail::where('id', $processingEmail->id)->update(
['status' => 3]
);
} else {
ProcessingEmail::destroy($processingEmail->id);
}
}
} else {
$processingEmails = ProcessingEmail::where('email_group_id', $cronJob->email_group_id)->get();
foreach ($processingEmails as $processingEmail) {
$smtpServer = SmtpServer::where('id', $processingEmail->smtp_id)->first();
Config::set('mail.mailers.smtp.host', $smtpServer->hostname);
Config::set('mail.mailers.smtp.port', $smtpServer->port);
Config::set('mail.mailers.smtp.username', $smtpServer->username);
Config::set('mail.mailers.smtp.password', $smtpServer->password);
$email = new SendEmail($processingEmail);
Mail::to($processingEmail->recipient_email)->send($email);
if (Mail::failures()) {
ProcessingEmail::where('email_leads_id', $processingEmail->email_lead_id)->update(
['status' => 3]
);
} else {
ProcessingEmail::where('email_leads_id', $processingEmail->email_lead_id)->delete();
}
}
return Command::SUCCESS;
}
}
}
mailService.php
<?php
namespace App\Http\Services;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class mailService extends Mailable
{
use Queueable, SerializesModels;
/**
* The data instance.
*
* #param $data
*/
public $data;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($data)
{
$this->data = $data;
}
public function build()
{
$address = 'janeexampexample#example.com';
$subject = 'This is a demo!';
$name = 'Jane Doe';
return $this->view('emails.test')
->from($address, $name)
->cc($address, $name)
->bcc($address, $name)
->replyTo($address, $name)
->subject($subject)
->with([ 'test_message' => $this->data['message'] ]);
}
}
clientService.php
use App\Http\Services\mailService as EmailService;
/**
* #param $clientId
* #param $comp_id
*
* #return \App\Http\Models\clients|\Illuminate\Database\Eloquent\Model
*/
public function getClient($clientId, $comp_id)
{
$data = ['message' => 'This is a test!'];
Mail::to('test#test.com')->send(new EmailService($data));
return $this->clientsRepository->getClient($clientId, $comp_id);
}
When I pass the argument $data to a new instance of EmailService I get an error saying
too few arguments passed to constructor function
but I don't understand why if I'm passing $data in the clientService, thank you for your help in advance.
I'm using the Lumen Framework 5.3 and PHP 7.2 and this code sample was taken from Sendgrid docs.
I have created a mailable php artisan make:mail SendInEmail
class SendInEmail extends Mailable
{
use Queueable, SerializesModels;
public $email;
public $sub;
public $emailcontent;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($email, $sub, $emailcontent)
{
$this->email = $email;
$this->sub = $sub;
$this->emailcontent = $emailcontent;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->subject($sub)->view('emails.sendemail');
}
}
In the build function I am passing the $sub variable, which comes from the controller, but it gives me an error:
Undefined variable: sub
When I use this:
return $this->subject('Some subject')->view('emails.sendemail');
It works fine.
P.S I did some research and found that I need use function to pass the subject variable (some anonymous function magic) (Not sure how to do that with mailables)
You're using the wrong variable, so change it to $this->sub:
return $this->subject($this->sub)->view('emails.sendemail');