I have added into a Laravel command mail::send to send an email to all users who meet the criteria. Whenever I run the first part of my foreach statement but gives me an [ErrorException] Undefined Variable: member whenever it gets to the emailing. My fire function code is below.
public function fire()
{
$members = Member::where('expire', '=', Carbon::now()->today());
$this->info('We found ' . $members->count() . ' expiring today (' . Carbon::now()->today() . ')!');
foreach ($members->get() as $member) {
$member->active = "0";
$member->save();
$this->comment($member->first_name . ' has been updated.'); //This is the last line which runs
Mail::send('emails.member.membership_expired', array('name'=>$member->first_name. ' ' . $member->last_name), function($message){
$message->to($member->email, $member->first_name . ' ' . $member->last_name)->subject('Your Membership has Expired');
});
}
}
With Mail::send you use a closure (also called anonymous function). To use local variables inside the closure you have to pass them in with use. Like that:
Mail::send('emails.member.membership_expired',
array('name'=>$member->first_name. ' ' . $member->last_name),
function($message) use ($member){
$message->to($member->email, $member->first_name . ' ' . $member->last_name)->subject('Your Membership has Expired');
}
);
Related
So I have a middleware that captures requests in/out. Here's the middleware code
public function handle($request, Closure $next)
{
$logger = "[" . $request->ip() . "]" . $request->method() . " => " . $request->fullUrl() . " [" . Carbon::now()->format("H:i:s") . "]; \nUser-Agent:" . $request->userAgent();
log_routes($logger);
return $next($request);
}
public function terminate($request, $response)
{
$logger = "[" . $request->ip() . "]" . $request->method() . " <= " . $request->fullUrl() . " [" . Carbon::now()->format("H:i:s") . "]; \nUser-Agent:" . $request->userAgent();
log_routes($logger);
}
normally this middleware output
[IP address]GET <= https://i-assume-this-should-be-my-url.com/endpoint [07:58:13];
User-Agent:User-Agent App-Version:app version
[IP address]GET => https://i-assume-this-should-be-my-url.com/endpoint [07:58:13];
User-Agent:User-Agent App-Version:app version
But somehow I don't know why it outputs foreign URL like this
[92.118.160.1]GET <= https://foreign-url.xyz [08:12:39];
User-Agent:NetSystemsResearch studies the availability of various services across the internet. Our website is netsystemsresearch.com App-Version:Not Filled
[92.118.160.1]GET => https://foreign-url.xyz [08:12:39];
User-Agent:NetSystemsResearch studies the availability of various services across the internet. Our website is netsystemsresearch.com App-Version:Not Filled
And another thing I'm really confused about is I use Cloudflare's firewall to block the incoming request if the User-Agent provided doesn't match with what I already defined. But this strange log shows there's the incoming request with unmatch user agent and can pass that firewall
Any help and explanation would be appreciated. Thank you so much
I want my application will send a email after 10 minutes of sending another email.
In my application
A user completes registration with payment
Application sends the user a payment confirmation email
Now I want to
send another email 10 minutes After payment confirmation email with welcome tips
Below is the function where for user setup .
public function finishUserSetup($Sub){
if($Sub == 0){
$subscription = SubscriptionPlans::where('identifier', '=', "Monthly")->first();
$expiry = date('Y-m-d', strtotime('+' . $subscription->months . ' months'));
$sub_period = "monthly";
} else{
$subscription = SubscriptionPlans::where('identifier', '=', "Annually")->first();
$expiry = date('Y-m-d', strtotime('+' . $subscription->months . ' months'));
$sub_period = "annually";
}
$this->expiry_date = $expiry;
$this->user_type = "SUB";
$this->subscription_period = $sub_period;
$this->update();
$replaceArray = array(
'fullname' => $this->forename . " " . $this->surname,
'subscriptionName' => $subscription->name,
);
EmailTemplate::findAndSendTemplate("paymentconfirm", $this->email, $this->forename . " " . $this->surname, $replaceArray);
}
In the above function the last line of code is the one which sends a payment confirmation email to the user which is
EmailTemplate::findAndSendTemplate("paymentconfirm", $this->email, $this->forename . " " . $this->surname, $replaceArray);
I want to execute the following line of code 10 minutes after the above one
EmailTemplate::findAndSendTemplate("WelcomeTips", $this->email, $this->forename . " " . $this->surname, $replaceArray);
How to run the above line of code after 10 minutes
First set up the queue configuration in your laravel project . then create a job with
php artisan make:job YourNameJob
Transfer the email sending process into YoutNameJob , and finally dispatch YoutNameJob twice in the finishUserSetup method, like this
public function finishUserSetup($Sub){
.
.
.
YoutNameJob::dispatch([arguments])->delay(now());
YoutNameJob::dispatch([arguments])->delay(now()->addMinutes(10));
}
I'm trying to update my Wordpress plugin since the location of PHPMailer is moved in wordpress 5.5.
I'm testing in Wordpress version 5.1 right now and i'm encountering the following error
Fatal error: Call to a member function isSMTP() on null in (path) on line (line)
As shown in the code below i've tried var_dumping the class methods and it shows isSMTP but when i call it a line later it returns the error.
if (!class_exists("\\PHPMailer")) {
global $wp_version;
if ( version_compare( $wp_version, '5.5', '<' ) ) {
require_once(\ABSPATH . \WPINC . "/class-phpmailer.php");
require_once(\ABSPATH . \WPINC . "/class-smtp.php");
require_once(\ABSPATH . \WPINC . "/class-pop3.php");
$oPhpMailer = new \PHPMailer();
}else {
require_once(\ABSPATH . \WPINC . "/PHPMailer/PHPMailer.php");
require_once(\ABSPATH . \WPINC . "/PHPMailer/SMTP.php");
require_once(\ABSPATH . \WPINC . "/class-pop3.php");
$oPhpMailer = new PHPMailer();
}
}
var_dump(get_class_methods($oPhpMailer));
$oPhpMailer->isSMTP();
Looks like if (!class_exists("\\PHPMailer")) returns false (which means the class exists). You don't set $oPhpMailer in this case which means $oPhpMailer = null. That is what the error means: you cannot run null->isSMTP().
It could very well be that this code is ran twice - the first time the $oPhpMailer gets set because the class does not exists (which is why you get the get_class_methods output). The second time the class exists, so the variable is null.
Try adding an else
if (!class_exists("\\PHPMailer")) {
// ...
} else {
$oPhpMailer = new \PHPMailer();
}
I 'm developing an android app which connects to database using PHP API. It's the first time for me to encounter PHP.
What happens is that I "POST" parameters to the php code with a URL to connect to it then php makes a query to my database and store them ... the issue is that all I see is what happens in logcat, I have no idea what is goin on with the PHP so if there is something wrong in there what can I do to debug it ?
Note : I 'm already familiar with echos and var dump I 'm looking for fully debugging tool that will allow me to debug the script without actually running it directly and by that I mean accessing it from my android project.
In such cases i log / append all actions on php side into a file - with a simple
file_put_contents({file}, {data}, FILE_APPEND);
.
u could also catch nearly every error in php with following methods:
set_error_handler({YOUR_ERROR_HANDLER}, E_ALL);
register_shutdown_function({YOUR_ERROR_HANDLER});
http://php.net/manual/de/function.set-error-handler.php
http://php.net/manual/de/function.register-shutdown-function.php
example code of how would i test an envoirement in php, which i could not debug direct:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
// will be triggered on every php shutdown - will log the last error (if one occurs)
register_shutdown_function(function()
{
// get last error
$aError = error_get_last();
if(!empty($aError))
{
logAction('called from register_shutdown_function | ' . $aError['message']);
}
});
// will be triggered on every error
set_error_handler(function($iErrCode, $sErrorText, $sErrorFile = '', $sErrorLine = '')
{
logAction('called from set_error_handler | Code: ' . $iErrCode . ' Text: ' . $sErrorText . ' File: ' . $sErrorFile . ' Line: ' . $sErrorLine);
}, E_ALL);
// will be triggered on every exception
set_exception_handler(function(\Exception $e)
{
logAction('called from set_exception_handler | Code: ' . $e->getCode() . ' Text: ' . $e->getMessage() . ' File: ' . $e->getFile() . ' Line: ' . $e->getLine());
});
// main log / debug method
function logAction($sText)
{
$sDate = date('Y.m.d H:i:s');
file_put_contents('debug.log', $sDate . ': ' . $sText, FILE_APPEND);
}
I am doing the following in my PHP
public $message = "<p>A new user has requested to signup for Company Name</p>
<p><strong>Name:</strong>". $name ."</p>
<p><strong>Email Address:</strong>" . $email . "</p>
<p><strong>Contact Number:</strong>" . $contact_number . "</p>
<p>Please check the attached file, for further details.</p>
<p>Thanks,<br/>Company Name Email Centre</p>";
As you can see I am tring to create a small segment of HTML interspersed with some PHP variables, however I am getting the following error,
Parse error: syntax error, unexpected '.', expecting ',' or ';'
It says this on line 31 which is the second <p> tag, however I cannot see a problem
You should not use var/string concatenation in a class var definition. Why don't you write a method such as getMessage() or printMessage() or something else ?
public $message= "<p>A new user has requested to signup for Company Name</p>\
<p><strong>Name:</strong>". $name ."</p>\
<p><strong>Email Address:</strong>" . $email . "</p>\
<p><strong>Contact Number:</strong>" . $contact_number . "</p>\
<p>Please check the attached file, for further details.</p>\
<p>Thanks,<br/>Company Name Email Centre</p>";
/* Try this it will work ... ( u need to escape lines using \ ) */
PHP terminates a string at newline. You need to concatenate everytime a newline begins:
public $message = "<p>A new user has requested to signup for Company Name</p>"
."<p><strong>Name:</strong>". $name ."</p>"
."<p><strong>Email Address:</strong>" . $email . "</p>"
."<p><strong>Contact Number:</strong>" . $contact_number . "</p>"
."<p>Please check the attached file, for further details.</p>"
."<p>Thanks,<br/>Company Name Email Centre</p>";
Try it this way. and have a look at http://php.net/manual/de/language.types.string.php
public $message = "<p>A new user has requested to signup for Company Name</p>" .
"<p><strong>Name:</strong>". $name ."</p>" .
"<p><strong>Email Address:</strong>" . $email . "</p>" .
"<p><strong>Contact Number:</strong>" . $contact_number . "</p>" .
"<p>Please check the attached file, for further details.</p>" .
"<p>Thanks,<br/>Company Name Email Centre</p>";
You can't do that in a class attribute definition, instead define the attribute then give it a value in the class constructor:
class Test {
public $message;
public $name = 'Demo Name';
public function __construct() {
$this->message = 'anything you want' . $this->name; # etc
}
}
In the actual definition you can only give litterals, no variable evaluations.
Cheers